| 1 |
(function($) { |
| 2 |
'use strict'; |
| 3 |
|
| 4 |
function parseConfig(raw) { |
| 5 |
if (!raw) { |
| 6 |
return {}; |
| 7 |
} |
| 8 |
try { |
| 9 |
return JSON.parse(raw); |
| 10 |
} catch (e) { |
| 11 |
return {}; |
| 12 |
} |
| 13 |
} |
| 14 |
|
| 15 |
function getCellValue($row, index, type) { |
| 16 |
var $cell = $row.find('td').eq(index); |
| 17 |
var value = $cell.text().trim(); |
| 18 |
|
| 19 |
if (type === 'number' || type === 'currency' || type === 'percent') { |
| 20 |
var normalized = value.replace(/[^0-9.+-]/g, ''); |
| 21 |
var number = parseFloat(normalized); |
| 22 |
return isNaN(number) ? 0 : number; |
| 23 |
} |
| 24 |
|
| 25 |
return value.toLowerCase(); |
| 26 |
} |
| 27 |
|
| 28 |
function TableFrontend($container) { |
| 29 |
this.$container = $container; |
| 30 |
this.config = parseConfig($container.attr('data-config')); |
| 31 |
this.$table = $container.find('table'); |
| 32 |
this.$tbody = this.$table.find('tbody'); |
| 33 |
this.$rows = this.$tbody.find('tr'); |
| 34 |
this.rows = this.$rows.toArray(); |
| 35 |
this.filtered = this.rows.slice(); |
| 36 |
this.currentPage = 1; |
| 37 |
this.rowsPerPage = parseInt(this.config.rowsPerPage || 10, 10); |
| 38 |
this.$pagination = $container.find('.kng-table-pagination'); |
| 39 |
|
| 40 |
this.bindEvents(); |
| 41 |
this.applyPagination(); |
| 42 |
} |
| 43 |
|
| 44 |
TableFrontend.prototype.bindEvents = function() { |
| 45 |
var self = this; |
| 46 |
|
| 47 |
if (this.config.search) { |
| 48 |
this.$container.find('.kng-table-search input').on('input', function() { |
| 49 |
self.applySearch($(this).val()); |
| 50 |
}); |
| 51 |
} |
| 52 |
|
| 53 |
if (this.config.sorting) { |
| 54 |
this.$table.find('th').on('click', function() { |
| 55 |
var $th = $(this); |
| 56 |
var index = $th.index(); |
| 57 |
var current = $th.attr('data-sort') || 'none'; |
| 58 |
var next = current === 'asc' ? 'desc' : 'asc'; |
| 59 |
|
| 60 |
self.$table.find('th').attr('data-sort', ''); |
| 61 |
$th.attr('data-sort', next); |
| 62 |
|
| 63 |
self.sortByColumn(index, next); |
| 64 |
}); |
| 65 |
} |
| 66 |
}; |
| 67 |
|
| 68 |
TableFrontend.prototype.applySearch = function(query) { |
| 69 |
var self = this; |
| 70 |
var keyword = (query || '').toLowerCase(); |
| 71 |
|
| 72 |
this.filtered = this.rows.filter(function(row) { |
| 73 |
var text = $(row).text().toLowerCase(); |
| 74 |
return text.indexOf(keyword) !== -1; |
| 75 |
}); |
| 76 |
|
| 77 |
this.currentPage = 1; |
| 78 |
this.renderRows(); |
| 79 |
this.renderPagination(); |
| 80 |
}; |
| 81 |
|
| 82 |
TableFrontend.prototype.sortByColumn = function(index, direction) { |
| 83 |
var type = this.$table.find('th').eq(index).attr('data-type') || 'text'; |
| 84 |
var self = this; |
| 85 |
|
| 86 |
this.filtered.sort(function(a, b) { |
| 87 |
var valueA = getCellValue($(a), index, type); |
| 88 |
var valueB = getCellValue($(b), index, type); |
| 89 |
|
| 90 |
if (valueA < valueB) { |
| 91 |
return direction === 'asc' ? -1 : 1; |
| 92 |
} |
| 93 |
if (valueA > valueB) { |
| 94 |
return direction === 'asc' ? 1 : -1; |
| 95 |
} |
| 96 |
return 0; |
| 97 |
}); |
| 98 |
|
| 99 |
this.filtered.forEach(function(row) { |
| 100 |
self.$tbody.append(row); |
| 101 |
}); |
| 102 |
|
| 103 |
this.currentPage = 1; |
| 104 |
this.renderRows(); |
| 105 |
this.renderPagination(); |
| 106 |
}; |
| 107 |
|
| 108 |
TableFrontend.prototype.applyPagination = function() { |
| 109 |
if (!this.config.pagination) { |
| 110 |
this.$pagination.hide(); |
| 111 |
this.filtered = this.rows.slice(); |
| 112 |
this.renderRows(); |
| 113 |
return; |
| 114 |
} |
| 115 |
|
| 116 |
this.renderPagination(); |
| 117 |
this.renderRows(); |
| 118 |
}; |
| 119 |
|
| 120 |
TableFrontend.prototype.renderRows = function() { |
| 121 |
var self = this; |
| 122 |
var start = 0; |
| 123 |
var end = this.filtered.length; |
| 124 |
|
| 125 |
if (this.config.pagination) { |
| 126 |
start = (this.currentPage - 1) * this.rowsPerPage; |
| 127 |
end = start + this.rowsPerPage; |
| 128 |
} |
| 129 |
|
| 130 |
this.$rows.hide(); |
| 131 |
|
| 132 |
this.filtered.slice(start, end).forEach(function(row) { |
| 133 |
$(row).show(); |
| 134 |
}); |
| 135 |
}; |
| 136 |
|
| 137 |
TableFrontend.prototype.renderPagination = function() { |
| 138 |
var self = this; |
| 139 |
|
| 140 |
if (!this.config.pagination) { |
| 141 |
return; |
| 142 |
} |
| 143 |
|
| 144 |
var totalPages = Math.ceil(this.filtered.length / this.rowsPerPage) || 1; |
| 145 |
this.$pagination.empty(); |
| 146 |
|
| 147 |
for (var i = 1; i <= totalPages; i++) { |
| 148 |
var $btn = $('<button type="button"></button>'); |
| 149 |
$btn.text(i); |
| 150 |
if (i === this.currentPage) { |
| 151 |
$btn.addClass('active'); |
| 152 |
} |
| 153 |
(function(page) { |
| 154 |
$btn.on('click', function() { |
| 155 |
self.currentPage = page; |
| 156 |
self.renderRows(); |
| 157 |
self.renderPagination(); |
| 158 |
}); |
| 159 |
})(i); |
| 160 |
this.$pagination.append($btn); |
| 161 |
} |
| 162 |
}; |
| 163 |
|
| 164 |
$(document).ready(function() { |
| 165 |
$('.kng-table-builder').each(function() { |
| 166 |
new TableFrontend($(this)); |
| 167 |
}); |
| 168 |
}); |
| 169 |
})(jQuery); |
| 170 |
|