简体   繁体   English

jQuery 数据表中的下拉过滤

[英]Dropdown filtering in jQuery Datatables

Link to CodePen .链接到CodePen

I'm using the Datatables jQuery plugin for this table.我正在为此表使用Datatables jQuery 插件

I'm trying to figure out how to implement the Filter By Location dropdown at the top to work.我想弄清楚如何实现顶部的按位置过滤下拉列表来工作。 So if you select Bracebridge from the dropdown for example, it only shows the products with Bracebridge.因此,例如,如果您从下拉列表中选择 Bracebridge,它只会显示带有 Bracebridge 的产品。 I've tried playing around with the column().search() function from Datatables, having a hard time getting it to work.我尝试使用 Datatables 中的column().search() 函数,但很难让它工作。

HTML HTML

<!--Filter-->
          <p>Filter By Location</p>
          <select id="locfilter" name="location">
            <option value="">All</option>
            <option value="Ajax">Ajax</option>
            <option value="Barrie">Barrie</option>
            <option value="Belleville">Belleville</option>
            <option value="Bracebridge">Bracebridge</option>
            <option value="Bradford">Bradford</option>
            <option value="Brampton">Brampton</option>
            <option value="Brantford">Brantford</option>
            <option value="Burlington">Burlington</option>
            <option value="Cambridge">Cambridge</option>
            <option value="Cobourg">Cobourg</option>
            <option value="Concord">Concord</option>
            <option value="Gloucester">Gloucester</option>
            <option value="Gormley">Gormley</option>
            <option value="Guelph">Guelph</option>
            <option value="Hamilton">Hamilton</option>
            <option value="Kingston">Kingston</option>
            <option value="London">London</option>
            <option value="Milton">Milton</option>
            <option value="Napanee">Napanee</option>
            <option value="Niagara Falls">Niagara Falls</option>
            <option value="North Bay">North Bay</option>
            <option value="Ottawa">Ottawa</option>
            <option value="Owen Sound">Owen Sound</option>
            <option value="Peterborough">Peterborough</option>
            <option value="Sarnia">Sarnia</option>
            <option value="Sault Ste Marie">Sault Ste Marie</option>
            <option value="Sudbury">Sudbury</option>
            <option value="Timmins">Timmins</option>
            <option value="Toronto">Toronto</option>
            <option value="Trenton">Trenton</option>
            <option value="Waterloo">Waterloo</option>
            <option value="Windsor">Windsor</option>
            <option value="Woodstock">Woodstock</option>
          </select>

JS (only has this snippet to pull in the table) JS(只有这个片段可以拉入表格)

$(document).ready(function() {
  $("#used-equip-table").DataTable();
});

Update your JavaScript section to this:将您的 JavaScript 部分更新为:

$(document).ready(function() {

  // create a variable for your table, so we can refer to it below:
  var table = $("#used-equip-table").DataTable();

  // add events to your drop-down control, to detect changes:
  $('#locfilter').on( 'keyup change clear', function () {
    table.draw();
  } );

  // use the following to process filter/search changes:
  $.fn.dataTable.ext.search.push(
    function( settings, data, dataIndex ) {
      var selectedValue = $('#locfilter').val();
      if (data[4].includes(selectedValue)) { // 4 = the fifth column!
        return true;
      } else {
        return false;
      }
    }
  );

});

That ext.search.push function shown above is part of the search plug-in, described here .上面显示的ext.search.push函数是search插件的一部分,描述在这里

Add an event on change of your custom filter.添加有关更改自定义过滤器的事件。

Please see this -请看这个——

$(document).ready(function() {
 $("#used-equip-table").DataTable();
});

$('#locfilter').on( 'change', function () {
 $('#used-equip-table').DataTable().column( 4 ).search(
   $('#locfilter').val()
 ).draw();
});

Please see working example here请在此处查看工作示例

   $("#locfilter").change(function() {
    var value = $("#locfilter").val();
    $.fn.dataTable.ext.search.push(
    function( settings, data, dataIndex ) {
        return data[4]==value
            ? true
            : false
        }     
    );
    table.draw();
    $.fn.dataTable.ext.search.pop();
   });

Try this code it worked for me .试试这个对我有用的代码。

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

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