jQuery.tablesorter.addParser是函数用法

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

[我没有打算使用is: function(s, table, cell, $cell) {}的明确信息,但是我没有从纪录片中获得了解的知识。

我有如下所示的代码块。该功能的目的是在文本排序器中使用列数据对数组数据进行排序之后,用列数据填充数组。但是,当我单击第二列时,使用addparser的列不再正确排序。因此,它与addParser函数有关,因此我认为我可以使用is函数来修复它。

jQuery.tablesorter.addParser({
                        id: "parser",
                        is: function(s, table, cell, $cell) {
                            console.log("s",s,"table", table,"cell", cell, "$cell",$cell)
                            // s is the text from the cell
                            // table is the current table (as a DOM element; not jQuery object)
                            // cell is the current table cell (DOM element)
                            // $cell is the current table cell (jQuery object; added v2.18.0)
                            // return false if you don't want this parser to be auto detected
                            return true;
                        },
                        parsed: true,
                        format: function(aStr) {
                            console.log("aStr", aStr)

                            if (aStr.includes(".")) {
                            array_.push(aStr);

                            } else if (aStr.includes("-")) {
                            var result2 = aStr.replace(/-/g, ".");
                            console.log("before reverse array", result2)
                            result2 = result2.split(".");
                            result2 = result2.reverse();
                            result2 = result2.join(".");
                            console.log("reversed array", result2)
                            array_.push(result2);

                            } else {
                            array_.push(aStr);
                            }

                            return array_;
                        },
                        type: "text"
                });
, headers : {
                    1: {
                        sorter: "parser"
                    },
          }
, textSorter : {

                        1:  function(aStr, bStr) {
                            <cfif isdefined("attributes.id") and attributes.id eq 0.1>
                                var value = "";
                                var previousValue = "";
                                console.log("array_",array_);

                                 for (var i = 0; i < array_.length; i++) {
                                    value = array_[i];
                                    previousValue = array_[i-1];
                                    // console.log("value",value, "previousValue", previousValue);

                                 }

                                 return value > previousValue ? 1 : value < previousValue ? -1 : 0;
                            </cfif>
                             },
          }
Sample data: 
var arr = [
   "1",
   "1",
  "1.1.1",
  "1.1.1.1",
  "1.1.1.2",
  "1.1.2",
  "1.1.3",
  "1.1.4",
  "1.1.5",
  "1.1.6",
  "1.1.7",
  "1.1.7.1",
  "1.1.7.2",
  "1.1.7.3",
  "1.1.8",
  "1.1.8.1",
  "1.1.9",
  "1.1.10",
  "1.1.11",
  "1.1.11.1",
  "1.1.11.2",
  "1.1.11.3",
  "1.1.11.4",
  "1.1.11.5",
  "1.1.11.6",
  "1.1.11.7",
  "1.1.11.8",
  "1.1.11.9",
  "1.1.12",
  "1.1.13",
  "1.1.13.1",
  "1.1.13.2",
  "1.1.13.3",
  "1.1.14",
  "1.1.15",
  "1.1.15.1",
  "1.1.15.2",

  "1.2",
  "1.2.1",
  "1.2.2",
  "1.2.3",
  "1.2.3.1",
  "1.2.3.2",
  "1.2.3.3",
  "1.2.3.4",
  "1.2.3.5",
  "1.2.3.6",
  "1.2.3.7",
  "1.2.4",
  "1.2.5",
  "1.2.6",
  "1.2.7",
  "1.3.1",
  "1.3.2",
  "2",
  "2.1",
  "1.3"
 ]
jquery arrays tablesorter
1个回答
1
投票

is功能和参数在本页上描述:https://mottie.github.io/tablesorter/docs/example-parsers.html

基本上,它仅用于自动检测是否应在特定列上使用解析器。

  • 如果要自动检测,则如果单元格字符串(true),单元格属性或属性符合您的条件,则返回s
  • 如果您已经知道表中有什么数据,特别是要拥有自定义解析器的列,则将is函数设置为always返回false;然后将标题(th中的thead)类名称设置为sorter-{parser.id}(请参见https://mottie.github.io/tablesorter/docs/example-option-built-in-parsers.html

在上面的示例代码中,我猜您正在对ipv4地址进行排序?签出https://mottie.github.io/tablesorter/docs/example-parsers-ip-address.html-解析器本身应可以使用任意长度的小数点分隔值,例如1.2.3.4.5.6.7.99


更新:我忘记了如果节数不一致,“ ipAddress”解析器将无法正常工作;例如如果该列中的所有单元格都有4个部分“ 1.1.1.1”(数字用小数点分隔),它将起作用

这里是修改后的版本,将按您的期望排序-demo

 var maxSections = 4; // e.g. "1.1.1.1" = 4; "1.1.1.1.1" = 5
 var maxCharsPerSection = 3; // e.g. "1.1.1.999" = 3; "1.1.1.9999" = 4

 // Don't modify anything below
 var filler = new Array(maxCharsPerSection).fill('0').join('');

 $.tablesorter.addParser({
   id: 'decimal-separated',
   is: function() {
     return false;
   },
   format: function(s) {
     var i = 0,
       a = s ? s.split('.') : [],
       r = [];
     for (i = 0; i < maxSections; i++) {
       r.push((filler + (a[i] || '0')).slice(-maxCharsPerSection));
     }
     return s ? r.join('.') : s;
   },
   type: 'text'
 });


 $(".big_list").tablesorter({
   sortList: [
     [0, 0]
   ],
   headers: {
     0: {
       sorter: 'decimal-separated'
     }
   }
 });
© www.soinside.com 2019 - 2024. All rights reserved.