Skip to content

Commit 5c5aeed

Browse files
authored
Merge pull request #208 from sokumon/save-sorting
feat: save sorting
2 parents f752027 + b2e4127 commit 5c5aeed

File tree

4 files changed

+92
-10
lines changed

4 files changed

+92
-10
lines changed

src/cellmanager.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,6 @@ export default class CellManager {
333333

334334
_selectArea($cell1, $cell2) {
335335
if ($cell1 === $cell2) return false;
336-
337336
const cells = this.getCellsInRange($cell1, $cell2);
338337
if (!cells) return false;
339338

@@ -360,9 +359,17 @@ export default class CellManager {
360359
const cell2 = $.data($cell2);
361360

362361
colIndex1 = +cell1.colIndex;
363-
rowIndex1 = +cell1.rowIndex;
364362
colIndex2 = +cell2.colIndex;
365-
rowIndex2 = +cell2.rowIndex;
363+
364+
if (this.columnmanager.sortState) {
365+
this.sortedColumn = true;
366+
rowIndex1 = this.datamanager.rowViewOrder.indexOf(parseInt(cell1.rowIndex, 10));
367+
rowIndex2 = this.datamanager.rowViewOrder.indexOf(parseInt(cell2.rowIndex, 10));
368+
} else {
369+
rowIndex1 = +cell1.rowIndex;
370+
rowIndex2 = +cell2.rowIndex;
371+
}
372+
366373
}
367374

368375
if (rowIndex1 > rowIndex2) {
@@ -394,7 +401,11 @@ export default class CellManager {
394401
}
395402
colIndex = colIndex1;
396403
});
397-
404+
if (this.columnmanager.sortState) {
405+
cells.forEach(selectedCells => {
406+
selectedCells[1] = this.datamanager.rowViewOrder[selectedCells[1]];
407+
});
408+
}
398409
return cells;
399410
}
400411

src/columnmanager.js

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -267,9 +267,29 @@ export default class ColumnManager {
267267
.then(() => this.instance.unfreeze())
268268
.then(() => {
269269
this.fireEvent('onSortColumn', this.getColumn(colIndex));
270+
this.setSortState();
270271
});
271272
}
272273

274+
saveSorting(colIndex) {
275+
let currentColumn = this.getColumn(colIndex);
276+
let saveSorting = {
277+
[currentColumn.name]: {
278+
colIndex: colIndex,
279+
sortOrder: currentColumn.sortOrder
280+
}
281+
};
282+
this.sortingKey = this.options.sortingKey ? `${this.options.sortingKey}::sortedColumns` : 'sortedColumns' ;
283+
localStorage.setItem(this.sortingKey, JSON.stringify(saveSorting));
284+
}
285+
setSortState(sortOrder) {
286+
if (sortOrder === 'none') {
287+
this.sortState = false;
288+
} else {
289+
this.sortState = true;
290+
}
291+
}
292+
273293
removeColumn(colIndex) {
274294
const removedCol = this.getColumn(colIndex);
275295
this.instance.freeze();
@@ -368,6 +388,19 @@ export default class ColumnManager {
368388
}
369389
}
370390

391+
applySavedSortOrder() {
392+
393+
let key = this.options.sortingKey ? `${this.options.sortingKey}::sortedColumns` : 'sortedColumns' ;
394+
let sortingConfig = JSON.parse(localStorage.getItem(key));
395+
if (sortingConfig) {
396+
const columnsToSort = Object.values(sortingConfig);
397+
for (let column of columnsToSort) {
398+
this.sortColumn(column.colIndex, column.sortOrder);
399+
this.sortState = true;
400+
}
401+
}
402+
}
403+
371404
sortRows(colIndex, sortOrder) {
372405
return this.datamanager.sortRows(colIndex, sortOrder);
373406
}
@@ -443,13 +476,21 @@ export default class ColumnManager {
443476

444477
getDropdownListHTML() {
445478
const { headerDropdown: dropdownItems } = this.options;
446-
447479
return `
448-
<div class="dt-dropdown__list">
449-
${dropdownItems.map((d, i) => `
450-
<div class="dt-dropdown__list-item" data-index="${i}">${d.label}</div>
451-
`).join('')}
480+
<div class="dt-dropdown__list">
481+
${dropdownItems.map((d, i) => `
482+
<div
483+
class="dt-dropdown__list-item${d.display ? ' dt-hidden' : ''}"
484+
data-index="${i}"
485+
>
486+
${d.label}
452487
</div>
453-
`;
488+
`).join('')}
489+
</div>
490+
`;
491+
}
492+
493+
toggleDropdownItem(index) {
494+
$('.dt-dropdown__list', this.instance.dropdownContainer).children[index].classList.toggle('dt-hidden');
454495
}
455496
}

src/datatable.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ class DataTable {
4141
if (this.options.data) {
4242
this.refresh();
4343
this.columnmanager.applyDefaultSortOrder();
44+
if (this.options.saveSorting) {
45+
this.setupSaveSorting();
46+
this.columnmanager.applySavedSortOrder();
47+
}
4448
}
4549
}
4650

@@ -210,6 +214,9 @@ class DataTable {
210214
sortColumn(colIndex, sortOrder) {
211215
this.columnmanager.sortColumn(colIndex, sortOrder);
212216
}
217+
saveSorting(colIndex, nextSortOrder) {
218+
this.columnmanager.saveSorting(colIndex, nextSortOrder);
219+
}
213220

214221
removeColumn(colIndex) {
215222
this.columnmanager.removeColumn(colIndex);
@@ -263,6 +270,25 @@ class DataTable {
263270
translate(str, args) {
264271
return this.translationManager.translate(str, args);
265272
}
273+
setupSaveSorting() {
274+
// add options in default headerdropdown
275+
let action = {
276+
label: this.translate('Save Sorting'),
277+
action: function (column) {
278+
this.saveSorting(column.colIndex, column.sotOrder);
279+
},
280+
display: 'hidden'
281+
};
282+
this.options.headerDropdown.push(action);
283+
this.columnmanager.bindDropdown();
284+
// add events for onSortColumn
285+
this.on('onSortColumn', function (column) {
286+
this.columnmanager.toggleDropdownItem(4);
287+
if (column.sortOrder === 'none') {
288+
localStorage.removeItem(this.columnmanager.sortingKey);
289+
}
290+
});
291+
}
266292
}
267293

268294
DataTable.instances = 0;

src/style.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,10 @@
295295
left: -999em;
296296
}
297297

298+
.dt-hidden{
299+
display: none;
300+
}
301+
298302
body.dt-resize {
299303
cursor: col-resize;
300304
}

0 commit comments

Comments
 (0)