猿问

显示数据表中具有单个值的下拉过滤器

我正在使用数据表,列值是 (tag1, tag2, tag3) 逗号分隔我已经为该列创建了一个下拉过滤器


该下拉列表的值类似于列值 (tag1, tag2, tag3) 逗号分隔


但我需要为下拉列表中的每个选项提供一个值


标签1


屋顶2


标签3


等等


这是代码


initComplete: function () {

    this.api().columns([1]).every(function () {

        var column = this;

        var select = jQuery('<select id="test-haris"><option value=""></option> 

            < /select>')

                .appendTo(jQuery(column.header()).empty())

                .on('change', function () {

                    var val = jQuery.fn.dataTable.util.escapeRegex(

                        jQuery(this).val()

                    );

                    column

                        .search(val ? '^' + val + '$' : '', true, false)

                        .draw();

                });

        column.data().unique().sort().each(function (d, j) {

            select.append('<option value="' + d + '">' + d + '</option>')

        });

    });

}


HUWWW
浏览 88回答 1
1回答

慕仙森

以下方法根据第一列的内容构建一个选择列表(下拉列表)。对于该列中的每个单元格,它将逗号分隔的项目拆分为单独的文本片段,然后为下拉列表创建一个排序的唯一列表。当您通过从下拉列表中选择项目进行搜索时,它会检查所选项目是否包含在该列中每个单元格的文本中的任何位置。为此,它使用自定义的 DataTables 过滤器。就我而言,我将下拉菜单放置在表格的页脚中 - 您可以更改它。该表如下所示:这是下拉菜单:从下拉列表中选择一个项目后,您可以看到过滤效果:该解决方案的代码如下 - 我已将其分成单独的部分/函数,以尝试使结构和方法更清晰:<!doctype html><html><head>&nbsp; <meta charset="UTF-8">&nbsp; <title>Demo</title>&nbsp; <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>&nbsp; <script src="https://cdn.datatables.net/1.10.21/js/jquery.dataTables.min.js"></script>&nbsp; <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.21/css/jquery.dataTables.min.css">&nbsp; <link rel="stylesheet" type="text/css" href="https://datatables.net/media/css/site-examples.css"></head><body><div style="margin: 20px;">&nbsp; &nbsp; <table id="example" class="display dataTable cell-border" style="width:100%">&nbsp; &nbsp; &nbsp; &nbsp; <thead>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Name</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Position</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Office</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Age</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Start date</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Salary</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; </thead>&nbsp; &nbsp; &nbsp; &nbsp; <tbody>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>Tiger , John, Nixon , </td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>System Architect</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>Edinburgh</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>61</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>2011/04/25</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>$320,800</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>John, Garrett , Winters , </td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>Accountant</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>Tokyo</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>63</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>2011/07/25</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>$170,750</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>Ashton, Winters , Cox</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>Junior Technical Author</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>San Francisco</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>66</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>2009/01/12</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>$86,000</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>Cedric , Kelly , Kelly</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>Senior Javascript Developer</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>Edinburgh</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>22</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>2012/03/29</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>$433,060</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; </tbody>&nbsp; &nbsp; &nbsp; &nbsp; <tfoot>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Name</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Position</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Office</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Age</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Start date</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Salary</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; </tfoot>&nbsp; &nbsp; </table></div><script type="text/javascript">$(document).ready(function() {&nbsp;&nbsp;&nbsp; // the DataTable object:&nbsp;&nbsp;&nbsp; var table = $('#example').DataTable( {&nbsp; &nbsp; select: false // or, whatever you need in here.&nbsp; } );&nbsp; // Setup - add a select list to first footer cell:&nbsp; $('#example tfoot th').slice(0, 1).each( function () {&nbsp; &nbsp; var dropdown = buildDropdown();&nbsp; &nbsp; $(this).html( dropdown );&nbsp; } );&nbsp; // add a change event to the select list:&nbsp; $('#mySelect').change(function() {&nbsp; &nbsp; table.draw();&nbsp; });&nbsp; // create a custom search function for the select list,&nbsp; // which finds if the selected item is contained in the cell:&nbsp; $.fn.dataTable.ext.search.push(&nbsp; &nbsp; function( settings, data, dataIndex ) {&nbsp; &nbsp; &nbsp; var selectedValue = $('#mySelect').val();&nbsp; &nbsp; &nbsp; console.log(selectedValue);&nbsp; &nbsp; &nbsp; if (data[0].includes(selectedValue)) {&nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; );&nbsp; function buildDropdown() {&nbsp; &nbsp; var selectHtml;&nbsp; &nbsp; // this will hold array of distinct values:&nbsp; &nbsp; var items = [];&nbsp; &nbsp; table.columns([0]).data().each(function (data, index) {&nbsp; &nbsp; &nbsp; data.forEach(function (newItems, index) {&nbsp; &nbsp; &nbsp; &nbsp; newItems.split(',').forEach(function (newItem, index) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( newItem.trim() !== '' && items.indexOf(newItem) === -1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; items.push(newItem.trim());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; });&nbsp; &nbsp; // sort and remove duplicates:&nbsp; &nbsp; var uniqueSortedItems = [...new Set(items)].sort();&nbsp; &nbsp; selectHtml = '<select id="mySelect"><option value=""></option>';&nbsp; &nbsp; uniqueSortedItems.forEach(function(item) {&nbsp;&nbsp; &nbsp; &nbsp; selectHtml = selectHtml + '<option value="' + item + '">' + item + '</option>';&nbsp; &nbsp; });&nbsp; &nbsp; selectHtml = selectHtml + '</select>';&nbsp; &nbsp; return selectHtml;&nbsp; }} );</script></body></html>我认为这就是您想要实现的目标 - 但当然,您需要将其集成到您的特定解决方案中。您还需要决定如何处理全局搜索功能(如果您正在使用它),因为它可能会干扰用于第一列的自定义搜索。
随时随地看视频慕课网APP
我要回答