【问题标题】:Datatables - how to disable sorting on last column in a table when not all columns are shown数据表 - 当未显示所有列时如何禁用表中最后一列的排序
【发布时间】:2016-09-06 09:26:09
【问题描述】:

我有一个如下所示的表格:

JS:

"columns": [
            {
                "data": 0,
                "iDataSort" : 0
            },
            {
                "data": 1,
                "iDataSort" : 1,
                "render": function ( data, type, row ) {
                    var returnString = data;
                    if( row[2] !== null && row[2].length > 0){
                        returnString += "<ul>";
                        for( var alternativeTitle in row[2] ){
                            returnString += "<li>" + row[2][alternativeTitle] + "</li>"
                        }
                        returnString += "</ul>"
                    }
                    return returnString;
                }
            },
            {
                "iDataSort" : 3,
                "data": 3
            },
            {
                "iDataSort" : 4,
                "data": 4
            },
            {
                "data":null,
                "bSearchable": false,
                "bSortable": false,
                "sClass": "center",
                "render": function (row) {
                    return '<a href"#">Edit</a>';
                }

            }

        ],

HTML:

        <table>
            <thead>
            <tr role="row" class="heading">
                <th>
                    Id#
                </th>
                <th>
                    Original title (alternative titles)
                </th>
                <th>
                    Year of production
                </th>
                <th>
                    Country
                </th>
                <th>
                    Actions
                </th>
            </tr>
            </thead>
            <tbody>
            </tbody>
        </table>

如您所见,我提供了一个 5 列数组作为数据源(id、标题、替代标题[]、生产年份和国家/地区),但我以不同方式呈现它们(原始标题和替代标题位于同一列而最后一列是操作按钮)。现在我想禁用对最后一列的排序(因为这没有任何意义,因为它是为按钮保留的)但是当我将 "bSortable": false 放在带有按钮的最后一列上时,它会影响最后一列在视觉上,但也会影响数据表发送的 GET,实际上当我点击“按国家/地区排序”时,没有任何反应,因为数据表将“bSortable_4 = false”发送到服务器。

我希望能够对 Country 列进行排序,但要禁用 Actions 列上的排序。如何做到这一点?

【问题讨论】:

标签: javascript jquery sorting datatable datatables


【解决方案1】:

这里是html

<table id="example">
            <thead>
            <tr role="row" class="heading">
                <th>
                    Id#
                </th>
                <th>
                    Original title (alternative titles)
                </th>
                <th>
                    Year of production
                </th>
                <th>
                    Country
                </th>
                <th>
                    Actions
                </th>
            </tr>
            </thead>
            <tbody>
            <tr><td>1</td><td>1</td><td>1</td><td>1</td><td>delete</td></tr>
            <tr><td>2</td><td>2</td><td>4</td><td>5</td><td>delete</td></tr>
            </tbody>
        </table>

这里是javascript

$(document).ready(function() {
$('#example').dataTable({
"order": [],
"aoColumnDefs": [
          { 'bSortable': false, 'aTargets': [ 4 ] }
       ],
});
});

这是工作小提琴https://jsfiddle.net/rtn6496n/10/

希望对你有帮助!

【讨论】:

  • 它没有用。当我使用您的解决方案时,它禁用了“操作”和“国家”列的排序。我能够单击“国家”列,但什么也没发生,因为数据表发送了“bSortable_4”== false,其中 4 是服务器列数组中的国家列(0 - id,1 - 原始标题,2 - 替代头衔,3 - 年,4 - 国家)
  • 它在我发送的小提琴中工作,请根据您的需要尝试即兴发挥,谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-11-12
  • 2015-10-25
  • 1970-01-01
  • 2015-12-15
  • 2018-04-29
  • 1970-01-01
  • 2016-05-11
相关资源
最近更新 更多