Dance Along
Dance Along

Reputation: 41

JQuery Tablesorter special date

I have a table storing term information like these:

WINT 2018, SPR 2017,....

I want to sort the term column by addParser. The way I refer to is here: https://mottie.github.io/tablesorter/docs/example-parsers.html

My Plan is to change the term information to date, and sort the date.

My code is here:

$(function(){
$.tablesorter.addParser({
  // set a unique id
   id: 'term',
  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(/wint /, /Jan 01, /)
      .replace(/sumr /, /Jul 01, /)
      .replace(/spr /, /Mar 01, /)
     .replace(/fall /, /Sep 01, /);
  },

  type: 'date'
}); 
    $(".tablesorter").tablesorter({
    headers: {      
      1: { sorter: "term" }
    }
    }
    );

});

The result is that the term column is frozen, while other columns are good.

My concerns are 2 points, 1. Is the replace format right? I could not find a replace example online. 2. Is the type : 'date' correct? Should it be text or numeric only?

Have anyone had similar experience? Any hint is appreciated!

Upvotes: 1

Views: 22

Answers (1)

Dance Along
Dance Along

Reputation: 41

I found the type is only for numeric and text. I might be wrong, but I stick to the numeric and give up changing it to date.

The code is here, and it works for me.

$.tablesorter.addParser({
  // set a unique id
  id: 'term',
  is: function(s) {
    // return false so this parser is not auto detected
    return false;
  },
  format: function(s) {
    // format your data for normalization
    text = s.split(' '); 
    s=text[1] + text[0];
   return s.toLowerCase()
      .replace(/wint/, 1)
      .replace(/sumr/, 7)
      .replace(/spr/, 3)
     .replace(/fall/, 9);
  },
  // set type, either numeric or text
  type: 'numeric'
});         

    $(".tablesorter").tablesorter({
    headers: {

      1: { sorter: "term" }
    }
    }
    );    

Thank you!

Upvotes: 1

Related Questions