【问题标题】:Filter column by dropdown-values using DataTables使用 DataTables 按下拉值过滤列
【发布时间】:2016-08-10 06:10:49
【问题描述】:

我正在使用 DataTables-plug(版本 1.10.12)在我的网站上显示表格。 表格的一列,称为“状态”,包含一个下拉字段,例如:

<tr>
<td>
...
    <select name="dropdown_status" id="139">
      <option value="-1">canceled</option>
      <option value="1" selected>open</option>
      <option value="2">in work</option>
      <option value="3">waiting</option>
      <option value="4">closed</option>
    </select>
...
</td>
</tr>

我想按该status-列进行过滤,例如,仅显示具有选定状态closed 的行。 默认过滤当然不适用于此类内容,因为需要过滤的不是纯文本,而是需要剥离的 html 代码。 我尝试了使用选择器、DataTables 渲染和搜索功能的不同方法。我对 JavaScript、AJAX、JQuery 和 DataTables 非常陌生,而且我的想法已经不多了。任何帮助将不胜感激。

【问题讨论】:

  • 你需要 milti-filter select 就像在这个例子中:link
  • sysquare:感谢您的评论,但我看不出这个例子对我有什么帮助。该示例过滤纯文本,但我想我需要以某种方式剥离我的列内容。

标签: javascript jquery datatables


【解决方案1】:

JS 代码:

$(document).ready(function() {
    $('#example').DataTable( {
        initComplete: function () {
            this.api().columns().every( function () {
                var column = this;
                var select = $('<select><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>' )
                } );
            } );
        }
    } );
} );

HTML 代码:

<table id="example" class="display" cellspacing="0" width="100%">
        <thead>
            <tr>
                <th>Name</th>
                <th>Position</th>
                <th>Office</th>
                <th>Age</th>
                <th>Start date</th>
                <th>Salary</th>
            </tr>
        </thead>
        <tfoot>
            <tr>
                <th>Name</th>
                <th>Position</th>
                <th>Office</th>
                <th>Age</th>
                <th>Start date</th>
                <th>Salary</th>
            </tr>
        </tfoot>
        <tbody>
            <tr>
                <td>Tiger Nixon</td>
                <td>System Architect</td>
                <td>Edinburgh</td>
                <td>61</td>
                <td>2011/04/25</td>
                <td>$320,800</td>
            </tr>

        </tbody>
    </table>

包括这些数据表 jquery 文件:

https://code.jquery.com/jquery-1.12.3.js

https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js

【讨论】:

  • 感谢您的回答。不幸的是,这并不能解决问题(我已经有相同的代码):我要过滤的状态列的下拉字段多次具有相同的值,并且过滤不起作用。每次我想过滤该列时结果都是空的,导致一个空表。
  • 虽然我的代码工作正常,但我的要求与您的完全相同。你可能需要调试 javascript 代码,肯定有问题。
  • 你能告诉我你的代码吗?尤其是带有 filterarble 下拉字段的表格列。
  • 这里不能粘贴。字数限制。但是,我的是基于 JSP Java spring 的代码。
猜你喜欢
  • 2011-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-30
  • 2021-07-18
  • 1970-01-01
  • 2017-01-12
  • 1970-01-01
相关资源
最近更新 更多