DataTables Case Insensitive Search Issue
DataTables Case Insensitive Search Issue
I'm using DataTables and jQuery to create a set of checkboxes to filter the table using the search box. This is implemented fully at http://www.lynden.com/about/brochures-test.html. Basically, when you select an option with the checkboxes, it'll take that option as a string and insert it in the DataTables search box. It works great, except for the fact that the caseInsensitive feature is just straight up not working when entering input into the search box without any prior filtering, which is weird. Weirder still is that when searching with "mi" it will pull up three results with the word Dynamic in them, but will totally ignore the Milky Way company category. Does anyone have any idea why it can sort of search case insensitively but ignores the beginnings of capitalized words?
$('.dropdown-container')
.on('click', '.dropdown-button', function () {
$('.dropdown-list').toggle();
})
.on('input', '.dropdown-search', function () {
var target = $(this);
var search = target.val().toLowerCase();
console.log(search);
if (!search) {
$('li').show();
return false;
}
$('li').each(function () {
var text = $(this).text().toLowerCase();
var match = text.indexOf(search) > -1;
$(this).toggle(match);
});
})
.on('change', '[type="checkbox"]', function () {
var numChecked = $('[type="checkbox"]:checked').length;
$('.quantity').text(numChecked || 'Any');
});
$(document).ready(function () {
table = $('#brochure').DataTable({
"search": {
"caseInsensitive": true
},
"pageLength": 25,
"order": [
[2, "desc"]
],
"lengthMenu": [
[25, 50, 75, -1],
[25, 50, 75, "All"]
],
"columnDefs": [{
"targets": [2, 4, 5],
"visible": false
}]
});
var pID = location.search; //grab everything after and including the "?" in the URL
console.log(pID);
mloc = pID.indexOf("=") + 1; //identify the location of the actual value
pID = pID.slice(mloc) // captures the value of the parameter
table.search(pID, [1, 2, 3, 4], true, false, false, true).draw(); // assigns the parameter to the hidden input tag's value
})
function filter() {
//build a industry string
var filters = $('input:checkbox[name="filter"]:checked').map(function () {
return this.value;
}).get().join(' ');
console.log(filters);
//now filter in column 3, with a regular expression, no smart filtering, no inputbox, not case sensitive
table.search(filters, [1, 2, 3, 4], true, false, false, true).draw();
}
I'd love some help on this issue!
Answers
I got a great answer for this from Stack Overflow: https://stackoverflow.com/questions/47892623/datatables-case-insensitive-search-issue
It appears this line has too many parameters:
table.search(filters, [1, 2, 3, 4], true, false, false, true).draw();
The parameters are described in this doc:
https://datatables.net/reference/api/search()
Looks like you have one too many boolean values and it looks like you are trying to search certain columns ( [1, 2, 3, 4). To search in specific columns you will need to use
columns().search()
.Kevin