columns().search() not working on multiple columns?
columns().search() not working on multiple columns?
lastbyte
Posts: 8Questions: 0Answers: 0
OK. I'm 0 for 2 on getting any responses on posts so far, but here goes #3 :-).
Is there a bug in columns(...).search(), or am I just not using it right? When I try to search multiple columns, only the first col gets searched. The below:
[code]
$('#search').on('keyup', function () {
table.api().columns([0,1,2]).search($(this).val()).draw();
});
[/code]
will only search column 0. Same thing if I use the other selectors (eg, class name). Any suggestions?
Is there a bug in columns(...).search(), or am I just not using it right? When I try to search multiple columns, only the first col gets searched. The below:
[code]
$('#search').on('keyup', function () {
table.api().columns([0,1,2]).search($(this).val()).draw();
});
[/code]
will only search column 0. Same thing if I use the other selectors (eg, class name). Any suggestions?
This discussion has been closed.
Replies
I think the problem you might be seeing here is that it is an AND search that DataTables is doing on the columns - i.e. `$(this).val()` is being applied to all three columns. If it isn't in all three, then the record won't be shown.
My guess is you are looking for an OR filter - is that correct? I'm sorry to say that DataTables doesn't have the ability to selectively do an OR filter over a set of columns, other than the global filter. Although it a darn fine idea for a future enhancement.
Allan
Thanks for the insight. That does make sense. I found a simple work around, but for the sake of completeness, I'll tell you what I was trying to do.
Basically, the issue that I was up against is that I wanted table.api().search(val) to only search a subset of the columns. I know I can simply set the non-searchable columns to searchable: false, but this renders the rest of the columns "invisible" to the other search methods (added via $.fn.dataTable.ext.search.push()).
To elaborate, I have 3 sets of fields: text, number, date. I want the text fields to be searchable via a text input, the date field to be searchable via a date field, and the number field to be searchable via a drop down. If all of the columns (text, number, date) are searchable: true, then I get unwanted results when the user types in a numeric value. And, if I set the numeric and date columns to searchable: false, then the middle parameter of the per-row search callback doesn't include the non-searchable values.
The work-around is to set all of the non-text fields to searchable: false, keep a reference to the entire data set, and use the 3rd parameter of the search function to index into it:
[code]
var dateMinSecs = null,
dateMaxSecs = null,
selIds = null;
$.fn.dataTable.ext.search.push(
function (settings, searchRow, dataIndex) {
if (dateMinSecs && dateMinSecs) {
return data[dataIndex][3] <= dateMaxSecs && data[dataIndex][3] >= dateMinSecs;
}
return true;
},
function (settings, searchRow, dataIndex) {
if (selIds) {
return selIds[data[dataIndex][13]];
}
return true;
}
);
$('#campaigns').on('change', function (evt) {
var $this = $(this);
selIds = ($this.prop('selectedIndex') < 0) ? null : {};
$.map($this.val(), function (val, i) {
selIds[parseInt(val, 10)] = true;
});
table.api().draw();
});
$('#search').on('keyup', function () {
var data = table.api()
.search($(this).val())
.draw();
});
[/code]
table.columns([0]).search($(this).val()).draw();
Allan