table.js (3114B)
1 import $ from '../../../../cms/ui/themes/default/script/jquery-global.js'; 2 3 export default function(element ) { 4 5 6 let calculateOrderList = function() { 7 8 // Analyse the order of the objects in this folder. 9 let order = new Array(); 10 11 $(element).find('.or-table--sortable').find('tbody > tr.or-data').each(function () { 12 let objectid = $(this).data('id'); 13 order.push(objectid); 14 }); 15 16 // Set the comma-separated list of objects into a input field. 17 $(element).find('input[name=order]').val(order.join(',')); 18 }; 19 20 calculateOrderList(); 21 22 // Manuelles Sortieren von Tabellen per Drag and drop. 23 24 let sortableElement = $(element).find('.or-table--sortable > tbody').get(0); 25 26 if ( sortableElement ) { 27 28 let sortable = import( './sortable.min.js' ); 29 sortable.then( Sortable => { 30 new Sortable(sortableElement, {onUpdate: calculateOrderList}) 31 } ); 32 33 } 34 35 // Alle Checkboxen setzen oder nicht setzen. 36 $(element).find('tr.headline > td > input.checkbox').click( function() { 37 $(this).closest('table').find('tr.or-data > td > input.or-checkbox').attr('checked',Boolean( $(this).attr('checked') ) ); 38 }); 39 40 /** 41 * Table-Filter. 42 */ 43 $(element).find('.or-table-filter > input').keyup( function() { 44 45 let filterExpression = $(this).val().toLowerCase(); 46 47 let table = $(this).parents('.or-table-wrapper').find('table'); 48 table.addClass('loader'); 49 50 setTimeout( () => { 51 table.find('tr:not(.or-table-header)').each(function () { 52 let $row = $(this); 53 if ( $row.text().toLowerCase().indexOf(filterExpression) > -1) 54 $row.removeClass('-off'); 55 else 56 $row.addClass('-off'); 57 } ); 58 table.removeClass('loader'); 59 }, 50); 60 61 } ); 62 63 64 /** 65 * Table-Sortierung. 66 */ 67 $(element).find('table > tbody > tr.or-table-header > td, table > tbody > tr > th').click( function() { 68 69 let column = $(this); 70 let table = column.closest('table'); 71 table.addClass('loader'); 72 73 let isAscending = !column.hasClass('sort-asc'); 74 table.find('tr.or-table-header > td, tr > th').removeClass('sort-asc').removeClass('sort-desc'); 75 if ( isAscending ) column.addClass('sort-asc'); else column.addClass('sort-desc'); 76 77 Promise.resolve().then( () => { // Sorting should be asynchronous, because we do not want to block the UI. 78 79 let rows = table.find('tr:not(.or-table-header)').toArray().sort(comparer(column.index())) 80 if (!isAscending) { 81 rows = rows.reverse() 82 } 83 for (let i = 0; i < rows.length; i++) { 84 table.append( $(rows[i]) ); 85 } 86 table.removeClass('loader'); 87 }); 88 89 } ); 90 91 function comparer(index) { 92 return function(a, b) { 93 let valA = getCellValue(a, index), valB = getCellValue(b, index) 94 return isNumeric(valA) && isNumeric(valB) ? valA - valB : valA.toString().localeCompare(valB) 95 } 96 } 97 98 function getCellValue(row, index) { 99 let x = $(row).children('td').eq(index); 100 return $(row).children('td').eq(index).text(); 101 } 102 103 function isNumeric(n) { 104 return !isNaN(parseFloat(n)) && isFinite(n); 105 } 106 107 };