@den4ik92/ng2-smart-table 19.0.3 → 19.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,167 @@
1
+ # Angular Smart Table Component
2
+
3
+ ng2-smart-table component made with :heart: by [Akveo team](http://akveo.com/). Follow us on [Twitter](https://twitter.com/akveo_inc) to get latest news about this component first!
4
+
5
+ ## ⚠ Low Maintenance
6
+ Due to project priority and resource constraints, this project is currently on low maintenance. We recognize that there are a lot of activities around this package. However, we are unable to accommodate the maintenance this project requires.
7
+
8
+ ### Demo
9
+
10
+ <a target="_blank" href="https://akveo.github.io/ng2-smart-table/">Live Demo</a>
11
+
12
+ ![alt tag](projects/demo/src/assets/demo.gif)
13
+
14
+ ## Installation
15
+
16
+ The library is available as npm package, so all you need to do is to run the following command:
17
+
18
+ ```
19
+ npm install --save ng2-smart-table
20
+ ```
21
+
22
+ This command will create a record in your `package.json` file and install the package into the npm modules folder.
23
+
24
+ ## Minimal Setup Example
25
+
26
+ First thing you need to do is to import the ng2-smart-table directives into your component.
27
+
28
+ ```
29
+
30
+ import { Ng2SmartTableModule } from 'ng2-smart-table';
31
+
32
+ ```
33
+
34
+ Then register it by adding to the list of directives of your module:
35
+
36
+ ```
37
+ // ...
38
+
39
+ @NgModule({
40
+ imports: [
41
+ // ...
42
+
43
+ Ng2SmartTableModule,
44
+
45
+ // ...
46
+ ],
47
+ declarations: [ ... ]
48
+ })
49
+ // ...
50
+ ```
51
+
52
+ Now, we need to configure the table and add it into the template. The only <strong>required</strong> setting for the component to start working is a columns configuration.
53
+ Let's register <i>settings</i> property inside of the component where we want to have the table and configure some columns [Settings documentation](https://akveo.github.io/ng2-smart-table/#/documentation):
54
+
55
+ ```
56
+ settings = {
57
+ columns: {
58
+ id: {
59
+ title: 'ID'
60
+ },
61
+ name: {
62
+ title: 'Full Name'
63
+ },
64
+ username: {
65
+ title: 'User Name'
66
+ },
67
+ email: {
68
+ title: 'Email'
69
+ }
70
+ }
71
+ };
72
+ ```
73
+
74
+ Finally let's put the ng2-smart-table component inside of the template:
75
+
76
+ ```
77
+ // ...
78
+
79
+ @Component({
80
+ template: `
81
+ <ng2-smart-table [settings]="settings"></ng2-smart-table>
82
+ `
83
+ })
84
+ // ...
85
+ ```
86
+ At this step you will have a minimal configured table. All functions are available by default and you don't need to configure them anyhow, so now you can add/edit/delete rows, sort or filter the table, etc.
87
+
88
+ Still it seems like something is missing... Right, there is no data in the table by default. To add some, let's create an array property with a list of objects in the component. Please note that object keys are the same as in the columns configuration.
89
+
90
+ ```
91
+ data = [
92
+ {
93
+ id: 1,
94
+ name: "Leanne Graham",
95
+ username: "Bret",
96
+ email: "Sincere@april.biz"
97
+ },
98
+ {
99
+ id: 2,
100
+ name: "Ervin Howell",
101
+ username: "Antonette",
102
+ email: "Shanna@melissa.tv"
103
+ },
104
+
105
+ // ... list of items
106
+
107
+ {
108
+ id: 11,
109
+ name: "Nicholas DuBuque",
110
+ username: "Nicholas.Stanton",
111
+ email: "Rey.Padberg@rosamond.biz"
112
+ }
113
+ ];
114
+ ```
115
+
116
+ And pass the data to the table:
117
+
118
+ ```
119
+ // ...
120
+
121
+ @Component({
122
+ template: `
123
+ <ng2-smart-table [settings]="settings" [source]="data"></ng2-smart-table>
124
+ `
125
+ })
126
+ // ...
127
+ ```
128
+
129
+ Now you have some data in the table.
130
+
131
+ ## Further Documentation
132
+ Installation, customization and other useful articles: https://akveo.github.io/ng2-smart-table/
133
+
134
+ ## UI Bakery
135
+ Try low-code internal tool builder for free
136
+ <a href="https://uibakery.io/?utm_source=github&utm_medium=clicks&utm_campaign=banner"><img src="https://user-images.githubusercontent.com/6151971/125071660-41f84900-e0c2-11eb-882a-0c675eb1e5e3.png"></a>
137
+
138
+ ## How can I support developers?
139
+ - Star our GitHub repo :star:
140
+ - Create pull requests, submit bugs, suggest new features or documentation updates :wrench:
141
+ - Follow us on [Twitter](https://twitter.com/akveo_inc) :feet:
142
+ - Like our page on [Facebook](https://www.facebook.com/akveo/) :thumbsup:
143
+
144
+ ## Can I hire you guys?
145
+ Yes! Visit [our homepage](http://akveo.com/) or simply leave us a note to [contact@akveo.com](mailto:contact@akveo.com). We will be happy to work with you!
146
+
147
+ ## Features
148
+ * Local data source (Server/API LocalDataSource is on its way)
149
+ * Filtering
150
+ * Sorting
151
+ * Pagination
152
+ * Inline Add/Edit/Delete
153
+ * Flexible event model
154
+
155
+ ## License
156
+ [MIT](LICENSE.txt) license.
157
+
158
+ ## Special thanks to our awesome contributors!
159
+
160
+ [<img alt="nnixaa" src="https://avatars0.githubusercontent.com/u/230527?v=3&s=60" width="60">](https://github.com/nnixaa)[<img alt="lexzhukov" src="https://avatars0.githubusercontent.com/u/12192373?v=3&s=60" width="60">](https://github.com/lexzhukov)[<img alt="damnko" src="https://avatars2.githubusercontent.com/u/680205?v=3&s=60" width="60">](https://github.com/damnko)[<img alt="Tibing" src="https://avatars2.githubusercontent.com/u/17410089?v=3&s=60" width="60">](https://github.com/Tibing)[<img alt="Ezeon" src="https://avatars0.githubusercontent.com/u/21973741?v=3&s=60" width="60">](https://github.com/Ezeon)[<img alt="Deilan" src="https://avatars1.githubusercontent.com/u/4777512?v=3&s=60" width="60">](https://github.com/Deilan)[<img alt="hoswey" src="https://avatars0.githubusercontent.com/u/3689445?v=3&s=60" width="60">](https://github.com/hoswey)[<img alt="stacyakveo" src="https://avatars2.githubusercontent.com/u/27723447?v=3&s=60" width="60">](https://github.com/stacyakveo)[<img alt="Akshaymisal5" src="https://avatars3.githubusercontent.com/u/15906551?v=3&s=60" width="60">](https://github.com/Akshaymisal5)[<img alt="geneeblack" src="https://avatars0.githubusercontent.com/u/282525?v=3&s=60" width="60">](https://github.com/geneeblack)[<img alt="vvandoorne" src="https://avatars2.githubusercontent.com/u/26658175?v=3&s=60" width="60">](https://github.com/vvandoorne)[<img alt="ananthhh" src="https://avatars1.githubusercontent.com/u/3583234?v=3&s=60" width="60">](https://github.com/ananthhh)[<img alt="bis-sb" src="https://avatars1.githubusercontent.com/u/22668001?v=3&s=60" width="60">](https://github.com/bis-sb)[<img alt="tadashi-aikawa" src="https://avatars1.githubusercontent.com/u/9500018?v=3&s=60" width="60">](https://github.com/tadashi-aikawa)
161
+
162
+ [<img alt="nureha" src="https://avatars2.githubusercontent.com/u/7064537?v=3&s=60" width="60">](https://github.com/nureha)[<img alt="vlupu10" src="https://avatars1.githubusercontent.com/u/3597512?v=3&s=60" width="60">](https://github.com/vlupu10)[<img alt="zhouhao27" src="https://avatars1.githubusercontent.com/u/8099731?v=3&s=60" width="60">](https://github.com/zhouhao27)[<img alt="hkb1990" src="https://avatars1.githubusercontent.com/u/2637138?v=3&s=60" width="60">](https://github.com/hkb1990)[<img alt="liaosong" src="https://avatars0.githubusercontent.com/u/3927282?v=3&s=60" width="60">](https://github.com/liaosong)[<img alt="ktriek" src="https://avatars2.githubusercontent.com/u/4461059?v=3&s=60" width="60">](https://github.com/ktriek)
163
+
164
+ ### From akveo
165
+
166
+ Enjoy :metal:
167
+ We're always happy to hear your feedback!
@@ -430,20 +430,44 @@ class Deferred {
430
430
  }
431
431
  // getDeepFromObject({result: {data: 1}}, 'result.data', 2); // returns 1
432
432
  function getDeepFromObject(object = {}, name, defaultValue = null) {
433
- const keys = name.split('.');
434
- // clone the object
435
- let level = deepExtend({}, object);
436
- keys.forEach((k) => {
437
- if (level && typeof level[k] !== 'undefined') {
438
- level = level[k];
439
- }
440
- });
441
- return typeof level === 'undefined' ? defaultValue : level;
433
+ try {
434
+ let level = deepExtend({}, object);
435
+ const keys = name.split('.');
436
+ if (keys.length === 1) {
437
+ return level[keys[0]] ?? defaultValue;
438
+ }
439
+ keys.forEach((k) => {
440
+ if (level && typeof level[k] !== 'undefined') {
441
+ level = level[k];
442
+ }
443
+ });
444
+ }
445
+ catch {
446
+ return defaultValue;
447
+ }
442
448
  }
443
449
  function getPageForRowIndex(index, perPage) {
444
450
  // we need to add 1 to convert 0-based index to 1-based page number.
445
451
  return Math.floor(index / perPage) + 1;
446
452
  }
453
+ function cloneArrayOfObject(array) {
454
+ return array.map((obj) => Object.assign({}, obj));
455
+ }
456
+ function setLocalStorage(key, value) {
457
+ localStorage.setItem(key, JSON.stringify(value));
458
+ }
459
+ function getLocalStorage(key) {
460
+ const valueString = localStorage.getItem(key);
461
+ if (!valueString) {
462
+ return null;
463
+ }
464
+ try {
465
+ return JSON.parse(valueString);
466
+ }
467
+ catch {
468
+ return null;
469
+ }
470
+ }
447
471
 
448
472
  class LocalDataSource extends DataSource {
449
473
  constructor(data = []) {
@@ -1100,9 +1124,13 @@ class Grid {
1100
1124
  this.createFormShown = false;
1101
1125
  this.onSelectRowSource = new Subject();
1102
1126
  this.onDeselectRowSource = new Subject();
1127
+ this.currentColumnsSortState = [];
1103
1128
  this.setSettings(settings);
1104
1129
  this.setSource(source);
1105
1130
  }
1131
+ setColumnsSortedEmitter(emitter) {
1132
+ this.columnsSortedEmitter = emitter;
1133
+ }
1106
1134
  detach() {
1107
1135
  if (this.sourceOnChangedSubscription) {
1108
1136
  this.sourceOnChangedSubscription.unsubscribe();
@@ -1130,6 +1158,12 @@ class Grid {
1130
1158
  return this.dataSet.newRow;
1131
1159
  }
1132
1160
  setSettings(settings) {
1161
+ this.updateSettingsAndDataSet(settings);
1162
+ if (this.getSetting('withColumnSort', false)) {
1163
+ this.setColumnsSortState(settings.columns);
1164
+ }
1165
+ }
1166
+ updateSettingsAndDataSet(settings) {
1133
1167
  this.settings = settings;
1134
1168
  this.dataSet = new DataSet([], this.getSetting("columns"));
1135
1169
  if (this.source) {
@@ -1308,6 +1342,72 @@ class Grid {
1308
1342
  getLastRow() {
1309
1343
  return this.dataSet.getLastRow();
1310
1344
  }
1345
+ // ------------------------------- column sort
1346
+ async getSortedTableColumns(newState, columns) {
1347
+ const sortedArray = [];
1348
+ newState.forEach((item2) => {
1349
+ const index = columns.findIndex((item1) => item1.key === item2.key && item1.title === item2.title);
1350
+ if (index > -1) {
1351
+ sortedArray.push({ ...columns[index], hide: !!item2.hide });
1352
+ }
1353
+ });
1354
+ return Promise.resolve(sortedArray);
1355
+ }
1356
+ async applyColumnsSortState(state, emitEvent = true) {
1357
+ this.currentColumnsSortState = this.getMergedColumnStates(state);
1358
+ this.updateSettingsAndDataSet({
1359
+ ...this.settings,
1360
+ columns: await this.getSortedTableColumns(this.currentColumnsSortState, this.settings?.columns),
1361
+ });
1362
+ if (this.columnStateStorageKey) {
1363
+ setLocalStorage(this.columnStateStorageKey, this.currentColumnsSortState);
1364
+ }
1365
+ if (emitEvent) {
1366
+ this.columnsSortedEmitter.emit(this.currentColumnsSortState);
1367
+ }
1368
+ }
1369
+ setColumnsSortState(columns) {
1370
+ const columnsState = this.getColumnsStateFromSettings(columns);
1371
+ if (this.columnStateStorageKey) {
1372
+ const storageState = getLocalStorage(this.columnStateStorageKey);
1373
+ if (!storageState) {
1374
+ this.currentColumnsSortState = columnsState;
1375
+ setLocalStorage(this.columnStateStorageKey, columnsState);
1376
+ return;
1377
+ }
1378
+ const merged = this.getMergedColumnStates(storageState, columnsState);
1379
+ this.applyColumnsSortState(merged, false);
1380
+ return;
1381
+ }
1382
+ this.applyColumnsSortState(columnsState, false);
1383
+ }
1384
+ getColumnsStateFromSettings(columns) {
1385
+ return (columns || this.settings.columns || []).map((column) => ({
1386
+ key: column.key,
1387
+ title: column.title,
1388
+ hide: !!column.hide,
1389
+ sortDisabled: !!column.sortDisabled,
1390
+ }));
1391
+ }
1392
+ getMergedColumnStates(newState, columnsState) {
1393
+ const columnsSettings = columnsState || this.getColumnsStateFromSettings();
1394
+ // merge new columns state with state from storage
1395
+ const filtered = [];
1396
+ newState.forEach((state) => {
1397
+ const fined = columnsSettings.find((column) => column.title === state.title && column.key === state.key);
1398
+ if (fined) {
1399
+ filtered.push({ ...fined, hide: fined.sortDisabled ? fined.hide : state.hide });
1400
+ }
1401
+ });
1402
+ // find new columns witch not exist in storage state
1403
+ const newColumns = columnsSettings.filter((state) => {
1404
+ return !filtered.some((column) => column.title === state.title && column.key === state.key);
1405
+ });
1406
+ return [...filtered, ...newColumns];
1407
+ }
1408
+ get columnStateStorageKey() {
1409
+ return this.settings.columnSortStorageKey;
1410
+ }
1311
1411
  }
1312
1412
 
1313
1413
  class EditCellDefault {
@@ -3285,6 +3385,7 @@ class Ng2SmartTableComponent {
3285
3385
  constructor() {
3286
3386
  this.multiRowSelect = output();
3287
3387
  this.rowClicked = output();
3388
+ this.columnsSorted = output();
3288
3389
  this.delete = output();
3289
3390
  this.edit = output();
3290
3391
  this.editCancel = output();
@@ -3373,7 +3474,6 @@ class Ng2SmartTableComponent {
3373
3474
  this.emitUserSelectRow(row);
3374
3475
  }
3375
3476
  onSelectAllRows() {
3376
- this.grid.dataSet.isAllSelected;
3377
3477
  this.grid.selectAllRows(!this.grid.dataSet.isAllSelected);
3378
3478
  this.emitUserSelectRow(null);
3379
3479
  }
@@ -3389,6 +3489,7 @@ class Ng2SmartTableComponent {
3389
3489
  initGrid() {
3390
3490
  this.source = this.prepareSource();
3391
3491
  this.grid = new Grid(this.source, this.prepareSettings());
3492
+ this.grid.setColumnsSortedEmitter(this.columnsSorted);
3392
3493
  }
3393
3494
  prepareSource() {
3394
3495
  if (this.source instanceof LocalDataSource) {
@@ -3408,7 +3509,7 @@ class Ng2SmartTableComponent {
3408
3509
  });
3409
3510
  }
3410
3511
  static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.0.3", ngImport: i0, type: Ng2SmartTableComponent, deps: [], target: i0.ɵɵFactoryTarget.Component }); }
3411
- static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.0.0", version: "19.0.3", type: Ng2SmartTableComponent, isStandalone: true, selector: "ng2-smart-table", inputs: { source: "source", settings: "settings" }, outputs: { multiRowSelect: "multiRowSelect", rowClicked: "rowClicked", delete: "delete", edit: "edit", editCancel: "editCancel", create: "create", custom: "custom", deleteConfirm: "deleteConfirm", editConfirm: "editConfirm", createConfirm: "createConfirm", rowHover: "rowHover" }, usesOnChanges: true, ngImport: i0, template: "<table [id]=\"tableId\" [class]=\"tableClass\">\n\n @if (!isHideHeader || !isHideSubHeader) {\n <thead ng2-st-thead\n [grid]=\"grid\"\n [source]=\"source\"\n [createConfirm]=\"createConfirm\"\n (create)=\"create.emit($event)\"\n (selectAllRows)=\"onSelectAllRows()\">\n </thead>\n }\n\n <tbody ng2-st-tbody [grid]=\"grid\"\n [source]=\"source\"\n [deleteConfirm]=\"deleteConfirm\"\n [editConfirm]=\"editConfirm\"\n [rowClassFunction]=\"rowClassFunction\"\n (edit)=\"edit.emit($event)\"\n (editCancel)=\"editCancel.emit($event)\"\n (delete)=\"delete.emit($event)\"\n (custom)=\"custom.emit($event)\"\n (userClickedRow)=\"emitUserRowClicked($event)\"\n (multipleSelectRow)=\"multipleSelectRow($event)\">\n </tbody>\n\n</table>\n\n@if (isPagerDisplay) {\n <ng2-smart-table-pager\n [source]=\"source\"\n [perPageSelect]=\"perPageSelect\">\n </ng2-smart-table-pager>\n}\n", styles: [":host{font-size:1rem}:host ::ng-deep *{box-sizing:border-box}:host ::ng-deep button,:host ::ng-deep input,:host ::ng-deep optgroup,:host ::ng-deep select,:host ::ng-deep textarea{color:inherit;font:inherit;margin:0}:host ::ng-deep table{line-height:1.5em;border-collapse:collapse;border-spacing:0;display:table;width:100%;max-width:100%;word-break:normal;word-break:keep-all;overflow:auto}:host ::ng-deep table tr th{font-weight:700}:host ::ng-deep table tr section{font-size:.75em;font-weight:700}:host ::ng-deep table tr td,:host ::ng-deep table tr th{font-size:.875em;margin:0;padding:.5em 1em}:host ::ng-deep a{color:#1e6bb8;text-decoration:none}:host ::ng-deep a:hover{text-decoration:underline}\n"], dependencies: [{ kind: "component", type: Ng2SmartTableTheadComponent, selector: "[ng2-st-thead]", inputs: ["grid", "source", "createConfirm"], outputs: ["sort", "selectAllRows", "create", "filter"] }, { kind: "component", type: Ng2SmartTableTbodyComponent, selector: "[ng2-st-tbody]", inputs: ["grid", "source", "deleteConfirm", "editConfirm", "rowClassFunction"], outputs: ["save", "cancel", "edit", "editCancel", "delete", "custom", "edited", "userSelectRow", "userClickedRow", "editRowSelect", "multipleSelectRow"] }, { kind: "component", type: PagerComponent, selector: "ng2-smart-table-pager", inputs: ["source", "perPageSelect"], outputs: ["changePage"] }] }); }
3512
+ static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.0.0", version: "19.0.3", type: Ng2SmartTableComponent, isStandalone: true, selector: "ng2-smart-table", inputs: { source: "source", settings: "settings" }, outputs: { multiRowSelect: "multiRowSelect", rowClicked: "rowClicked", columnsSorted: "columnsSorted", delete: "delete", edit: "edit", editCancel: "editCancel", create: "create", custom: "custom", deleteConfirm: "deleteConfirm", editConfirm: "editConfirm", createConfirm: "createConfirm", rowHover: "rowHover" }, usesOnChanges: true, ngImport: i0, template: "<table [id]=\"tableId\" [class]=\"tableClass\">\n\n @if (!isHideHeader || !isHideSubHeader) {\n <thead ng2-st-thead\n [grid]=\"grid\"\n [source]=\"source\"\n [createConfirm]=\"createConfirm\"\n (create)=\"create.emit($event)\"\n (selectAllRows)=\"onSelectAllRows()\">\n </thead>\n }\n\n <tbody ng2-st-tbody [grid]=\"grid\"\n [source]=\"source\"\n [deleteConfirm]=\"deleteConfirm\"\n [editConfirm]=\"editConfirm\"\n [rowClassFunction]=\"rowClassFunction\"\n (edit)=\"edit.emit($event)\"\n (editCancel)=\"editCancel.emit($event)\"\n (delete)=\"delete.emit($event)\"\n (custom)=\"custom.emit($event)\"\n (userClickedRow)=\"emitUserRowClicked($event)\"\n (multipleSelectRow)=\"multipleSelectRow($event)\">\n </tbody>\n\n</table>\n\n@if (isPagerDisplay) {\n <ng2-smart-table-pager\n [source]=\"source\"\n [perPageSelect]=\"perPageSelect\">\n </ng2-smart-table-pager>\n}\n", styles: [":host{font-size:1rem}:host ::ng-deep *{box-sizing:border-box}:host ::ng-deep button,:host ::ng-deep input,:host ::ng-deep optgroup,:host ::ng-deep select,:host ::ng-deep textarea{color:inherit;font:inherit;margin:0}:host ::ng-deep table{line-height:1.5em;border-collapse:collapse;border-spacing:0;display:table;width:100%;max-width:100%;word-break:normal;word-break:keep-all;overflow:auto}:host ::ng-deep table tr th{font-weight:700}:host ::ng-deep table tr section{font-size:.75em;font-weight:700}:host ::ng-deep table tr td,:host ::ng-deep table tr th{font-size:.875em;margin:0;padding:.5em 1em}:host ::ng-deep a{color:#1e6bb8;text-decoration:none}:host ::ng-deep a:hover{text-decoration:underline}\n"], dependencies: [{ kind: "component", type: Ng2SmartTableTheadComponent, selector: "[ng2-st-thead]", inputs: ["grid", "source", "createConfirm"], outputs: ["sort", "selectAllRows", "create", "filter"] }, { kind: "component", type: Ng2SmartTableTbodyComponent, selector: "[ng2-st-tbody]", inputs: ["grid", "source", "deleteConfirm", "editConfirm", "rowClassFunction"], outputs: ["save", "cancel", "edit", "editCancel", "delete", "custom", "edited", "userSelectRow", "userClickedRow", "editRowSelect", "multipleSelectRow"] }, { kind: "component", type: PagerComponent, selector: "ng2-smart-table-pager", inputs: ["source", "perPageSelect"], outputs: ["changePage"] }] }); }
3412
3513
  }
3413
3514
  i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.0.3", ngImport: i0, type: Ng2SmartTableComponent, decorators: [{
3414
3515
  type: Component,