2012-10-02 70 views
1

我看到一个其他人有同样的问题(jquery tablesorter ajax table only sorting one direction),但它不是同一个原因。Mottie jQuery Tablesorter fork自定义排序只排序一个方向

使用jQuery的tablesorter(https://github.com/Mottie/tablesorter)的叉使用数据属性进行排序通过定制解析器的柱(法国日期/时间):

<td data-since="28-09-2012 15:41:10"> 
    <strong>4 jours, 16 minutes</strong> (28-09-2012 15:41:10) 
</td> 

我可排序ascendantly成功的列,但当我尝试再次单击列标题时,插件不会降级排序。

具有基本数据格式的其他列在两个方向上都正确排序。

下面是基于自定义的分析程序,美国商务部和其他计算器帖子:

$(document).ready(function() { 
    //https://stackoverflow.com/questions/9550354/jquery-tablesorter-plugin-secondary-hidden-sorting 
    $.tablesorter.addParser({ 
     // set a unique id 
     id: 'parseSinceColumn', 
     is: function(s) { 
      return /\d{1,2}-\d{1,2}-\d{1,4} \d{1,2}:\d{1,2}:\d{1,2}/.test(s); 
     }, 
     format: function(s, table, cell, cellIndex) { 
      // get data attributes from $(cell).attr('data-something'); 
      var cellDate = $(cell).attr('data-since'); 

      s = s.replace(/\-/g," "); 
      s = s.replace(/:/g," "); 
      s = s.split(" "); 

      return new Date(s[2], s[1]-1, s[0], s[3], s[4], s[5]).getTime(); 
     }, 
     // set type, either numeric or text 
     type: 'numeric' 
    }); 
    $("#pr-table").tablesorter({ 
     headers : { 
      3 : { sorter: 'parseSinceColumn' } 
     } 
    }); 
}); 

你有在途中的任何想法解决这个问题?

非常感谢。

编辑:

我觉得插件真的试图解决,但结果是一样的。

这里是插件的调试:

  • 首先排序,成功:

排序上3,1和dir 1次(8毫秒)

重建表( 3毫秒)

完成施加部件(0毫秒)

  • 第二排序,在排序没有changement:

排序上3,0和dir 0时间(7毫秒)

重建表(3毫秒)

完成施加部件(0毫秒)

回答

1

我终于找到了解决方案。

还有两个粗错误:

  • 我告诉的插件,该指数第3列分析器“parseSinceColumn”所以“是”功能应该返回false。事实上,单元格的内容不能因为“s”不是数据属性的内容,所以插件无法检测到对于该列

    • 格式功能使用好解析器匹配正则表达式'''参数来解析日期。好的变量是cellDate ...

这是最后的和fonctional片段:

$(document).ready(function() { 
    //http://stackoverflow.com/questions/9550354/jquery-tablesorter-plugin-secondary-hidden-sorting 
    $.tablesorter.addParser({ 
     // set a unique id 
     id: 'parseSinceColumn', 
     is: function(s) { 
      return false; 
     }, 
     format: function(s, table, cell, cellIndex) { 
      var cellDate = $(cell).attr('data-since'); 
      // get data attributes from $(cell).attr('data-something'); 
      // check specific column using cellIndex 
      cellDate = cellDate.replace(/\-/g," "); 
      cellDate = cellDate.replace(/:/g," "); 
      cellDate = cellDate.split(" "); 

      return new Date(cellDate[2], cellDate[1]-1, cellDate[0], cellDate[3], cellDate[4], cellDate[5]).getTime(); 
     }, 
     // set type, either numeric or text 
     type: 'numeric' 
    }); 
    $("#pr-table").tablesorter({ 
     headers : { 
      3 : { sorter: 'parseSinceColumn' } 
     } 
    }); 
});