Hiding field in smartTable
diff --git a/views/ngXosLib/xosHelpers/spec/ui/smart-table.test.js b/views/ngXosLib/xosHelpers/spec/ui/smart-table.test.js
index 95b7d4b..e8cc030 100644
--- a/views/ngXosLib/xosHelpers/spec/ui/smart-table.test.js
+++ b/views/ngXosLib/xosHelpers/spec/ui/smart-table.test.js
@@ -7,10 +7,19 @@
(function () {
'use strict';
+ const mockData = [
+ {
+ id: 1,
+ first_name: 'Jon',
+ last_name: 'Snow',
+ hidden_field: 'hidden'
+ }
+ ];
+
describe('The xos.helper module', function(){
describe('The xos-smart-table component', () => {
- let spy, scope, isolatedScope, element;
+ var spy, emptySpy, scope, isolatedScope, element;
beforeEach(module('xos.helpers'));
@@ -18,12 +27,15 @@
beforeEach(function(){
module(function($provide){
$provide.service('MockResource', function(){
- this.test = (msg) => console.log(msg);
- this.query = jasmine.createSpy('add')
- .and.returnValue({$promise: cb => {
- console.log('------------------ CB ------------------');
- return cb([]);
- }});
+ this.query = jasmine.createSpy('query').and.callFake(() => {
+ return {$promise: {then: (cb) => cb(mockData)}};
+ });
+ });
+
+ $provide.service('EmptyResource', function(){
+ this.query = jasmine.createSpy('emptyQuery').and.callFake(() => {
+ return {$promise: {then: (cb) => cb([])}};
+ });
});
});
})
@@ -32,11 +44,11 @@
scope = $rootScope.$new();
scope.config = {
- resource: 'MockResource'
+ resource: 'MockResource',
+ hiddenFields: ['hidden_field']
};
spy = MockResource;
- // console.log(MockResource.query.toString(), spy.query.toString());
element = angular.element('<xos-smart-table config="config"></xos-smart-table>');
$compile(element)(scope);
@@ -45,11 +57,40 @@
}));
it('should query elements', () => {
-
- console.log(spy.query.toString());
expect(spy.query).toHaveBeenCalled();
+ expect($(element).find('.alert').parent().parent()).toHaveClass('ng-hide');
});
+ it('should hide hidden fields', () => {
+ expect($(element).find('thead th').length).toEqual(2);
+ expect($(element).find('thead th')[0]).toContainText('First name:');
+ expect($(element).find('thead th')[1]).toContainText('Last name:');
+ });
+
+ describe('when fetching an empty collection', () => {
+ beforeEach(inject(function ($compile, $rootScope, EmptyResource) {
+ scope = $rootScope.$new();
+
+ scope.config = {
+ resource: 'EmptyResource'
+ };
+
+ emptySpy = EmptyResource;
+
+ element = angular.element('<xos-smart-table config="config"></xos-smart-table>');
+ $compile(element)(scope);
+ scope.$digest();
+ isolatedScope = element.isolateScope().vm;
+ }));
+
+ it('should display an alert', () => {
+ expect(emptySpy.query).toHaveBeenCalled();
+ expect($(element).find('.alert').parent().parent()).not.toHaveClass('ng-hide');
+ expect($(element).find('.alert')).toContainText('No data to show');
+ });
+ });
+
+
});
});
})();
\ No newline at end of file
diff --git a/views/ngXosLib/xosHelpers/src/ui_components/smartComponents/smartTable/smartTable.component.js b/views/ngXosLib/xosHelpers/src/ui_components/smartComponents/smartTable/smartTable.component.js
index b8b5915..4a0ecd9 100644
--- a/views/ngXosLib/xosHelpers/src/ui_components/smartComponents/smartTable/smartTable.component.js
+++ b/views/ngXosLib/xosHelpers/src/ui_components/smartComponents/smartTable/smartTable.component.js
@@ -48,6 +48,7 @@
// color: 'red'
// }
// ],
+ classes: 'table table-striped table-bordered table-responsive',
filter: 'field',
order: true,
pagination: {
@@ -55,22 +56,26 @@
}
};
- let Resource = $injector.get(this.config.resource);
+ this.Resource = $injector.get(this.config.resource);
- console.log('query', Resource.query.toString(), Resource.test(`I'm Alive!`));
-
- Resource.query().$promise
+ this.Resource.query().$promise
.then((res) => {
- console.log('Data!!');
+
+ if(!res[0]){
+ return;
+ }
+
let props = Object.keys(res[0]);
_.remove(props, p => {
return p == 'id' || p == 'password' || p == 'validators'
});
- let labels = props.map(p => LabelFormatter.format(p));
+ if(angular.isArray(this.config.hiddenFields)){
+ props = _.difference(props, this.config.hiddenFields)
+ }
- console.log(props, labels);
+ let labels = props.map(p => LabelFormatter.format(p));
props.forEach((p, i) => {
this.tableConfig.columns.push({
@@ -79,9 +84,6 @@
});
});
-
- console.log(this.tableConfig.columns);
-
this.data = res;
})
}