Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Improved table sorting. #4

Merged
merged 3 commits into from
Aug 8, 2024
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
129 changes: 91 additions & 38 deletions dist/frappe-datatable.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -2259,42 +2259,12 @@ class DataManager {
}

_sortRows(colIndex, sortOrder) {

if (this.currentSort.colIndex === colIndex) {
// reverse the array if only sortOrder changed
if (
(this.currentSort.sortOrder === 'asc' && sortOrder === 'desc') ||
(this.currentSort.sortOrder === 'desc' && sortOrder === 'asc')
) {
this.reverseArray(this.rowViewOrder);
this.currentSort.sortOrder = sortOrder;
return;
}
if (this.options.treeView) {
this.treeSort(colIndex, sortOrder);
} else {
this.rowViewOrder.sort((a, b) => this.compareContent(a, b, colIndex, sortOrder));
}

this.rowViewOrder.sort((a, b) => {
const aIndex = a;
const bIndex = b;

let aContent = this.getCell(colIndex, a).content;
let bContent = this.getCell(colIndex, b).content;
aContent = aContent == null ? '' : aContent;
bContent = bContent == null ? '' : bContent;

if (sortOrder === 'none') {
return aIndex - bIndex;
} else if (sortOrder === 'asc') {
if (aContent < bContent) return -1;
if (aContent > bContent) return 1;
if (aContent === bContent) return 0;
} else if (sortOrder === 'desc') {
if (aContent < bContent) return 1;
if (aContent > bContent) return -1;
if (aContent === bContent) return 0;
}
return 0;
});

if (this.hasColumnById('_rowIndex')) {
// update row index
const srNoColIndex = this.getColumnIndexById('_rowIndex');
Expand All @@ -2306,6 +2276,82 @@ class DataManager {
}
}

treeSort(colIndex, sortOrder) {
let tree = [];
let rowMap = {};

// Build Tree
for (let i = 0; i < this.rows.length; i++) {
const rowIndex = this.rows[i].meta.rowIndex;
const currentIndent = this.rows[i].meta.indent;

let currentNode = { parent: rowIndex, children: [] };
rowMap[rowIndex] = currentNode;

if (currentIndent === 0) {
tree.push(currentNode);
} else {
let parentIndex = rowIndex - 1;

while (parentIndex >= 0 && this.rows[parentIndex].meta.indent >= currentIndent) {
parentIndex--;
}

if (parentIndex >= 0) {
rowMap[parentIndex].children.push(currentNode);
}
}
}

// Sort Tree
this._sortTree(tree, colIndex, sortOrder);

// Row View Order
let flattenedTree = [];

let traverseNode = (node) => {
flattenedTree.push(node.parent);
if (node.children) {
node.children.forEach(child => traverseNode(child));
}
};

tree.forEach(node => traverseNode(node));
this.rowViewOrder = flattenedTree;
}

_sortTree(tree, colIndex, sortOrder) {
if (!tree || tree.length === 0) return;

tree.sort((a, b) => this.compareContent(a.parent, b.parent, colIndex, sortOrder));
tree.forEach(node => {
this._sortTree(node.children, colIndex, sortOrder);
});
}

compareContent(a, b, colIndex, sortOrder) {
const aIndex = a;
const bIndex = b;

let aContent = this.getCell(colIndex, a).content;
let bContent = this.getCell(colIndex, b).content;
aContent = aContent == null ? '' : aContent;
bContent = bContent == null ? '' : bContent;

if (sortOrder === 'none') {
return aIndex - bIndex;
} else if (sortOrder === 'asc') {
if (aContent < bContent) return -1;
if (aContent > bContent) return 1;
if (aContent === bContent) return 0;
} else if (sortOrder === 'desc') {
if (aContent < bContent) return 1;
if (aContent > bContent) return -1;
if (aContent === bContent) return 0;
}
return 0;
};

reverseArray(array) {
let left = null;
let right = null;
Expand Down Expand Up @@ -2952,7 +2998,11 @@ class CellManager {
this.clearSelection();
this._selectedCells = cells.map(index => this.getCell$(...index));
requestAnimationFrame(() => {
this._selectedCells.map($cell => $cell.classList.add('dt-cell--highlight'));
this._selectedCells.forEach($cell => {
if ($cell && $cell.classList) {
$cell.classList.add('dt-cell--highlight');
}
});
});
return true;
}
Expand Down Expand Up @@ -3011,8 +3061,11 @@ class CellManager {
}

clearSelection() {
(this._selectedCells || [])
.forEach($cell => $cell.classList.remove('dt-cell--highlight'));
(this._selectedCells || []).forEach($cell => {
if ($cell && $cell.classList) {
$cell.classList.remove('dt-cell--highlight');
}
});

this._selectedCells = [];
this.$selectionCursor = null;
Expand Down Expand Up @@ -5965,7 +6018,7 @@ class DataTable {
DataTable.instances = 0;

var name = "@paralogic/frappe-datatable";
var version = "1.17.4";
var version = "1.17.5";
var description = "A modern datatable library for the web";
var main = "dist/frappe-datatable.cjs.js";
var unpkg = "dist/frappe-datatable.min.js";
Expand Down
Loading
Loading