I've been researching into this for a while and useful information is drying up without a solution.
I'm using Datatables, and in certain columns I am displaying a Chosen multi-select box to allow the user to edit their data after it is added by continuing to make selections.
I've got per-column search boxes working using initComplete, and they do filter the data in the column, but not in an acceptable way; they are matching on the options' text, but also on the select element's classes, and even on all the unselected options, such that it is useless.
...,
initComplete: function () {
// Apply the search
this.api().columns().every(function () {
var that = this;
$('input', this.footer()).on('keyup change clear', function () {
if (that.search() !== this.value) {
that
.search(this.value)
.draw();
}
});
});
}
I would like this to only match on the text of the selected options of the select boxes. Writing the function to get the array of text strings of a select box in a given row is easily done by me, but I'm struggling to find out where I can hook in such a custom function to the Datatables flow.
The Datatables documentation mentions that you can define a column filter function for orthogonal data, but gives no example of this and I'm struggling to find any online either. My attempt at this was adding the following to the DataTable initialisation, but it's not getting called:
...,
"columns": [
{
filter: function (a,b,c) {
console.log('called from column filter');
console.log(arguments)
return false;
}
},
{
filter: function (a,b,c) {
console.log('called to column filter');
console.log(arguments)
return false;
}
},
null
]
Thank you for your time.
Update
So I've found that I can push a function onto $.fn.dataTable.ext.search.push(...) which seems like it can do what I need. Following down this path, I've found that the column html that is passed in the parameters does not include the Chosen generated elements, which I need, just the pre-chosen .... I've tried using table.rows.invalidate.draw but this just removes the Chosen elements from the DOM instead of fixing this problem by making Datatables pass in the expected html data.
Example of the actual row html that I would expect to be passed in:
<td>
<select class="myclasses form-control" multiple="" style="display: none;">
<option value="...">Bahamas</option>
...
</select>
<div class="chosen-container chosen-container-multi" title="" style="width: 437px;">
<ul class="chosen-choices">
<li class="search-choice">
<span>Greece</span>
<a class="search-choice-close" data-option-array-index="4"></a>
</li>
<li class="search-choice">
<span>Belgium</span>
<a class="search-choice-close" data-option-array-index="6"></a>
</li>
<li class="search-choice">
<span>France</span>
<a class="search-choice-close" data-option-array-index="7"></a></li><li class="search-field">
<input class="chosen-search-input" type="text" autocomplete="off" value="Select some options" style="width: 25px;">
</li>
</ul>
<div class="chosen-drop">
<ul class="chosen-results"></ul>
</div>
</div>
</td>
The actual parameter data in the search call, which appears to be in the pre-chosen state. This is why I expected .invalidate() to fix this...
<select class="myclasses form-control" multiple>
<option value="...">Bahamas</option>
</select>
I'm now looking into generating the chosen html before I add the select html to the table. This may still have problems if a user makes changes to the select box options after they have been added to the table, but I'll see when I get there.
