How to filter by column x?
How to filter by column x?
I can't figure out how to select what column to use when filtering a dataset.
With my current code, it automaticly selects the first column and then populates a select-box with each unique value,but I need to use column #2.
TLRD; How to populate and filter by other column than the first one?
I'm sure this is stupid simple, but...
Current code:
<script type="text/javascript">
$(document).ready(function()
{
oTable = $('#contacts-datatable').DataTable(
{
pageLength: 100,
dom: "lrt",
order: [
[0, "asc"]
],
columnDefs: [
{
targets: [0],
visible: true,
searchable: true
}],
initComplete: function()
{
this.api().columns(':lt(1)').every(function()
{
var column = this;
var select = $('<select class="titleOption "><option value=""></option></select>')
.appendTo($(".filterSomething"))
.on('change', function()
{
var val = $.fn.dataTable.util.escapeRegex(
$(this).val()
);
column
.search(val ? '^' + val + '$' : '', true, false)
.draw();
});
column.data().unique().sort().each(function(d, j)
{
select.append('<option value="' + d + '">' + d + '</option>')
});
});
}
});
$('#contactSearchBox').keyup(function(){
oTable.search($(this).val()).draw() ;
})
});
</script>
Edited by Colin - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.
This question has an accepted answers - jump to answer
Answers
is saying columns less than 1, i.e. 0, so the first column. To make that the second column, use
though as it's a single column, you don't really need the loop,
Colin
@colin : Awesome, thanks! I need to brush up on my JS...