Skip to content

Commit e532a4f

Browse files
committed
fix(ngTableColumn): setting column attribute fails
1 parent a237e70 commit e532a4f

File tree

3 files changed

+78
-11
lines changed

3 files changed

+78
-11
lines changed

src/browser/ngTableColumn.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export class NgTableColumn<TCol extends IColumnDef | IDynamicTableColDef> {
6161
const getterFn = extendedCol[prop];
6262
extendedCol[prop] = function () {
6363
if (arguments.length === 1 && !isScopeLike(arguments[0])) {
64-
getterFn.assign(null, arguments[0]);
64+
getterFn.assign(defaultScope, arguments[0]);
6565
} else {
6666
const scope = arguments[0] || defaultScope;
6767
const context = Object.create(scope);
@@ -74,6 +74,20 @@ export class NgTableColumn<TCol extends IColumnDef | IDynamicTableColDef> {
7474
};
7575
if (getterFn.assign) {
7676
extendedCol[prop].assign = getterFn.assign;
77+
} else {
78+
const wrappedGetterFn = extendedCol[prop];
79+
let localValue: any;
80+
const getterSetter = function getterSetter(/*[value] || [$scope, locals]*/) {
81+
if (arguments.length === 1 && !isScopeLike(arguments[0])) {
82+
(getterSetter as any).assign(null, arguments[0]);
83+
} else {
84+
return localValue != undefined ? localValue : wrappedGetterFn.apply(extendedCol, arguments);
85+
}
86+
};
87+
(getterSetter as any).assign = function ($scope: IScope, value: any) {
88+
localValue = value;
89+
};
90+
extendedCol[prop] = getterSetter;
7791
}
7892
}
7993
return extendedCol as IColumnDef;

src/browser/public-interfaces.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export type ColumnFieldContext = IScope & {
1313
*/
1414
export interface IColumnField<T> {
1515
(context?: ColumnFieldContext): T;
16+
(value: T): void;
1617
assign($scope: IScope, value: T): void;
1718
}
1819

test/tableSpec.spec.ts

Lines changed: 62 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ describe('ng-table', () => {
1919

2020
interface ICustomizedScope extends IScope {
2121
$$childHead: INgTableChildScope;
22+
model: {
23+
exportedCols?: IColumnDef[];
24+
};
2225
ageFilter: IFilterTemplateDefMap;
2326
ageExpandedFilter: { [name: string]: IFilterTemplateDef };
2427
ageTitle: string | { (): string };
@@ -66,6 +69,7 @@ describe('ng-table', () => {
6669
beforeEach(inject(($rootScope: IScope, _$compile_: ICompileService) => {
6770
scope = $rootScope.$new(true) as ICustomizedScope;
6871
$compile = _$compile_;
72+
scope.model = {};
6973
}));
7074

7175
function createNgTableParams<T>(initialParams?: IParamValues<T>, settings?: ISettings<T>): NgTableParams<T>;
@@ -399,7 +403,7 @@ describe('ng-table', () => {
399403
// given
400404
const { params, tableElm } = createTable(tableWithSortableAgeColumnTpl);
401405
(params.settings().getData as jasmine.Spy).calls.reset();
402-
const columnHeader =
406+
const columnHeader =
403407
tableElm[0].querySelector('.ng-table-sort-header > th') as HTMLTableHeaderCellElement;
404408

405409
// when
@@ -809,10 +813,10 @@ describe('ng-table', () => {
809813

810814
describe('$columns', () => {
811815
let tableElm: IAugmentedJQuery,
812-
tp: NgTableParams<IPerson>;
816+
params: NgTableParams<IPerson>;
813817
beforeEach(() => {
814818
const html = `<div>
815-
<table ng-table="tableParams">
819+
<table ng-table="tableParams" ng-table-columns-binding="model.exportedCols">
816820
<tr ng-repeat="user in $data">
817821
<td title="ageTitle" ng-if="isAgeVisible" filter="ageFilter">{{user.age}}</td>
818822
<td title="'Name'" groupable="'name'" sortable="'name'">{{user.name}}</td>
@@ -824,12 +828,12 @@ describe('ng-table', () => {
824828
};
825829
scope.isAgeVisible = true;
826830
scope.ageTitle = 'Age';
827-
({ params: tp, tableElm } = createTable(html));
831+
({ params, tableElm } = createTable(html));
828832
});
829833

830834
it('should make $columns available on the scope created for ng-table', () => {
831835
// check that the scope is indeed the one created for out NgTableParams
832-
expect(scope.$$childHead.params).toBe(tp);
836+
expect(scope.$$childHead.params).toBe(params);
833837

834838
expect(scope.$$childHead.$columns).toBeDefined();
835839
});
@@ -838,12 +842,16 @@ describe('ng-table', () => {
838842
expect(scope['$columns']).toBeUndefined();
839843
});
840844

845+
it('ng-table-columns-binding should make $columns externally available', () => {
846+
expect(scope.model.exportedCols).toBeDefined();
847+
});
848+
841849
it('$scolumns should contain a column definition for each `td` element', () => {
842-
expect(scope.$$childHead.$columns.length).toBe(2);
850+
expect(scope.model.exportedCols.length).toBe(2);
843851
});
844852

845853
it('each column definition should have getters for each column attribute', () => {
846-
const ageCol = scope.$$childHead.$columns[0];
854+
const ageCol = scope.model.exportedCols[0];
847855
expect(ageCol.title()).toBe('Age');
848856
expect(ageCol.show()).toBe(true);
849857
expect(ageCol.filter()).toBe(scope.ageFilter);
@@ -855,7 +863,7 @@ describe('ng-table', () => {
855863
expect(ageCol.sortable()).toBe(false);
856864
expect(ageCol.titleAlt()).toBe('');
857865

858-
const nameCol = scope.$$childHead.$columns[1];
866+
const nameCol = scope.model.exportedCols[1];
859867
expect(nameCol.title()).toBe('Name');
860868
expect(nameCol.show()).toBe(true);
861869
expect(nameCol.filter()).toBe(false);
@@ -869,7 +877,7 @@ describe('ng-table', () => {
869877
});
870878

871879
it('each column attribute should be assignable', () => {
872-
const ageCol = scope.$$childHead.$columns[0];
880+
const ageCol = scope.model.exportedCols[0];
873881

874882
ageCol.title.assign(scope.$$childHead, 'Age of person');
875883
expect(ageCol.title()).toBe('Age of person');
@@ -903,14 +911,58 @@ describe('ng-table', () => {
903911
expect(ageCol.titleAlt()).toBe('really');
904912

905913

906-
const nameCol = scope.$$childHead.$columns[1];
914+
const nameCol = scope.model.exportedCols[1];
907915

908916
nameCol.groupable.assign(scope.$$childHead, false);
909917
expect(nameCol.groupable()).toBe(false);
910918

911919
nameCol.sortable.assign(scope.$$childHead, false);
912920
expect(nameCol.sortable()).toBe(false);
913921
});
922+
923+
it('each column attribute should be settable', () => {
924+
const ageCol = scope.model.exportedCols[0];
925+
926+
ageCol.title('Age of person');
927+
expect(ageCol.title()).toBe('Age of person');
928+
expect(scope.ageTitle).toBe('Age of person');
929+
930+
ageCol.show(false);
931+
expect(ageCol.show()).toBe(false);
932+
expect(scope.isAgeVisible).toBe(false);
933+
934+
const newFilter: IFilterTemplateDefMap = { age: 'select' };
935+
ageCol.filter(newFilter);
936+
expect(ageCol.filter()).toBe(newFilter);
937+
expect(scope.ageFilter).toBe(newFilter);
938+
939+
ageCol.class('amazing');
940+
expect(ageCol.class()).toBe('amazing');
941+
942+
ageCol.groupable('age');
943+
expect(ageCol.groupable()).toBe('age');
944+
945+
ageCol.headerTemplateURL('some.html');
946+
expect(ageCol.headerTemplateURL()).toBe('some.html');
947+
948+
ageCol.headerTitle('wow');
949+
expect(ageCol.headerTitle()).toBe('wow');
950+
951+
ageCol.sortable('incredible');
952+
expect(ageCol.sortable()).toBe('incredible');
953+
954+
ageCol.titleAlt('really');
955+
expect(ageCol.titleAlt()).toBe('really');
956+
957+
958+
const nameCol = scope.model.exportedCols[1];
959+
960+
nameCol.groupable(false);
961+
expect(nameCol.groupable()).toBe(false);
962+
963+
nameCol.sortable(false);
964+
expect(nameCol.sortable()).toBe(false);
965+
});
914966
});
915967

916968

0 commit comments

Comments
 (0)