I have three JQuery DataTables in my JSP page on different tabs where I want to display almost the same table, with a slight modification. The table on the first tab goes as:
<table id="firstTable">
<tbody>
<c:forEach items="${A_List}" varStatus="status" var="alist">
<tr role="row" id="colorRow" data-user="${alist.D}">
<td>${alist.A}</td>
<td>${alist.B}</td
<td>${alist.C}</td>
<td>${alist.D}</td>
</tr>
</c:forEach>
</tbody>
</table>
Now, the second table on the second tab:
<table id="secondTable">
<tbody>
<c:forEach items="${A_List}" varStatus="status" var="alist">
<tr role="row" id="colorRow" data-user="${alist.D}">
<td>${alist.A}</td>
<td>${alist.B}</td
<td>${alist.C}</td>
<td>${alist.D}</td>
</tr>
</c:forEach>
</tbody>
</table>
And the third table goes as:
<table id="thirdTable">
<tbody>
<c:forEach items="${A_List}" varStatus="status" var="alist">
<tr role="row" id="colorRow" data-user="${alist.D}">
<td>${alist.A}</td>
<td>${alist.B}</td
<td>${alist.C}</td>
<td>${alist.D}</td>
</tr>
</c:forEach>
</tbody>
</table>
Now, on the first table, I want to display everything. On the second table, I want to display only the rows which have "Yes" as their value in the last column and the third table displays all the rows which have "No" as their value in the last column. Additionally, there are rows which neither have a "Yes" or a "No". They can be completely ignored. For this, I tried to implement it as:
$(function(){
var firstTable = $('#firstTable').DataTable();
var secondTable = $('#secondTable').DataTable();
var thirdTable = $('#thirdTable').DataTable();
$.fn.dataTable.ext.search.push(
function(settings, data, dataIndex) {
return $(firstTable.row(dataIndex).node()).attr('data-user') == 'YES';
});
secondTable.draw();
$.fn.dataTable.ext.search.push(
function(settings, data, dataIndex) {
return $(secondTable.row(dataIndex).node()).attr('data-user') == 'NO';
});
thirdTable.draw();
});
However, this gives doesn't do as intended. The second tab does appear, but when I try to filter out anything using the search tab, the first and the second, both tables are messed up.
I also tried fnDeleteRow
. Didn't work either. Thanks in advance!
You need to make sure you've got a
thead
:Which will stop DataTables complaining, then your DataTables can be created like this:
Working JSFiddle here. Hope that helps (there are probably better ways of doing it TBH - perhaps someone else will chip in as this is quite hackie - it always helps to have something to look at... JSFiddle is your friend).