mirror of
https://github.com/Karaka-Management/jsOMS.git
synced 2026-02-15 00:38:42 +00:00
Improve performance of sorting
This commit is contained in:
parent
b9761f16bb
commit
9e5f78c3ea
|
|
@ -132,11 +132,12 @@
|
||||||
const table = document.getElementById(id),
|
const table = document.getElementById(id),
|
||||||
rows = table.getElementsByTagName('tbody')[0].rows,
|
rows = table.getElementsByTagName('tbody')[0].rows,
|
||||||
rowLength = rows.length,
|
rowLength = rows.length,
|
||||||
rowId = this.closest('tr').rowIndex;
|
rowId = this.closest('tr').rowIndex,
|
||||||
|
orderType = jsOMS.hasClass(this, 'order-up') ? 1 : -1;
|
||||||
|
|
||||||
if (jsOMS.hasClass(this, 'order-up') && rowId > 1) {
|
if (orderType === 1 && rowId > 1) {
|
||||||
rows[rowId].parentNode.insertBefore(rows[rowId - 2], rows[rowId]);
|
rows[rowId].parentNode.insertBefore(rows[rowId - 2], rows[rowId]);
|
||||||
} else if (jsOMS.hasClass(this, 'order-down') && rowId < rowLength) {
|
} else if (orderType === -1 && rowId < rowLength) {
|
||||||
rows[rowId - 1].parentNode.insertBefore(rows[rowId], rows[rowId - 1]);
|
rows[rowId - 1].parentNode.insertBefore(rows[rowId], rows[rowId - 1]);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
@ -151,9 +152,10 @@
|
||||||
const table = document.getElementById(id),
|
const table = document.getElementById(id),
|
||||||
rows = table.getElementsByTagName('tbody')[0].rows,
|
rows = table.getElementsByTagName('tbody')[0].rows,
|
||||||
rowLength = rows.length,
|
rowLength = rows.length,
|
||||||
cellId = this.closest('td').cellIndex;
|
cellId = this.closest('td').cellIndex,
|
||||||
|
sortType = jsOMS.hasClass(this, 'sort-asc') ? 1 : -1;
|
||||||
|
|
||||||
let j, row1, row2, content1, content2,
|
let j, i, row1, row2, content1, content2,
|
||||||
order = false,
|
order = false,
|
||||||
shouldSwitch = false;
|
shouldSwitch = false;
|
||||||
|
|
||||||
|
|
@ -163,21 +165,28 @@
|
||||||
for (j = 0; j < rowLength - 1; ++j) {
|
for (j = 0; j < rowLength - 1; ++j) {
|
||||||
shouldSwitch = false;
|
shouldSwitch = false;
|
||||||
row1 = rows[j].getElementsByTagName('td')[cellId];
|
row1 = rows[j].getElementsByTagName('td')[cellId];
|
||||||
row2 = rows[j + 1].getElementsByTagName('td')[cellId];
|
|
||||||
content1 = row1.getAttribute('data-content') !== null ? row1.getAttribute('data-content') : row1.textContent;
|
content1 = row1.getAttribute('data-content') !== null ? row1.getAttribute('data-content') : row1.textContent;
|
||||||
content2 = row2.getAttribute('data-content') !== null ? row2.getAttribute('data-content') : row2.textContent;
|
|
||||||
|
|
||||||
if (jsOMS.hasClass(this, 'sort-asc') && content1 > content2) {
|
for (i = j + 1; i < rowLength; ++i) {
|
||||||
shouldSwitch = true;
|
row2 = rows[i].getElementsByTagName('td')[cellId];
|
||||||
break;
|
content2 = row2.getAttribute('data-content') !== null ? row2.getAttribute('data-content') : row2.textContent;
|
||||||
} else if (jsOMS.hasClass(this, 'sort-desc') && content1 < content2) {
|
|
||||||
shouldSwitch = true;
|
if (sortType === 1 && content1 > content2) {
|
||||||
|
shouldSwitch = true;
|
||||||
|
} else if (sortType === -1 && content1 < content2) {
|
||||||
|
shouldSwitch = true;
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (shouldSwitch === true) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (shouldSwitch) {
|
if (shouldSwitch) {
|
||||||
rows[j].parentNode.insertBefore(rows[j + 1], rows[j]);
|
rows[j].parentNode.insertBefore(rows[i - 1], rows[j]);
|
||||||
order = true;
|
order = true;
|
||||||
}
|
}
|
||||||
} while (order);
|
} while (order);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user