diff --git a/src/cellmanager.js b/src/cellmanager.js index c1d1a8f..8275bfd 100644 --- a/src/cellmanager.js +++ b/src/cellmanager.js @@ -333,7 +333,6 @@ export default class CellManager { _selectArea($cell1, $cell2) { if ($cell1 === $cell2) return false; - const cells = this.getCellsInRange($cell1, $cell2); if (!cells) return false; @@ -360,9 +359,17 @@ export default class CellManager { const cell2 = $.data($cell2); colIndex1 = +cell1.colIndex; - rowIndex1 = +cell1.rowIndex; colIndex2 = +cell2.colIndex; - rowIndex2 = +cell2.rowIndex; + + if (this.columnmanager.sortState) { + this.sortedColumn = true; + rowIndex1 = this.datamanager.rowViewOrder.indexOf(parseInt(cell1.rowIndex, 10)); + rowIndex2 = this.datamanager.rowViewOrder.indexOf(parseInt(cell2.rowIndex, 10)); + } else { + rowIndex1 = +cell1.rowIndex; + rowIndex2 = +cell2.rowIndex; + } + } if (rowIndex1 > rowIndex2) { @@ -394,7 +401,11 @@ export default class CellManager { } colIndex = colIndex1; }); - + if (this.columnmanager.sortState) { + cells.forEach(selectedCells => { + selectedCells[1] = this.datamanager.rowViewOrder[selectedCells[1]]; + }); + } return cells; } diff --git a/src/columnmanager.js b/src/columnmanager.js index fabfbe7..93344da 100644 --- a/src/columnmanager.js +++ b/src/columnmanager.js @@ -267,9 +267,29 @@ export default class ColumnManager { .then(() => this.instance.unfreeze()) .then(() => { this.fireEvent('onSortColumn', this.getColumn(colIndex)); + this.setSortState(); }); } + saveSorting(colIndex) { + let currentColumn = this.getColumn(colIndex); + let saveSorting = { + [currentColumn.name]: { + colIndex: colIndex, + sortOrder: currentColumn.sortOrder + } + }; + this.sortingKey = this.options.sortingKey ? `${this.options.sortingKey}::sortedColumns` : 'sortedColumns' ; + localStorage.setItem(this.sortingKey, JSON.stringify(saveSorting)); + } + setSortState(sortOrder) { + if (sortOrder === 'none') { + this.sortState = false; + } else { + this.sortState = true; + } + } + removeColumn(colIndex) { const removedCol = this.getColumn(colIndex); this.instance.freeze(); @@ -368,6 +388,19 @@ export default class ColumnManager { } } + applySavedSortOrder() { + + let key = this.options.sortingKey ? `${this.options.sortingKey}::sortedColumns` : 'sortedColumns' ; + let sortingConfig = JSON.parse(localStorage.getItem(key)); + if (sortingConfig) { + const columnsToSort = Object.values(sortingConfig); + for (let column of columnsToSort) { + this.sortColumn(column.colIndex, column.sortOrder); + this.sortState = true; + } + } + } + sortRows(colIndex, sortOrder) { return this.datamanager.sortRows(colIndex, sortOrder); } @@ -443,13 +476,21 @@ export default class ColumnManager { getDropdownListHTML() { const { headerDropdown: dropdownItems } = this.options; - return ` -