javascript jQuery tablesorter:如何通过使用类而不是“内联 JSON”来禁用对列的排序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6627737/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery tablesorter: How to disable sorting on a column by using a class instead of "inline JSON"?
提问by Andrew
I am using the jQuery tablesorter plugin. I know how to disable sorting on a column by using the jQuery Metadata plugin:
我正在使用jQuery tablesorter 插件。我知道如何使用 jQuery 元数据插件禁用对列的排序:
<th class="{sorter: false}">Don't sort me</th>
But I would rather do this by setting a class, so I don't have to use an additional plugin. Also I think I would remember the class name easier than remembering this JSON syntax. How can I do the same same thing using this syntax:
但我更愿意通过设置一个类来做到这一点,所以我不必使用额外的插件。另外我想我会比记住这个 JSON 语法更容易记住类名。我如何使用以下语法做同样的事情:
<th class="not-sortable">Don't sort me</th>
回答by Eric Petroelje
You shouldn't have to modify the source of the plugin. Assuming your th
class for not sorting is called nosort
:
您不应该修改插件的源代码。假设你的th
类不排序被称为nosort
:
function setupTablesorter() {
$('table.tablesorter').each(function (i, e) {
var myHeaders = {}
$(this).find('th.nosort').each(function (i, e) {
myHeaders[$(this).index()] = { sorter: false };
});
$(this).tablesorter({ widgets: ['zebra'], headers: myHeaders });
});
}
回答by benjamin.keen
I agree, inline JSON was weird. If you're using tablesorter v2.3 or later, you can actually use data roles to do the same thing - and not have to use the metadata plugin:
我同意,内联 JSON 很奇怪。如果您使用的是 tablesorter v2.3 或更高版本,您实际上可以使用数据角色来做同样的事情——而不必使用元数据插件:
<th data-sorter="false">...</th>
Much cleaner. Note, however, that it requires jQuery.
干净多了。但是请注意,它需要 jQuery。
For more info & a demo: http://mottie.github.io/tablesorter/docs/example-parsers-jquery-data.html
有关更多信息和演示:http: //mottie.github.io/tablesorter/docs/example-parsers-jquery-data.html
All the best!
一切顺利!
- Ben
- 本
回答by Arxisos
I think the only way to get this to work is modifying the source code of the plugin.
我认为让它工作的唯一方法是修改插件的源代码。
At jquery.tablesorter.js
, Line 483:
在jquery.tablesorter.js
,第 483 行:
function checkHeaderMetadata(cell) {
if (($.metadata) && ($(cell).metadata().sorter === false)) {
return true;
};
return false;
}
Change this code to:
将此代码更改为:
function checkHeaderMetadata(cell) {
if ((($.metadata) && ($(cell).metadata().sorter === false)) || $(cell).hasClass("not-sortable")) {
return true;
};
return false;
}
Now the function checkHeaderMetadata
is also returning true, if the cell has a class named not-sortable
.
现在checkHeaderMetadata
,如果单元格有一个名为not-sortable
.
回答by toesslab
As of version 2.0.xyou can decide at initialisation, which column to notsort.
从2.0.x版本开始,您可以在初始化时决定不排序哪一列。
Add a property called headers
and foreach desired column the property sorter
with the value false
. Counting columns starts at 0
.
添加一个名为的属性,headers
并为每个所需的列添加sorter
值为的属性false
。计数列从 开始0
。
jQuery('#element').tablesorter({
headers: {
// first column
0: {
sorter: false
},
// third column
2: {
sorter: false
}
}
});
Example taken from the docs.
从文档中获取的示例。