[英]Search or filter a column in jquery datatable using multi select dropdown with checkbox
我正在嘗試使用多選復選框下拉列表過濾或搜索 jquery 數據表中的列。 例如,假設它是一個城市列,如果我根據復選框下拉列表檢查兩個引用並在下拉列表中顯示檢查引用的結果,則該表應該過濾或搜索。 我嘗試了很多方法來找到解決方案,但是,我到現在都無法破解。 如果有人可以幫助解決這個問題,我們將不勝感激。 提前致謝。 我只是將 jquery 代碼放在下面供您參考。
<head>
<meta charset="utf-8">
<title>jQuery Datatable</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css">
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.13/css/jquery.dataTables.min.css">
<link rel="stylesheet" href="http://davidstutz.github.io/bootstrap-multiselect/dist/css/bootstrap-multiselect.css">
<!-- <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> -->
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://cdn.datatables.net/1.10.13/js/jquery.dataTables.min.js"></script>
<script src="http://davidstutz.github.io/bootstrap-multiselect/dist/js/bootstrap-multiselect.js"></script>
<style>
* {
margin:0;
padding: 0;
}
#wrap {
width: 100%;
margin: auto;
}
.addcolr {
color: green;
}
</style>
<script>
/*$(document).ready(function() {
$('#example th').each(function() {
alert(this.textContent);
});
});*/
$(document).ready(function() {
$('#example').DataTable({
"bFilter": true,
initComplete: function () {
this.api().columns(2).every( function () {
var column = this;
var select = $('<select id="sel_dropdown" multiple="multiple"><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>' )
} );
} );
}
});
$('#example tbody tr td:nth-child(4)').each( function(){
if($(this).text() == 61) {
$(this).css("color","red");
}
else if($(this).text() == 47) {
$(this).addClass("addcolr");
}
});
//multiselect
$('#sel_dropdown').multiselect();
// $('#example').DataTable();
$('#sel_dropdown').change( function () {
$('#example').DataTable().fnFilter( $('#sel_dropdown').val(),2);
});
} );
</script>
</head>
<body>
<div id="wrap"></div></body>
您可以使用Search API實現這一點
它允許您構建正則表達式,然后過濾所需的列:
$('#table').DataTable().column(col).search(regex, true, true).draw();
我編寫了一個完整的示例以供將來參考:
如果你使用 angularjs,你可以試試Smart table ,它超級好用!:
<st-select-multiple collection="collection" predicate="education"></st-select-multiple>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.