Search code examples
htmlhtml-tabletablesorter

Date sorting issue if a extra budge digit using eg. "02/13/2017 0"


I am using tablesorter plugin for sorting my table. I have a date column and with the date I have budge digit for example

"02/13/2017 0"

I want to sort this only according to date. But it's not working. I need quick suggestion for this question. Here is some html for that:

<td role="gridcell">
  <div class="slds-truncate">
    <span class="uiOutputDate">02/13/2017</span>
    <span class="badge2">0</span>
  </div>
</td>

Solution

  • You have two choices:

    1. Use the extractMMDDYYYY parser available in the parser-date-extract.js file (demo)

      <th class="sorter-extractMMDDYYYY">Date</th>
      
    2. Use the textExtraction callback to target the element containing the date (demo)

      $(function() {
        $('table').tablesorter({debug: true,
          theme: 'blue',
          textExtraction: {
            0: function(node) {
              return $(node).find('.uiOutputDate').text() || $(node).text();
            }
          },
          widgets: ['zebra'],
        });
      });
      

      * Note that the textExtraction function in the original version of tablesorter (v2.0.5b) only supports a single function (ref).