繁体   English   中英

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

[英]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();

我编写了一个完整的示例以供将来参考:

JSFIDDLE

如果你使用 angularjs,你可以试试Smart table ,它超级好用!:

 <st-select-multiple collection="collection" predicate="education"></st-select-multiple>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM