DataTables v1.10 sorting by hidden column

核能气质少年 提交于 2019-12-13 00:20:11

问题


After migrating to v1.10 sorting by hidden column stopped working.

Fiddler Example v1.10 http://jsfiddle.net/0rstgd4f/

var dataTableInfo = $("#dataTable1").DataTable(
    {
        "initComplete": function(settings, json) 
			{
			    settings.aoColumns[0].iDataSort = 1;		
		    }
    });
<table id="dataTable1">
    <thead>
        <tr>
            <th>
                Column1
            </th>
            <th style="display:none;">
                Column2
            </th>
            <th>
                Column3
            </th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>
                1
            </td>
            <td style="display:none;">
                1
            </td>
            <td>
                a
            </td>
        </tr>
        <tr>
            <td>
                2
            </td>
            <td style="display:none;">
                2
            </td>
            <td>
                b
            </td>
        </tr>
        <tr>
            <td>
                3
            </td>
            <td style="display:none;">
                1
            </td>
            <td>
                c
            </td>
        </tr>
    </tbody>
</table>

v.1.8.2 http://jsfiddle.net/rzzrbwb0/

Columns should be sorted as: 1 3 2

or 2 1 3

I tried to use new definition for initComplete as columns(), column() and so on but it did not work.

Any ideas and suggestions are welcome.


回答1:


I don't think initComlete is the correct place to change DataTables behavior, at least for 1.10. From the manual:

DataTables stores the configuration and data for each table in a settings object. The structure of this object is considered to be private in terms of the DataTables API and reading or writing to the values in the object is not supported. The property names and values contained within the object can, will and do change between versions!

If you're using DataTables 1.10, it should be defined using columns.orderData or columnDefs.orderData, see the example below:

var dataTableInfo = $("#dataTable1").DataTable({   
   "columnDefs": [
      { "orderData": 1, "targets": [ 0 ] }
   ]        
});

See this JSFiddle for demonstration.



来源:https://stackoverflow.com/questions/30538878/datatables-v1-10-sorting-by-hidden-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!