Skip to content

Commit cf792e1

Browse files
committed
feat: save sorting
1 parent f752027 commit cf792e1

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

src/columnmanager.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ export default class ColumnManager {
123123
});
124124
const $cell = $.closest('.dt-cell', e.target);
125125
const { colIndex } = $.data($cell);
126+
debugger;
126127
this._dropdownActiveColIndex = colIndex;
127128
}
128129

@@ -256,6 +257,7 @@ export default class ColumnManager {
256257
animation: 150
257258
});
258259
}
260+
259261

260262
sortColumn(colIndex, nextSortOrder) {
261263
this.instance.freeze();
@@ -267,8 +269,21 @@ export default class ColumnManager {
267269
.then(() => this.instance.unfreeze())
268270
.then(() => {
269271
this.fireEvent('onSortColumn', this.getColumn(colIndex));
272+
this.saveSorting(colIndex, nextSortOrder)
270273
});
271274
}
275+
276+
saveSorting(colIndex){
277+
let currentColumn = this.getColumn(colIndex);
278+
let saveSorting = {
279+
[currentColumn.name]: {
280+
colIndex: colIndex,
281+
sortOrder: currentColumn.sortOrder
282+
}
283+
}
284+
localStorage.setItem('savedSorting', JSON.stringify(saveSorting));
285+
}
286+
272287

273288
removeColumn(colIndex) {
274289
const removedCol = this.getColumn(colIndex);
@@ -368,6 +383,14 @@ export default class ColumnManager {
368383
}
369384
}
370385

386+
applySavedSortOrder(){
387+
let sortingConfig = JSON.parse(localStorage.getItem("savedSorting"))
388+
const columnsToSort = Object.values(sortingConfig)
389+
for(let column of columnsToSort){
390+
this.sortColumn(column.colIndex, column.sortOrder);
391+
}
392+
}
393+
371394
sortRows(colIndex, sortOrder) {
372395
return this.datamanager.sortRows(colIndex, sortOrder);
373396
}

src/datatable.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class DataTable {
4040

4141
if (this.options.data) {
4242
this.refresh();
43+
this.columnmanager.applySavedSortOrder();
4344
this.columnmanager.applyDefaultSortOrder();
4445
}
4546
}

src/defaults.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export default function getDefaultOptions(instance) {
2323
label: instance.translate('Reset sorting'),
2424
action: function (column) {
2525
this.sortColumn(column.colIndex, 'none');
26+
localStorage.set("savedSort", null)
2627
}
2728
},
2829
{

0 commit comments

Comments
 (0)