dongxuan1660 2014-09-23 07:49 采纳率: 100%
浏览 51
已采纳

JQuery Tablesorter:emptyTo:'bottom'不适用于自定义解析器

I'm using JQuery Tablesorter version 2.17.8 to sort a table
I've created a custom parser to sort a date, followed by a time, followed by an optional string.

The parser looks like this:

$.tablesorter.addParser({
id: "datetime",
is: function (s) {
    return /\d{2}\-\d{2}\-\d{2}\s\d{2}:\d{2}(door \w+)?/.test(s);
}, format: function (s) {
    arr = s.split(/[\s\-:]/);
    if(arr[0] == "") // table column is empty
        timestamp = null; 
    else {
        // Month is zero-indexed so subtract one from the month inside the constructor
        date = new Date(Date.UTC('20' + arr[2], arr[1] - 1, arr[0], arr[3], arr[4])); // Y M D H M
        timestamp = date.getTime();
    }
    console.log(timestamp);
    return timestamp;
}, type: "numeric"
});

Example data of the table column to be sorted:

  • 30-04-14 09:55
  • 30-04-14 14:11 door I1
  • Empty cell

The parser works as intended but I want the empty cells to be sorted to the bottom and that doesn't seem to work.

I placed this in my PHP file:

<script type="text/javascript">
{literal}
$(document).ready(function() 
{ 
    $("#betalingen").tablesorter( { 
        headers: { 
            5 : { sorter: false } },        // disable sorting for column #5
        widgets: ["saveSort", "zebra"],     // apply alternating row coloring
        sortList: [[2,0]],                  // initial sorting order for Date column = ascending (0)
        emptyTo: 'bottom'                   // empty cells are always at the bottom
        } 
    ); 
} );
{/literal}
</script>

Note that I've tried every option here: http://mottie.github.io/tablesorter/docs/example-option-sort-empty.html
And when I remove my custom parser (and let Tablesorter figure out a parser), it sorts the empty cells to the bottom as intended but obviously the column isn't sorted correctly.

Anyone knows what is going on here?

  • 写回答

1条回答 默认 最新

  • dongxin991209 2014-09-23 20:51
    关注

    You'll need to tweak you parser slightly.

    • You don't really need an is function since you can set the parser to a column and not need to auto-detect it.
    • As @karlingen pointed out, when the cell is empty, you're still returning timestamp which has been set to null; it gets saved as null and not an empty string.

      $.tablesorter.addParser({
          id: "datetime",
          is: function (s) {
              // no need to auto-detect
              return false;
          },
          format: function (s) {
              var date, timestamp,
                  arr = s.split(/[\s\-:]/);
              if (arr[0] !== "") {
                  // Month is zero-indexed so subtract one from the month inside the constructor
                  date = new Date(Date.UTC('20' + arr[2], arr[1] - 1, arr[0], arr[3], arr[4])); // Y M D H M
                  timestamp = date.getTime();
              }
              // is date really a date?
              return date instanceof Date && isFinite(date) ? timestamp : s;
          },
          type: "numeric"
      });
      
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度