jQuery Tablesorter自定义排序不起作用

问题描述 投票:1回答:1

我在基于Django的网站上使用基于jQuery的Tablesorter库。我正试图让我的表中的“Pos”列按照我在Parser函数中设置的自定义顺序进行排序。我曾尝试过几篇文章(包括this oneanother one),但它仍然按字母顺序排序。

它的行为方式......

  • 在页面加载时,单击Pos列不会更改顺序,但它会循环显示小的向上/向下图标
  • 按名称列(有效)进行排序,然后单击Pos将其设置回页面加载时的排序方式

JS

<script>
  $(document).ready(function() {
    //Disable sorting on specified columns
    $("#fullTable thead th:eq(3)").data("sorter", false); //Monday
    $("#fullTable thead th:eq(4)").data("sorter", false); //Tuesday
    $("#fullTable thead th:eq(5)").data("sorter", false); //Wednesday
    $("#fullTable thead th:eq(6)").data("sorter", false); //you get the idea...
    $("#fullTable thead th:eq(7)").data("sorter", false);
    $("#fullTable thead th:eq(8)").data("sorter", false);
    $("#fullTable thead th:eq(9)").data("sorter", false);
    $("#fullTable thead th:eq(10)").data("sorter", false); //Sunday

    $.tablesorter.addParser({
      // set a unique id
      id: 'positions',
      is: function(s) {
          // return false so this parser is not auto detected
          return false;
      },
      format: function(s) {
          // format your data for normalization
          return s.toLowerCase()
              .replace(/A/, 0)
              .replace(/E2/, 1)
              .replace(/E1/, 2)
              .replace(/C2/, 3)
              .replace(/C1/, 4)
              .replace(/SC/, 5)
              .replace(/PC/, 6)
              .replace(/AD/, 7);
      },
      // set type, either numeric or text
      type: 'numeric'
    });

    $("#fullTable").tablesorter({
      cancelSelection: true,
      sortReset: true
    });
  });
</script>

HTML

<div class="mainTableContainer" style="overflow-x:scroll; overflow-y:hidden; width:fit-content; width:-moz-fit-content; height:auto; display:inline-block;">
      <table id='fullTable' class="newtable tablesorter" border="1" alt="Staff Entry table" > <!--PABSO-278-->
        <thead style="border: 1px solid black">
          <tr>
            <th><div style="width:100px">Week</div></th>
            <th><div style="width:200px">Name</div></th>
            <th class="sorter-positions"><div style="width:50px">Pos</div></th>
            <th><div style="width:200px">Monday</div></th>
            <th><div style="width:200px">Tuesday</div></th>
            <th><div style="width:200px">Wednesday</div></th>
            <th><div style="width:200px">Thursday</div></th>
            <th><div style="width:200px">Friday</div></th>
            <th><div style="width:200px">Saturday</div></th>
            <th><div style="width:200px">Sunday</div></th>
          </tr>
        </thead>

任何帮助都将非常感谢!

javascript jquery tablesorter
1个回答
0
投票

一位同事指出,这是我的一个愚蠢的错误:

format: function(s) {
      // format your data for normalization
      return s.toLowerCase()
          .replace(/A/, 0)
          .replace(/E2/, 1)
          .replace(/E1/, 2)
          .replace(/C2/, 3)
          .replace(/C1/, 4)
          .replace(/SC/, 5)
          .replace(/PC/, 6)
          .replace(/AD/, 7);
  },

我正在解析我的表数据.toLowerCase()。因此,当.replace()调用查找大写值时,找不到任何内容!

© www.soinside.com 2019 - 2024. All rights reserved.