基于列索引和更改列数的 DataTable 默认排序问题

Issue with DataTable default ordering based on column index and changing number of columns

我正在尝试找出一种方法来解决 Repeater 中 DataTable 集的问题。根据查看页面的用户,列数会发生变化。例如,User1 将看到 8 列,而 user2 将在 table.

中看到 7 列

Datatables 让我选择 table 的默认排序,并具有排序功能:

var table = $('#example'String).DataTable();

// Sort by column 8 and then re-draw

table
    .order( [ 8, 'asc' ] )

问题是每当我为 User2 更改第一列的可见性时:

column0.Visible = False

...列数发生变化,因此进行排序的正确索引已从索引 8 更改为索引 7。

我尝试使用 <td> ID 的默认排序,但它只接受 Colummn Index

我尝试过使用负索引从右开始,但它不起作用。

我还尝试找到一种方法来手动对 table 列进行硬编码,而不更改最终用户的顺序。

如果我可以更改列的顺序会很简单,但它们需要在屏幕上按特定顺序显示。

任何熟悉数据table的人都可以帮助我解决这个问题。将不胜感激 :)

我看不到您的数据表初始化代码,但我通过在初始化中命名列然后使用它来查找列索引来完成此操作:

在初始化中:

table = $("table").DataTable({
           "columns": [
                    {"name": "Row1"}
                    {"name": "Row2"}
                     ]
         });

然后,你可以这样得到行的索引:

var row1Index = table.column('Row1:name').index();

并使用索引进行过滤:

table.order([row1Index, 'asc']);

它应该允许你的索引是基于初始化的变量。

在此处查看名称文档:https://datatables.net/reference/option/columns.name