Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/410.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用带有复选框的多选下拉列表搜索或筛选jquery datatable中的列_Javascript_Jquery_Css_Checkbox_Datatables - Fatal编程技术网

Javascript 使用带有复选框的多选下拉列表搜索或筛选jquery datatable中的列

Javascript 使用带有复选框的多选下拉列表搜索或筛选jquery datatable中的列,javascript,jquery,css,checkbox,datatables,Javascript,Jquery,Css,Checkbox,Datatables,我正在尝试使用multiselect复选框下拉列表筛选或搜索jquery数据表中的列。例如,假设它是一个城市列,如果我按照复选框下拉列表选中两个cites并在下拉列表中显示选中cites的结果,则表应该进行过滤或搜索。我尝试了很多方法来寻找解决方案,但到目前为止,我还是无法解决。如果有人能帮助解决这个问题,我们将不胜感激。提前谢谢。我只是把jquery代码放在下面供您参考 <head> <meta charset="utf-8"> <title>

我正在尝试使用multiselect复选框下拉列表筛选或搜索jquery数据表中的列。例如,假设它是一个城市列,如果我按照复选框下拉列表选中两个cites并在下拉列表中显示选中cites的结果,则表应该进行过滤或搜索。我尝试了很多方法来寻找解决方案,但到目前为止,我还是无法解决。如果有人能帮助解决这个问题,我们将不胜感激。提前谢谢。我只是把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>

jQuery数据表
* {
保证金:0;
填充:0;
}
#包裹{
宽度:100%;
保证金:自动;
}
艾德科尔先生{
颜色:绿色;
}
/*$(文档).ready(函数(){
$('#示例th')。每个(函数(){
警报(this.textContent);
});
});*/
$(文档).ready(函数(){
$('#示例')。数据表({
“bFilter”:没错,
initComplete:函数(){
this.api().columns(2).every(函数)(){
var列=此;
变量选择=$(“”)
.appendTo($(column.footer()).empty());
/*.on('change',function(){
var val=$.fn.dataTable.util.escapeRegex(
$(this.val()
);
柱
.search(val?“^”+val+“$”:“”,true,false)
.draw();
} );*/
column.data().unique().sort().each(函数(d,j){
选择。追加(“”+d+“”)
} );
} );
}
});
$('#示例tbody tr td:nth child(4)')。每个(函数(){
if($(this).text()==61){
$(this.css(“颜色”、“红色”);
}
else if($(this).text()==47){
$(此).addClass(“addcolr”);
}
});
//多选
$('sel#u下拉列表').multiselect();
//$(“#示例”).DataTable();
$('sel#u下拉列表')。更改(函数(){
$(“#示例”).DataTable().fnFilter($(“#sel#U下拉列表”).val(),2);
});
} );

您可以通过

它允许您构建正则表达式,然后过滤所需的列:

$('#table').DataTable().column(col).search(regex, true, true).draw();
我编写了一个完整的示例以供将来参考:


如果您使用angularjs,您可以尝试,它非常容易使用!: