I have a function that I'm testing, it is supposed to check all table headers to see if it has a particular class, remove it and replace it with another class. I'm using this to switch metadata parsers for the tablesorter plugin. The code I have so far is below:
我有一个我正在测试的函数,它应该检查所有表头是否有特定的类,删除它并用另一个类替换它。我正在使用它来切换tablesorter插件的元数据解析器。我到目前为止的代码如下:
$(function(){
$("th").each(function(){
if ($(this).hasClass("{sorter: 'usLongDate'}")) {
$(this).removeClass("{sorter: 'usLongDate'}").addClass("{sorter: 'false'}");
}
});
});
$(function() {
$("table").tablesorter({});
});
I'm trying to switch the class parser in the following header to the false inline parser.
我正在尝试将以下标头中的类解析器切换到false内联解析器。
<th class="{sorter: 'usLongDate'}"><a rel = "Header" href="#" title="Sort column in decending order" class="">Date</a></th>
Of course this isn't my only header, I'm just testing it on this one, I'm assuming the rest of the headers shouldn't be affected, tablesorter should still be sorting those. But when I run the html file, nothing works. Is there something I'm missing?? Thanks.
当然这不是我唯一的标题,我只是在这个上测试它,我假设其余的标题不应该受到影响,tablesorter仍然应该对它们进行排序。但是当我运行html文件时,没有任何作用。有什么我想念的吗?谢谢。
NOTE: Just to avoid confusion, the tablesorter plugin for jQuery can use inline parsers within the class attribute of a table header in order to determine how and/or whether a column should be sorted.
注意:为了避免混淆,jQuery的tablesorter插件可以在表头的class属性中使用内联解析器,以确定如何和/或是否应该对列进行排序。
UPDATE: Tried Vincent Robert's suggestion, unfortunately it doesn't work. In regards to whether or not addClass and removeClass can handle these. I have an original function that was able to add the disable parser to all classes,
更新:试过文森特罗伯特的建议,不幸的是它不起作用。关于addClass和removeClass是否可以处理这些。我有一个原始函数,能够将禁用解析器添加到所有类,
$("th[class='']").addClass("{sorter: 'false'}");
this comes from Paolo Bergantino, and it worked great. I'm just trying to extend it a bit.
这来自Paolo Bergantino,效果很好。我只想尝试扩展它。
4 个解决方案
#1
This is probably what you're looking for.
这可能就是你要找的东西。
$(document).ready(function() {
$("th[class*='usLongDate']").each(function(){
$(this).removeClass("{sorter: 'usLongDate'}").addClass("{sorter: false}");
});
$("table").tablesorter();
});
The issue probably stems from the fact that classes are separated by spaces. Technically, "{sorter:" and "'usLongDate'}" are two different classes. I don't know exactly how TableSorter is handling them, but removing "{sorter:" and then adding it back in should keep it next to "false}".
问题可能源于类被空格分隔的事实。从技术上讲,“{sorter:”和“'usLongDate'}”是两个不同的类。我不知道TableSorter究竟是如何处理它们的,但是删除“{sorter:”然后再添加它应该将它保持在“false}”旁边。
#2
Correct me if I'm wrong. But class="{sorter: 'usLongDate'}" is not a valid class name. Classes are names separated by spaces. class="class1 class2" means the element has two classes class1 and class2 not one class. Valid class names are explained here.
如我错了请纠正我。但class =“{sorter:'usLongDate'}”不是有效的类名。类是由空格分隔的名称。 class =“class1 class2”表示该元素有两个类class1和class2而不是一个类。这里解释了有效的类名。
EDIT
If you want to be able to change the class anyway use attr instead of add/remove class
如果你想能够改变类,那么使用attr而不是add / remove class
$(this).attr('class', 'myNewClass');
and to remove use:
并删除使用:
$(this).removeAttr('class');
#3
I don"t think addClass() or removeClass() can handle these kind of "classes". You should instead directly access the "class" attribute.
我不认为addClass()或removeClass()可以处理这类“类”。你应该直接访问“class”属性。
Now I am not familiar with this plugin...
现在我不熟悉这个插件......
$(function(){
$("th").each(function(){
if ($(this).attr("class") == "{sorter: 'usLongDate'}") {
$(this).attr("class", "{sorter: 'false'}");
}
});
});
EDIT
Just browsed the documentation and you could use the options way instead of the class way:
只是浏览文档,您可以使用选项方式而不是类方式:
$('table').tablesorter({
headers: $('th').map(function(i)
{
return { sorter: false /* or whatever you want based on $(this) content */ };
})
});
This should work as long as you are calling it before any tablesorter() call.
只要您在任何tablesorter()调用之前调用它,这应该可以工作。
#4
Should you be calling
你应该打电话吗?
$("table").tablesorter({});
Before trying to look at the classes? I'm assuming it's the plugin that is putting those class names in place ?
在尝试查看课程之前?我假设它是插入这些类名称的插件?
#1
This is probably what you're looking for.
这可能就是你要找的东西。
$(document).ready(function() {
$("th[class*='usLongDate']").each(function(){
$(this).removeClass("{sorter: 'usLongDate'}").addClass("{sorter: false}");
});
$("table").tablesorter();
});
The issue probably stems from the fact that classes are separated by spaces. Technically, "{sorter:" and "'usLongDate'}" are two different classes. I don't know exactly how TableSorter is handling them, but removing "{sorter:" and then adding it back in should keep it next to "false}".
问题可能源于类被空格分隔的事实。从技术上讲,“{sorter:”和“'usLongDate'}”是两个不同的类。我不知道TableSorter究竟是如何处理它们的,但是删除“{sorter:”然后再添加它应该将它保持在“false}”旁边。
#2
Correct me if I'm wrong. But class="{sorter: 'usLongDate'}" is not a valid class name. Classes are names separated by spaces. class="class1 class2" means the element has two classes class1 and class2 not one class. Valid class names are explained here.
如我错了请纠正我。但class =“{sorter:'usLongDate'}”不是有效的类名。类是由空格分隔的名称。 class =“class1 class2”表示该元素有两个类class1和class2而不是一个类。这里解释了有效的类名。
EDIT
If you want to be able to change the class anyway use attr instead of add/remove class
如果你想能够改变类,那么使用attr而不是add / remove class
$(this).attr('class', 'myNewClass');
and to remove use:
并删除使用:
$(this).removeAttr('class');
#3
I don"t think addClass() or removeClass() can handle these kind of "classes". You should instead directly access the "class" attribute.
我不认为addClass()或removeClass()可以处理这类“类”。你应该直接访问“class”属性。
Now I am not familiar with this plugin...
现在我不熟悉这个插件......
$(function(){
$("th").each(function(){
if ($(this).attr("class") == "{sorter: 'usLongDate'}") {
$(this).attr("class", "{sorter: 'false'}");
}
});
});
EDIT
Just browsed the documentation and you could use the options way instead of the class way:
只是浏览文档,您可以使用选项方式而不是类方式:
$('table').tablesorter({
headers: $('th').map(function(i)
{
return { sorter: false /* or whatever you want based on $(this) content */ };
})
});
This should work as long as you are calling it before any tablesorter() call.
只要您在任何tablesorter()调用之前调用它,这应该可以工作。
#4
Should you be calling
你应该打电话吗?
$("table").tablesorter({});
Before trying to look at the classes? I'm assuming it's the plugin that is putting those class names in place ?
在尝试查看课程之前?我假设它是插入这些类名称的插件?