openrat-cms

OpenRat Content Management System
git clone http://git.code.weiherhei.de/openrat-cms.git
Log | Files | Refs | README

commit 0a539c48cdceac7667238db63dcd3e3d34c40d19
parent df643e680a736c2cd76443ed8336f9f544ed0719
Author: Jan Dankert <develop@jandankert.de>
Date:   Sat, 17 Apr 2021 03:02:30 +0200

Fix: Table sorter (was broken since the migration to OQuery)

Diffstat:
Mmodules/cms/ui/themes/default/script/Oquery.js | 23+++++++++++++++++++++++
Mmodules/template_engine/components/html/component_table/table.js | 33+++++++++++++++++++--------------
2 files changed, 42 insertions(+), 14 deletions(-)

diff --git a/modules/cms/ui/themes/default/script/Oquery.js b/modules/cms/ui/themes/default/script/Oquery.js @@ -265,4 +265,27 @@ export class OQuery { return false; } + toArray() { + return this.nodes; + } + + eq( index ) { + return this.createNew( [ this.nodes[index] ] ); + } + + index() { + if ( this.nodes.length == 0 ) + return -1; + + let node = this.nodes[0]; + + let children = node.parentNode.childNodes; + let num = 0; + for (let i=0; i<children.length; i++) { + if ( children[i] == node) return num; + if ( children[i].nodeType==1) num++; + } + return -1; + } + } \ No newline at end of file diff --git a/modules/template_engine/components/html/component_table/table.js b/modules/template_engine/components/html/component_table/table.js @@ -60,39 +60,44 @@ export default function(element ) { /** * Table-Sortierung. */ - $(element).find('table > tbody > tr.headline > td, table > tbody > tr > th').click( function() { + $(element).find('table > tbody > tr.or-table-header > td, table > tbody > tr > th').click( function() { let column = $(this); - let table = column.parents('table'); + let table = column.closest('table'); table.addClass('loader'); let isAscending = !column.hasClass('sort-asc'); - table.find('tr.headline > td, tr > th').removeClass('sort-asc').removeClass('sort-desc'); + table.find('tr.or-table-header > td, tr > th').removeClass('sort-asc').removeClass('sort-desc'); if ( isAscending ) column.addClass('sort-asc'); else column.addClass('sort-desc'); - setTimeout(function () { // Sorting should be asynchronous, because we do not want to block the UI. + new Promise( (resolve,reject) => { // Sorting should be asynchronous, because we do not want to block the UI. - let rows = table.find('tr:gt(0)').toArray().sort(comparer(column.index())) - if (!isAscending) { - rows = rows.reverse() - } - for (var i = 0; i < rows.length; i++) { - table.append(rows[i]) - } - table.removeClass('loader'); - }, 50); + let rows = table.find('tr:not(.or-table-header)').toArray().sort(comparer(column.index())) + if (!isAscending) { + rows = rows.reverse() + } + for (let i = 0; i < rows.length; i++) { + table.append( $(rows[i]) ); + } + table.removeClass('loader'); + }); } ); function comparer(index) { return function(a, b) { let valA = getCellValue(a, index), valB = getCellValue(b, index) - return $.isNumeric(valA) && $.isNumeric(valB) ? valA - valB : valA.toString().localeCompare(valB) + return isNumeric(valA) && isNumeric(valB) ? valA - valB : valA.toString().localeCompare(valB) } } function getCellValue(row, index) { + let x = $(row).children('td').eq(index); return $(row).children('td').eq(index).text(); } + function isNumeric(n) { + return !isNaN(parseFloat(n)) && isFinite(n); + } + }; \ No newline at end of file