【问题标题】:make columns unsortable in dataTables.js jQuery plugin在 dataTables.js jQuery 插件中使列不可排序
【发布时间】:2011-08-15 22:51:13
【问题描述】:

我正在使用 dataTables.js jQuery 插件。

我的表格的第一列是一个行计数器,所以我不希望它可以被用户排序。最后一列包含用户可以在一行上执行的一些操作链接。如何使这两列无法排序?

注意:我正在使用数据表的管道(服务器端进程)模式。

【问题讨论】:

  • 这个问题你解决了吗?如果是,你能提供正确的答案吗?

标签: jquery jquery-plugins datatables


【解决方案1】:

这是通过将 bSortable 设置为 false 来完成的:

/* Using aoColumnDefs */
$(document).ready(function() {
    $('#example').dataTable( {
        "aoColumnDefs": [ 
            { "bSortable": false, "aTargets": [ 0 ] }
        ] } );
} );

/* Using aoColumns */
$(document).ready(function() {
    $('#example').dataTable( {
        "aoColumns": [ 
            { "bSortable": false },
            null,
            null,
            null,
            null
        ] } );
} );

【讨论】:

    【解决方案2】:

    DataTables 1.10+ 还包括supports HTML5 data- style attributes,包括data-sortable="false",这使得该列不符合排序条件:

    <table>
        <thead>
            <tr>
                <th data-sortable="false">Row</th>
                <th>Name</th>
                <th>Join Date</th>
                <th>Organization</th>
                <th data-sortable="false">Options</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                [etc]
            </tr>
        </tbody>
    </table>
    

    See this demo in jsFiddle

    【讨论】:

      【解决方案3】:

      aaSortingFixed

      这个参数基本一致 到 aaSorting 参数,但不能 被用户交互覆盖 桌子。这意味着你 可以有一个列(可见或 隐藏),排序将始终 首先强制 - 之后的任何排序 然后(来自用户)将是 按要求执行。这可以是 用于将行分组在一起。

      使用示例:

      $(document).ready( function() {
          $('#example').dataTable( {
               "aaSortingFixed": [[0,'asc'],[5,'asc']]
          } );
      } );
      

      0 是“不可排序”行的编号(从左至右)。 (所以在那个例子中,第一列和第六列是固定的)

      Official Documentation

      【讨论】:

        【解决方案4】:

        您可以在单独的列中定义一个回调函数以支持不可更改的数字顺序:

        $('#someId').dataTable({
                // ...
                "aoColumns": [
                    // ...
                    {"bSortable": false}, // set unsortable this column
                    // ...
                ],
                fnDrawCallback: function(oSettings) {
                    $(this).find('tbody tr').each(function(index) {
                        $(this).find('td').eq(1).text(index + 1); // .eq([index of column])
                    });
                }
            });
        

        【讨论】:

          猜你喜欢
          • 2013-12-28
          • 1970-01-01
          • 2015-11-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-04-08
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多