2017-01-19 7 views
3

複数選択チェックボックスを使用してjqueryデータテーブルの列をフィルタリングまたは検索しようとしています。たとえば、都市の列であるとすると、チェックボックスを2つ選択すると、フィルタリングまたは検索が行われ、チェックボックスの結果がドロップダウンで表示されます。私は解決策を見つけるために多くの方法を試しましたが、私は今までそれを分解することができませんでした。誰かがこれを解決するのを助けることができれば大いに感謝します。前もって感謝します。私はちょうどあなたの参照のために下に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> 

答えて

5

あなたはSearch API

でそれはあなたが正規表現を構築し、目的の列フィルタリングすることができますことを達成することができます:私は今後の参考のために完全な例をコード化しました

$('#table').DataTable().column(col).search(regex, true, true).draw(); 

を:

JSFIDDLE

0

あなたはangularjsを使用する場合は、Smart tableを試すことができ、それが使用する超簡単です!:

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

関連する問題