1

我在基于 Django 的网站中使用基于 jQuery 的Tablesorter库。我试图让表中的“Pos”列根据我在 Parser 函数中设置的自定义顺序进行排序。我试过关注几篇文章(包括这篇文章和另一篇文章),但它仍然只按字母顺序排序。

它的行为方式...

  • 在页面加载时,单击 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>

任何帮助将不胜感激!

4

1 回答 1

1

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

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() 调用查找大写值时,没有找到!

于 2019-03-20T10:56:00.860 回答