Search code examples
jqueryarraystablesorter

jQuery.tablesorter.addParser is function usage


There is no clear information about how to use the is: function(s, table, cell, $cell) {} as I am intented to use however I did not get a comprehended knowledge from the documentary.

I have a block of code as displayed below. The function purpose is to fill an array with the column data after that in textsorter use it to sort the array data. however when I click on the second column, the column which uses the addparser does not sort properly anymore. So it has to do with addParser function so I thought that I may fix it by using is function.

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"
 ]

Solution

  • The is function and parameters are described on this page: https://mottie.github.io/tablesorter/docs/example-parsers.html

    Essentially, it is only used to auto-detect if the parser should be used on a specific column.

    • If you want it to auto-detect, then only return true if the cell string (s), cell property or attribute matches your criteria.
    • If you already know what data is in your table, specifically which columns you want to have a custom parser, then set the is function to always return false; then set the header (th in the thead) class name to sorter-{parser.id} (see https://mottie.github.io/tablesorter/docs/example-option-built-in-parsers.html)

    In the above example code, I'm guessing you're sorting ipv4 addresses? Check out https://mottie.github.io/tablesorter/docs/example-parsers-ip-address.html - the parser itself should work with any length of decimal point separated values, e.g. 1.2.3.4.5.6.7.99


    Update: I forgot that the "ipAddress" parser doesn't work as expected if the number of sections aren't consistent; e.g. it will work if all cells in the column have 4 sections "1.1.1.1" (numbers separated by decimal points)

    Here is a modified version that will sort as you expect - 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'
         }
       }
     });