我使用jQuery Lazy plugin来延迟加载JS和CSS资源。我还使用了TableSorter jQuery plugin来使表格可排序。现在,当我简单地在Lazy插件中定义Tablesorter,然后像这样使用Tablesorter时,这两个工具就可以很好地工作了。
$('#table_id').tablesorter();如上所示,Tablesorter库按需加载,HTML表变得可排序。但是,我需要将自定义解析器添加到表排序中,如下所示:
$.tablesorter.addParser({
id: 'my_parser',
is: function(s) {
return false;
},
format: function(s) {
var result = s.match(/([0-9]+)/);
if (result == null) return 0;
return parseInt(result[0]);
},
type: 'numeric'
}); 上面addParser代码的问题是,我得到的错误是"$.tablesorter没有‘addParser’方法“。无论TableSorter插件是否已经加载,都没有区别。懒惰插件用它自己的函数覆盖了$.tablesorter,并且不允许这种调用--我想。
此behaviour can be seen here in this jsFiddle的示例。
有人能帮我吗?我将非常感谢任何人的帮助或想法。提前谢谢你。
发布于 2011-12-18 18:23:03
为了证明我的观点,yepnope.js可以轻松地完成您需要的工作(例如,如果周围有任何可排序的表,则有条件地加载表排序插件):
$(function () {
yepnope({
test : $("table.sortable").length,
yep : "/resources/jquery.tablesorter.js",
complete: function () {
$.tablesorter.addParser({
// whatever;
});
$("table.sortable").tablesorter();
}
});
});发布于 2011-12-18 18:22:49
你得到这个错误是很正常的。懒惰插件的工作方式是为您在选项中提供的name创建一个代理。
所以实际上$.tablesorter是(查看控制台):
$.tablesorter:
function proxy() {
var self = this;
arg = arguments;
if( $.lazy.archive[src].status === 'loaded' ) {
$.each(this,function(){
$(this)[name].apply(self,arg);
});
} else if ( $.lazy.archive[src].status === 'loading' ) {
$.lazy.archive[src].que.push({'name':name,'self':self,'arguments':arg});
...如果该插件已经加载到$.lazy.archive中,它会将其应用于调用tablesorter()的元素,否则它会先加载该插件,然后再应用它。
因此,您没有插件通常提供的原始名称空间,并且在您的上下文中不能使用$.tablesorter.addParser。
https://stackoverflow.com/questions/8550706
复制相似问题