Files
intranet/resources/js/jquery.filterTable.js
2020-12-01 17:23:13 -03:00

68 lines
1.5 KiB
JavaScript

(function($) {
$.fn.filterTable = function(options)
{
var input = this;
var excludeFilter = function(row, col, headers, excludes) {
var exclude = false;
var cols = $(headers.get(row)).find('th');
if (excludes.indexOf($(cols.get(col)).html().toUpperCase()) > -1) {
exclude = true;
}
return exclude;
}
this.keyup(function(e) {
var settings = $.extend({
"table": "filteredTable",
"excludes": [],
"height": 1
}, options);
var id_table = settings.table;
var excludes = settings.excludes;
var height = settings.height;
for (i = 0; i < excludes.length; i ++) {
excludes[i] = excludes[i].toUpperCase();
}
var filter = input.val().toUpperCase();
var table = $('#' + id_table);
var rows = table.find('tbody tr');
var headers = table.find('thead tr');
if (filter == '') {
rows.css('display', '');
return;
}
for (i = 0; i < rows.length; i += height) {
var cols;
var found = false;
for (n = 0; n < height; n ++) {
cols = $(rows.get(i + n)).find('td');
$.each(cols, function(j, e) {
if (excludeFilter(n, j, headers, excludes)) {
return;
}
if ($(this).html().toUpperCase().indexOf(filter) > -1) {
found = true;
}
});
}
if (found) {
for (n = 0; n < height; n ++) {
$(rows.get(i + n)).css('display', '');
}
} else {
for (n = 0; n < height; n ++) {
$(rows.get(i + n)).css('display', 'none');
}
}
}
});
return this;
}
}(jQuery));