gpt4 book ai didi

javascript - 使用带复选框的多选下拉列表搜索或过滤 jquery 数据表中的列

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:00:09 25 4
gpt4 key购买 nike

我正在尝试使用多选复选框下拉列表来过滤或搜索 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();

我已经编写了一个完整的示例以供将来引用:

JSFIDDLE

关于javascript - 使用带复选框的多选下拉列表搜索或过滤 jquery 数据表中的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41739419/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com