【问题标题】:DataTables button filters from data html attribute数据 html 属性中的 DataTables 按钮过滤器
【发布时间】:2018-07-03 18:47:36
【问题描述】:

我正在将 DataTables 插件用于具有动态信息 (https://datatables.net/) 的 bootstrap 4 表。我从不同的 API 获取信息,构建一个对象并附加到表中。

一切正常,但我希望能够在表格中未向用户显示的内容上创建一些过滤按钮。

我有几列可用于排序/搜索(默认数据表),但我希望能够从数据区域和数据子区域 HTML 属性中进行过滤。我已经从 DataTables 插件中阅读了有关数据搜索的信息,但由于我需要超过 1 个过滤器(正如我所说,从 -region 和 -subregion 将是一个开始,但可能会扩展)这对我来说并不是真正有用。

我的想法是创建一个下拉菜单(用于外观目的)并将按钮链接到过滤器。过滤器将检查按下的按钮(按钮文本)并根据它过滤整个表格(类似于搜索,但针对数据 html 属性)。

我整天都在寻找类似的东西,但一无所获....希望这里有人使用过这样的 DataTables,可以给我一个提示。

提前致谢。

【问题讨论】:

  • 目前的 DateTables API 似乎只有 2 个过滤选项;两者都不执行您想要的搜索。您可以将 HTML 注入生成的 DataTables HTML 中,然后执行您的过滤器,或者您可以修改搜索框的行为,将您自己的事件附加到搜索框,一旦它生成并可供 DOM 使用。我已使用此方法将我自己选择的下拉过滤器添加到生成的 DataTables 中。
  • 注入 HTML 是指为每个项目或其他内容添加包含所需信息的新表格列?数据区域、数据子区域等已经在 html 中(更准确地说,它们是每个项目标题中的数据属性)。修改行为​​部分是我需要的,但我不知道从哪里开始。我检查了他们的文档,过去的堆栈问题,但我真的不明白该怎么做。您能否分享一些有关您如何进行下拉过滤的代码?谢谢。
  • DataTables api 将元素添加到表中以进行搜索和分页以及您实现的任何其他选项。我的意思是,一旦该 api 生成它的 HTML,您就可以使用 JavaScript 来识别这些元素和容器,并使用自定义功能将您自己的 HTML 注入其中。
  • 例如:在 DataTables 网站的登录页面(您的链接有问题)他们有一个例子。检查元素,您可以找到诸如“dataTables_wrapper”和“dataTables-filter”之类的类名。使用 JavaScript,document.querySelector('.dataTables_wrapper'),你可以在任何你喜欢的地方添加 HTML。
  • 我已经说过信息在 html 中,例如(不是真实的)class="blabla" data-region="Region" data-subregion="subregion">跨度>

标签: javascript jquery datatables bootstrap-4


【解决方案1】:

好吧,我还没有找到任何方法,所以我放弃了。我使用之前存储在“数据”属性中的信息创建了一些新列。我隐藏了列并使用了 api 的表引用

table.columns(colnumber).search('text').draw()

不漂亮,但它很有效,所以我不会浪费更多时间寻找不同的方法。

【讨论】:

    【解决方案2】:

    作为 JQuery 和 DataTables 插件,可能有一种更优雅的方式来执行此操作,但这很简单且有效。

    这是一个表格的 HTML:

    <table style="width:100%;border-collapse:collapse;border:solid 1px black;">
            <thead>
                <tr>
                    <th>Column 1</th>
                    <th>Column 2</th>
                </tr>
            </thead>
            <tbody>
                <tr data-region="1" data-subregion="a">
                    <td>Region 1</td>
                    <td>Sub-Region A</td>
                </tr>
                <tr data-region="1" data-subregion="b">
                    <td>Region 1</td>
                    <td>Sub-Region B</td>
                </tr>
                <tr data-region="2" data-subregion="a">
                    <td>Region 2</td>
                    <td>Sub-Region A</td>
                </tr>
                <tr data-region="2" data-subregion="b">
                    <td>Region 2</td>
                    <td>Sub-Region B</td>
                </tr>
                <tr data-region="2" data-subregion="c">
                    <td>Region 2</td>
                    <td>Sub-Region C</td>
                </tr>
                <tr data-region="3" data-subregion="a">
                    <td>Region 3</td>
                    <td>Sub-Region A</td>
                </tr>
            </tbody>
        </table>
    

    配置数据表并添加您的自定义元素以进行过滤。

    $(document).ready(function(){
    
            // Execute the DataTables API on the table(s)
            $('table').DataTable();
    
            // give the DataTables API a moment to finish drawing elements to the DOM
            setTimeout(function(){
                // Just adding some Dropdown lists, but you can add a custom search box
                // this first one filters by the data-region attribute
                var regionFilter = $('<select data-filter="region">'
                    + '<option value="0">All Regions</option>'
                    + '<option value="1">Region 1</option>'
                    + '<option value="2">Region 2</option>' 
                    + '<option value="3">Region 3</option>' 
                    + '</select>');
                // this second one filters by the data-subregion attribute
                var subregionFilter = $('<select data-filter="subregion">'
                    + '<option value="0">All Sub-Regions</option>'
                    + '<option value="a">Sub-Region A</option>'
                    + '<option value="b">Sub-Region B</option>' 
                    + '<option value="c">Sub-Region C</option>' 
                    + '</select>');
                // prepend the dropdown lists into the dataTables_filter container
                // optionally, you can overwrite the default search box that the DataTables API places here
                $('.dataTables_filter').prepend(regionFilter);
                $('.dataTables_filter').prepend(subregionFilter);
                // give your own HTML a moment to draw to the DOM
                setTimeout(function(){
                    // configure the selection to trigger the filter action
                    $('select[data-filter]').on('change', function(){
                        // the <select> element has a data-filter attribute which defines which attribute to search the table for
                        var dataFilter = $(this).attr('data-filter');
                        var keyword = $('select[data-filter="' + dataFilter + '"] option:selected').val();
                        // execute the search
                        searchDataAttributes(dataFilter, keyword);
                    });
                }, 300);
            }, 400);
    
        });
    

    创建您自己的搜索实现

    // This search is very simple - you can implement any type of search you like
        function searchDataAttributes(attribute, keyword) {
            // put together the attribute to search
            var dataAttribute = 'data-' + attribute;
            // the value 0 is being used to show all
            if (keyword=='0') {
                $('tbody tr').show();
                return true;
            }
            // if the keyword is not 0, then look at the attributes of each row in the table
            $('tbody tr').each(function(){
                var attributeValue = $(this).attr(dataAttribute);
                // if the value of the attribute in the row matches the value of the keyword, then show the row
                if (attributeValue==keyword) {
                    console.log('show');
                    $(this).show();
                } else {
                    console.log('hide');
                    $(this).hide();
                }
            });
        }
    

    这是一个简单的下拉列表过滤器。您可以将搜索框替换为您自己的自定义搜索框,按照您想要的方式进行搜索。如果您不想,您根本不必让 DataTables 绘制它们的搜索框。这只是绕过 DataTables 限制的一种方法。

    【讨论】:

    • 感谢您抽出宝贵时间写下所有内容,但这绝对不是我想要的。我在页面上有分页,这也不是 DataTables 搜索,它是 jquery 中的显示/隐藏。在询问之前我已经尝试过了,但是该方法存在几个问题 - > 1 - 隐藏/显示会破坏分页(您可能有少于 X 个项目的页面,因为它们是隐藏的并且 DataTables 似乎没有检查隐藏行) 2 - 隐藏/显示不使用 DataTables api 进行搜索和/或其他功能,因此它变得无用。这种方法最好不使用 DT。
    • @DanteR。我解释说这是该方法的一个简单示例。由您决定如何以您希望的方式实现搜索。如果您不喜欢这种方法或不理解它,那也没关系。
    【解决方案3】:

    datatables.net 中使用引导下拉菜单

    1. 从 Bootstrap 复制所有下拉列表,https://getbootstrap.com/docs/4.5/components/dropdowns/
    2. 创建一个从引导程序返回下拉 html 的 javascript 函数,如下所示:
    function SearchHtml(){
            return '<div class="position-relative">' + 
      '<button class="btn btn-block btn-soft-secondary dropdown-toggle" href="javascript:;" role="button"' + 
           'aria-haspopup="true"' + 
           'aria-expanded="false"' + 
           'data-unfold-event="click' + "
           'data-unfold-target="#filter3Dropdown"' + 
           'data-unfold-type="css-animation"' + 
           'data-unfold-duration="300"' + 
           'data-unfold-delay="300"' + 
           'data-unfold-animation-in="slidefadeIn"' + 
           'data-unfold-animation-out="fadeOut">' + 
            '<span class="fas fa-sliders-h dropdown-item-icon"></span>' + 
            'Refine' + 
      '</button>' + 
      '<div id="filter3Dropdown" class="dropdown-menu dropdown-unfold dropdown-menu-sm-right dropdown-menu-size-lg p-5" aria-labelledby="filter3DropdownInvoker">' + 
          '<div class="row">' + 
              '<div class="col-md-12">' + 
                 '<!-- create your html refine search parameters here -->' + 
              '</div>' + 
          '</div>' + 
      '</div>' + 
    '</div>';
        }
    
    1. 只需删除下拉菜单中的所有下拉项,然后创建/构造您的 html 搜索参数。

    2. 从数据表初始化开始,将按钮文本设置为 SearchHtml()。对于 className,调整按钮类,因为按钮会随着填充而显得更大。

    
        buttons: [
                    {
                        text: SearchHtml(),   
                        className: 'btn-sm btn-rounded p-0 border-primary'
                    }
                ]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多