2012-10-02 65 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' } 
     } 
    }); 
});