Jquery DataTables 在排序时将顺序更改为 desc
我正在使用 DataTables 来显示一些数据,它效果很好,但我想稍微自定义它,但不确定如何。
我想要做的是,当用户单击列标题对该列进行排序时,我希望它最初按降序而不是升序排序。有什么办法可以做到这一点吗?
I am using DataTables to display some data and it works great but I want to customize it slightly and not sure how.
What I want to do is when a user clicks on a column heading to sort that column I want it to initially order descendingly rather than ascendingly. Is there any way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
看看这个: DataTables 排序方向控制示例
您可以执行以下操作:
Have a look at this: DataTables sorting direction control example
You can do something like:
当前版本的 DataTables (1.10) 提供了以下方法来切换此默认排序顺序,其中使用
columnDefs
下的属性orderSequence
(或columns
但更少)灵活的)。以下是有关
orderSequence
的文档。正如它还提到的,您可以强制列仅在单击时按 DESC 或 ASC 进行排序,您的界面可能会从中受益。
就我而言,我需要在初始单击时对不确定数量的列进行降序排序,因此我决定切换示例以定位列标题的
class
名称,而不是将每列显式定义为 < code>"targets":[1],"targets":[2],...[n] 或以编程方式构建我关心的列索引数组。您可以通过多种方式来定位列根据此处。
最终结果是一个像这样的表定义:
数据表的授权如下:
瞧!首先,对带有“descendFirst”类(任意选择的描述性类名)标记的
的所有列单击降序排序。
The current version of DataTables (1.10) provides the following way of switching this default sorting order with the property
orderSequence
undercolumnDefs
(orcolumns
but less flexible).Here's the documentation on
orderSequence
.As it also mentions, you may force a column to only sort when clicked as DESC or ASC which your interface may very well benefit from.
In my case, I needed to have columns descending their sort on initial click for an indeterminate number of columns so I decided to switch the example to target a column header's
class
name rather than explicitly defining each column as"targets":[1],"targets":[2],...[n]
or programatically building an array of the indices of columns I cared about.You can target columns multiple ways according to here.
The end result is a table definition like so:
And Data Table empowered as such:
Voila! First click descending sort on all columns with a
<th>
marked with a class of 'descendFirst' (an arbitrarily chosen, descriptive class name).为了回应最后对空白进行排序,这就是我想到的--
(我只是讨厌首先对空白进行排序!!)
包含这些自定义排序函数
将排序标签应用到适当的列
In response to sorting blanks last, here's what I came up with--
(I just HATE blanks sorting first!!)
Include these custom sort functions
Apply the sort tags to the appropriate columns
如果像戴夫和我这样的其他人正在寻找一种在所有列上设置排序顺序的方法,那么以下内容可能适合您。为了更改所有列的排序顺序,我设置了一个循环来在表实例化后更改设置:
希望有帮助。在 jQuery 1.11.0 和 DataTables 1.10.0 上测试
If anyone else like Dave and myself is looking for a way to set the sort order on all columns, the following may work for you. In order to change the sorting order on all of the columns I set up a loop to alter the settings after the table had instantiated:
Hope that helps. Tested on jQuery 1.11.0 and DataTables 1.10.0
下面的代码是先设置默认排序顺序 desc,然后对所有列设置升序。
Below code is to set default sorting order desc first then asceding for all columns.
最后获得空白的唯一方法是进行一些修改(因为排序工作正常)。
不要从服务器返回空白值,而是返回类似以下内容的内容:“[空白]”
我还没有测试过它,但我很确定方括号会出现在字母数字代码之后。方括号传统上也象征着尚未完成或确认的事情。
The only way to get your blank ones last would be a bit of a hack (since the sort is working correctly).
Instead of returning blank values from your server, return something like: "[Blank]"
I haven't tested it, but I'm pretty sure square brackets will come after alphanumeric codes. Also square brackets traditionally symbolises something that hasn't been completed or confirmed yet.
这对我有用:
This works for me: