我正在基于Django的网站中使用基于jQuery的Tablesorter库。我正在尝试获取表格中的“ Pos”列,以根据我在Parser函数中设置的自定义顺序进行排序。我尝试过阅读几篇文章(包括this one和another one),但它仍然仅按字母顺序排序。
行为方式...
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>
任何帮助都将不胜感激!
答案 0 :(得分: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()调用以查找大写值时,找不到任何内容!