我正在使用DataTables-plug (版本1.10.12)在我的网站上显示一个表格。表中名为"status“的一列包含一个下拉字段,如下所示:
<tr>
<td>
...
<select name="dropdown_status" id="139">
<option value="-1">canceled</option>
<option value="1" selected>open</option>
<option value="2">in work</option>
<option value="3">waiting</option>
<option value="4">closed</option>
</select>
...
</td>
</tr>
我想要按该状态-column进行过滤,例如,只显示具有所选状态closed
的行。默认过滤当然不适用于这样的内容,因为它不是需要过滤的纯文本,而是需要剥离的html代码。我尝试过使用选择器、DataTables呈现和搜索函数的不同方法。我对JavaScript、AJAX、JQuery和DataTables都很陌生,而且我的想法也快用完了。任何帮助都将不胜感激。
发布于 2016-08-10 14:16:42
JS代码:
$(document).ready(function() {
$('#example').DataTable( {
initComplete: function () {
this.api().columns().every( function () {
var column = this;
var select = $('<select><option value=""></option></select>')
.appendTo( $(column.footer()).empty() )
.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>' )
} );
} );
}
} );
} );
HTML代码:
<table id="example" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</tfoot>
<tbody>
<tr>
<td>Tiger Nixon</td>
<td>System Architect</td>
<td>Edinburgh</td>
<td>61</td>
<td>2011/04/25</td>
<td>$320,800</td>
</tr>
</tbody>
</table>
包括以下datatable jquery文件:
https://code.jquery.com/jquery-1.12.3.js
https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js
https://stackoverflow.com/questions/38865494
复制相似问题