Skip to content

Commit 8f5461f

Browse files
committed
feat(NgTableParams): nested paramater values never undefined
This is part of supporting typescript strictNullChecks compiler option. BREAKING CHANGES `NgTableParams.parameters`: any param value supplied as undefined will now be ignored Instead of `undefined` you will need to supply a suitable value as described below: * `count`, `page` - `0` * `filter`, `group`, `sorting` - an empty object to remove existing values
1 parent 3ca6852 commit 8f5461f

File tree

5 files changed

+259
-91
lines changed

5 files changed

+259
-91
lines changed

src/core/ngTableDefaults.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* @license New BSD License <http://creativecommons.org/licenses/BSD/>
77
*/
88

9-
import { ParamValues } from './ngTableParams';
9+
import { ParamValuesPartial } from './ngTableParams';
1010
import { SettingsPartial } from './ngTableSettings';
1111

1212

@@ -15,7 +15,7 @@ import { SettingsPartial } from './ngTableSettings';
1515
* an instance of `NgTableParams`
1616
*/
1717
export interface Defaults {
18-
params?: ParamValues<any>;
18+
params?: ParamValuesPartial<any>;
1919
settings?: SettingsPartial<any>
2020
}
2121

src/core/ngTableParams.ts

Lines changed: 47 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import * as ng1 from 'angular';
1010
import { ILogService, IPromise, IQService } from 'angular';
1111
import { convertSortToOrderBy, isGroupingFun } from './util';
12+
import { assignPartialDeep } from '../shared';
1213
import { Defaults } from './ngTableDefaults'
1314
import { NgTableEventsChannel } from './ngTableEventsChannel'
1415
import { SettingsPartial, Settings } from './ngTableSettings'
@@ -25,31 +26,34 @@ export interface InternalTableParams<T> extends NgTableParams<T> {
2526
isNullInstance: boolean
2627
}
2728

29+
30+
export type ParamValuesPartial<T> = Partial<ParamValues<T>>;
31+
2832
/**
2933
* The runtime values for {@link NgTableParams} that determine the set of data rows and
3034
* how they are to be displayed in a table
3135
*/
32-
export interface ParamValues<T> {
36+
export class ParamValues<T> {
3337
/**
3438
* The index of the "slice" of data rows, starting at 1, to be displayed by the table.
3539
*/
36-
page?: number;
40+
page = 1;
3741
/**
3842
* The number of data rows per page
3943
*/
40-
count?: number;
44+
count = 10;
4145
/**
4246
* The filter that should be applied to restrict the set of data rows
4347
*/
44-
filter?: FilterValues;
48+
filter: FilterValues = {};
4549
/**
4650
* The sort order that should be applied to the data rows.
4751
*/
48-
sorting?: SortingValues;
52+
sorting: SortingValues = {};
4953
/**
5054
* The grouping that should be applied to the data rows
5155
*/
52-
group?: string | Grouping<T>;
56+
group: string | Grouping<T> = {};
5357
}
5458

5559

@@ -85,17 +89,11 @@ export class NgTableParams<T> {
8589
private ngTableDefaults: Defaults
8690
private ngTableEventsChannel: NgTableEventsChannel;
8791
private prevParamsMemento: Memento<T>;
88-
private _params: ParamValues<T> = {
89-
page: 1,
90-
count: 10,
91-
filter: {},
92-
sorting: {},
93-
group: {}
94-
};
92+
private _params = new ParamValues();
9593
private _settings = this.defaultSettings;
9694
private $q: IQService;
9795
private $log: ILogService
98-
constructor(baseParameters?: ParamValues<T> | boolean, baseSettings?: SettingsPartial<T>) {
96+
constructor(baseParameters?: ParamValuesPartial<T> | boolean, baseSettings?: SettingsPartial<T>) {
9997

10098
// the ngTableController "needs" to create a dummy/null instance and it's important to know whether an instance
10199
// is one of these
@@ -115,7 +113,7 @@ export class NgTableParams<T> {
115113
}
116114
})();
117115

118-
ng1.extend(this._params, this.ngTableDefaults.params);
116+
assignPartialDeep(this._params, this.ngTableDefaults.params);
119117

120118
this.settings(baseSettings);
121119
this.parameters(baseParameters, true);
@@ -268,7 +266,7 @@ export class NgTableParams<T> {
268266
return this._params.group;
269267
}
270268

271-
const newParameters: ParamValues<T> = {
269+
const newParameters: ParamValuesPartial<T> = {
272270
page: 1
273271
};
274272
if (isGroupingFun(group) && sortDirection !== undefined) {
@@ -382,39 +380,45 @@ export class NgTableParams<T> {
382380
/**
383381
* Set new parameters
384382
*/
385-
parameters(newParameters?: ParamValues<T> | { [name: string]: string }, parseParamsFromUrl?: boolean): this
386-
parameters(newParameters?: ParamValues<T> | { [name: string]: string }, parseParamsFromUrl?: boolean): ParamValues<T> | this {
383+
parameters(newParameters?: ParamValuesPartial<T> | { [name: string]: string }, parseParamsFromUrl?: boolean): this
384+
parameters(newParameters?: ParamValuesPartial<T> | { [name: string]: string }, parseParamsFromUrl?: boolean): ParamValues<T> | this {
385+
if (newParameters === undefined) {
386+
return this._params;
387+
}
388+
389+
// todo: move parsing of url like parameters into a seperate method
390+
387391
parseParamsFromUrl = parseParamsFromUrl || false;
388-
if (newParameters !== undefined) {
389-
for (const key in newParameters) {
390-
let value = newParameters[key];
391-
if (parseParamsFromUrl && key.indexOf('[') >= 0) {
392-
const keys = key.split(/\[(.*)\]/).reverse()
393-
let lastKey = '';
394-
for (let i = 0, len = keys.length; i < len; i++) {
395-
const name = keys[i];
396-
if (name !== '') {
397-
const v = value;
398-
value = {};
399-
value[lastKey = name] = (isNumber(v) ? parseFloat(v) : v);
400-
}
401-
}
402-
if (lastKey === 'sorting') {
403-
this._params[lastKey] = {};
392+
for (const key in newParameters) {
393+
let value = newParameters[key];
394+
if (parseParamsFromUrl && key.indexOf('[') >= 0) {
395+
const keys = key.split(/\[(.*)\]/).reverse()
396+
let lastKey = '';
397+
for (let i = 0, len = keys.length; i < len; i++) {
398+
const name = keys[i];
399+
if (name !== '') {
400+
const v = value;
401+
value = {};
402+
value[lastKey = name] = (isNumber(v) ? parseFloat(v) : v);
404403
}
405-
this._params[lastKey] = ng1.extend(this._params[lastKey] || {}, value[lastKey]);
404+
}
405+
if (lastKey === 'sorting') {
406+
this._params[lastKey] = {};
407+
}
408+
this._params[lastKey] = ng1.extend(this._params[lastKey] || {}, value[lastKey]);
409+
} else {
410+
if (newParameters[key] === undefined) {
411+
// skip
412+
}
413+
else if (key === 'group') {
414+
this._params[key] = this.parseGroup(newParameters[key]);
406415
} else {
407-
if (key === 'group') {
408-
this._params[key] = this.parseGroup(newParameters[key]);
409-
} else {
410-
this._params[key] = (isNumber(newParameters[key]) ? parseFloat(newParameters[key]) : newParameters[key]);
411-
}
416+
this._params[key] = (isNumber(newParameters[key]) ? parseFloat(newParameters[key]) : newParameters[key]);
412417
}
413418
}
414-
this.log('ngTable: set parameters', this._params);
415-
return this;
416419
}
417-
return this._params;
420+
this.log('ngTable: set parameters', this._params);
421+
return this;
418422
}
419423
/**
420424
* Trigger a reload of the data rows

test/specs/settings.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ describe('Settings', () => {
1212
beforeAll(() => expect(ngTableCoreModule).toBeDefined());
1313

1414
let defaultOverrides: SettingsPartial<any>;
15-
let standardSettings: SettingsPartial<any>;
15+
let standardSettings: Settings<any>;
1616

1717
beforeEach(ng1.mock.module('ngTable-core'));
1818

@@ -103,7 +103,7 @@ describe('Settings', () => {
103103
filterDelay: 20
104104
}
105105
};
106-
const originalNewSettings = { ...newSettings, filterOptions: { ... newSettings.filterOptions } };
106+
const originalNewSettings = { ...newSettings, filterOptions: { ...newSettings.filterOptions } };
107107

108108
const actual = Settings.merge(allSettings, newSettings);
109109
expect(actual).not.toBe(allSettings);

test/specs/table.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { IAugmentedJQuery, ICompileService, IQService, IPromise, IScope, ITimeoutService } from 'angular';
22
import * as ng1 from 'angular';
33
import { ngTableModule } from '../../index';
4-
import { NgTableParams, ParamValues, SettingsPartial, SortingValues } from '../../src/core';
4+
import { NgTableParams, ParamValuesPartial, SettingsPartial, SortingValues } from '../../src/core';
55
import { ColumnDef, FilterTemplateDef, FilterTemplateDefMap, SelectOption } from '../../src/browser'
66

77
describe('ng-table', () => {
@@ -72,10 +72,10 @@ describe('ng-table', () => {
7272
scope.model = {};
7373
}));
7474

75-
function createNgTableParams<T>(initialParams?: ParamValues<T>, settings?: SettingsPartial<T>): NgTableParams<T>;
75+
function createNgTableParams<T>(initialParams?: ParamValuesPartial<T>, settings?: SettingsPartial<T>): NgTableParams<T>;
7676
function createNgTableParams<T>(settings?: SettingsPartial<T>): NgTableParams<T>;
7777
function createNgTableParams<T>(settings?: any): NgTableParams<T> {
78-
let initialParams: ParamValues<T>;
78+
let initialParams: ParamValuesPartial<T>;
7979
if (arguments.length === 2) {
8080
initialParams = arguments[0];
8181
settings = arguments[1];

0 commit comments

Comments
 (0)