Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions src/cellmanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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) {
Expand Down Expand Up @@ -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;
}

Expand Down
53 changes: 47 additions & 6 deletions src/columnmanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -443,13 +476,21 @@ export default class ColumnManager {

getDropdownListHTML() {
const { headerDropdown: dropdownItems } = this.options;

return `
<div class="dt-dropdown__list">
${dropdownItems.map((d, i) => `
<div class="dt-dropdown__list-item" data-index="${i}">${d.label}</div>
`).join('')}
<div class="dt-dropdown__list">
${dropdownItems.map((d, i) => `
<div
class="dt-dropdown__list-item${d.display ? ' dt-hidden' : ''}"
data-index="${i}"
>
${d.label}
</div>
`;
`).join('')}
</div>
`;
}

toggleDropdownItem(index) {
$('.dt-dropdown__list', this.instance.dropdownContainer).children[index].classList.toggle('dt-hidden');
}
}
26 changes: 26 additions & 0 deletions src/datatable.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ class DataTable {
if (this.options.data) {
this.refresh();
this.columnmanager.applyDefaultSortOrder();
if (this.options.saveSorting) {
this.setupSaveSorting();
this.columnmanager.applySavedSortOrder();
}
}
}

Expand Down Expand Up @@ -210,6 +214,9 @@ class DataTable {
sortColumn(colIndex, sortOrder) {
this.columnmanager.sortColumn(colIndex, sortOrder);
}
saveSorting(colIndex, nextSortOrder) {
this.columnmanager.saveSorting(colIndex, nextSortOrder);
}

removeColumn(colIndex) {
this.columnmanager.removeColumn(colIndex);
Expand Down Expand Up @@ -263,6 +270,25 @@ class DataTable {
translate(str, args) {
return this.translationManager.translate(str, args);
}
setupSaveSorting() {
// add options in default headerdropdown
let action = {
label: this.translate('Save Sorting'),
action: function (column) {
this.saveSorting(column.colIndex, column.sotOrder);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
this.saveSorting(column.colIndex, column.sotOrder);
this.saveSorting(column.colIndex, column.sortOrder);

@sokumon Spelling mistake?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes thanks will fix it tomorrow

},
display: 'hidden'
};
this.options.headerDropdown.push(action);
this.columnmanager.bindDropdown();
// add events for onSortColumn
this.on('onSortColumn', function (column) {
this.columnmanager.toggleDropdownItem(4);
if (column.sortOrder === 'none') {
localStorage.removeItem(this.columnmanager.sortingKey);
}
});
}
}

DataTable.instances = 0;
Expand Down
4 changes: 4 additions & 0 deletions src/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,10 @@
left: -999em;
}

.dt-hidden{
display: none;
}

body.dt-resize {
cursor: col-resize;
}
Loading