Filter DataTable rows by row tag
Filter DataTable rows by row tag
tomography
Posts: 3Questions: 2Answers: 0
I have a DataTable where each row have a tag.
<table id="mytable" class="table table-striped table-bordered dt-responsive" width="100%">
<thead>
<tr>
<th>Column1</th>
<th>Column2</th>
</tr>
</thead>
<tbody>
<tr mytag = "tag1">
<td>...</td>
<td>...</td>
</tr>
<tr mytag = "tag2">
<td>...</td>
<td>...</td>
</tr>
</tbody>
</table>
Have a select box with all the tags.
<select id="select_tag" class="select2-original">
<option value="tag1">tag1</option>
<option value="tag2">tag2</option>
</select>
And when a tag is selected, I want to filter the table and only display the rows with the tag but what I did is not working.
var mytable= $('#mytable').DataTable();
$('#select_tag').on('change', function () {
var tagvalue = this.value;
mytable
.rows(function (idx, data, node) {
return node.getAttribute("mytag") == tagvalue? true : false;
})
.draw();
})
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
You will probably want to use a search plugin for this. In the plugin you can get the row with
var row = table.row(dataIndex).node();
. This example posted in this thread on May 5 might help. Anything after that in the thread is probably not applicable to your solution. Also look at this thread.Kevin