Search code examples
jquerytablesorter

What is the different between textExtraction and addParser on jQuery Tablesorter


I don't understand what is the different, I am trying to figure out which one is in the end who know what is the text or whatever for sorting.


Solution

  • When tablesorter is initialized, it uses the textExtraction function to extract the content from the table. For example, if your table cell has this markup:

    <tr>
        <td><span class="value hidden">42</span> Forty-Two</td>
        <td>Fred</td>
    </tr>
    

    The textExtraction function can be set to target specific text within a table cell.

    textExtraction: function(){
        1: function(node, table, cellIndex) {
            return $(node).find('.value').text();
        }
    }
    

    A parser on the other hand takes the text from the textExtraction function and manipulates it so it is easier to sort. For example, if we had this table markup:

    <tr>
        <td>Fred</td>
        <td>1/31/2014</td>
    </tr>
    

    A date parser (the shortDate parser in this case) will take that date "1/31/2014" and convert it into a number of milliseconds:

    new Date("1/31/2014").getTime() // result: 1391148000000
    

    this number is much easier to sort and compare.

    Of course, there is some overlap between the two, because we can make the textExtraction function do this conversion as well; but internally, any value that is returned from the textExtraction function is trimmed (using jQuery $.trim()) and the result is always returned as a string. So the result would not be the exactly the same as it would be from a parser.

    I hope that answers your question.