Skip to content
This repository was archived by the owner on Feb 17, 2019. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ For customization, the following parameters are available:
* sortOpt: A boolean indicating whether or not to sort the options. This must be true for sortOptCallback to work. Defaults to true.
* debug: A boolean indicating whether or not to run in "debug" mode. Debug mode displays performance data.
* minOptions: Number of options required to show a select filter. Defaults to 2, meaning if a column only has one unique value, no select box will be shown (what would be the point?).
* showFilterInteractions: A boolean indicating whether or not to add a "ddtf-optionunavailable" class to filter values that will interact with other already-applied filters to result in the table being completely filtered. This helps avoid users wasting time on useless options, but does add some overhead and therefore may not be appropriate for very large tables, especially in restricted environments (mobile).

Markup Options
--------------------
The plugin will also take into account the following instructions in the table markup:

* "skip-filter" class on column headers ("th" elements) will cause the plugin to avoid that column.
* "filter-container-elementid" attribute on column headers ("th" elements) will cause the filtering drop-down to be created in the specified container element, instead of replacing the content of the header element.

Compatibility
-------------------
Expand Down
51 changes: 48 additions & 3 deletions ddtf.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ $.fn.ddTableFilter = function(options) {
var table = $(this);
var start = new Date();

$('th:visible', table).each(function(index) {
$('th', table).each(function(index) {
if($(this).hasClass('skip-filter')) return;
var selectbox = $('<select>');
var values = [];
Expand Down Expand Up @@ -42,8 +42,14 @@ $.fn.ddTableFilter = function(options) {
$(selectbox).append('<option value="' + this.val + '">' + this.text + '</option>')
});

$(this).wrapInner('<div style="display:none">');
$(this).append(selectbox);
var targetID = $(this).attr('filter-container-elementid');
if(targetID) {
$("#" + targetID, table).append(selectbox);
}
else {
$(this).wrapInner('<div style="display:none">');
$(this).append(selectbox);
}

selectbox.bind('change', {column:col}, function(event) {
var changeStart = new Date();
Expand Down Expand Up @@ -84,6 +90,44 @@ $.fn.ddTableFilter = function(options) {
}
});

if (showFilterInteractions) {
//handle greying out of inapplicable filters
$('th', table).each(function(index) {
if($(this).hasClass('skip-filter')) return;
var values = [];
$('tr:not(.skip-filter)', table).each(function(rowindex) {
var rowValue;
var otherfilterHidesRow = false;
$('td', this).each(function(columnindex) {
if(columnindex === index) {
rowValue = $(this).attr('ddtf-value');
}
else {
if($(this).hasClass('ddtf-filtered'))
otherfilterHidesRow = true;
}
});
if(!otherfilterHidesRow && $.inArray(rowValue, values) === -1)
values.push(rowValue);
});

var targetID = $(this).attr('filter-container-elementid');
var $targetSelect;

if(targetID)
$targetSelect = $("#" + targetID + ">select", table);
else
$targetSelect = $("select", this);

$("option", $targetSelect).each(function(index) {
if($.inArray($(this).val(), values) === -1 && $(this).val() != '--all--')
$(this).addClass("ddtf-optionunavailable");
else
$(this).removeClass("ddtf-optionunavailable");
});
});
}

if($.isFunction(options.afterFilter)) {
options.afterFilter.apply(table);
}
Expand Down Expand Up @@ -121,6 +165,7 @@ $.fn.ddTableFilter.defaultOptions = {
},
emptyText:'--Empty--',
sortOpt:true,
showFilterInteractions:false,
debug:false,
minOptions:2
}
Expand Down