Jquery tablesorter generates error in custom parser for type text when input is numeric

StackOverflow https://stackoverflow.com/questions/20710512

  •  20-09-2022
  •  | 
  •  

سؤال

I have a simple tablesorter in which I have written my own parser for a column. On the PhP side, I generate a data attribute whose value is used as the sort value for the cell. My problem is that in the custom parser, when I return the Data-... value, it gives me an error on an integer value, when type specified for the custom parser is text. One would normally expect such errors the other way around. The error on the console generated by tablesorter.js is

        Uncaught TypeError: Object 11 has no method 'replace' 

Where 11 is the numeric value assigned to data-skaddress attribute of the cell.

 $.tablesorter.addParser({
    id: 'skaddress',
    is: function(s) {
      return false;
    },
    format: function(s, table, cell, cellIndex) {
      return $(cell).data('skaddress');  
    },
    type: 'text'  // <<<<-------- If this is changed to 'numeric', all is well
 });

In other words, if I have a numeric value assigned to data-skaddress attibute of the cell it fails when I think that 'text' type simply means that any string, including all digits, would be considered a text field. The HTML markup of the cell generating the error is:

        <td  data-ssipaddress="11">12.29.14.97.190.111</td>

Any way around this? Spent a lot of time trying to find a real solution. Of course, there are workarounds. I can, for example, always generate a fixed alphabet before the long numeric string on the PhP side. The reason that I need a text field and not a numeric field is because my strings consist of long series of digits though in the example I am using only two digit strings.

Please do not ask me to use Datatables instead. I like tablesorter (the one maintained by Mottie?) as it is light weight and great for what I need and infact I think better than Datatables in some aspect. It took me some time to get a hang of it. Just this unexpected problem...

Update: It works if a leading zero is placed before the string of digits assigned to data- attribute!

Thanks

هل كانت مفيدة؟

المحلول

There is a difference between 555 and "555".

You must make sure that your value is a string when you pass it. You can assure that by:

foo = foo + "";

if foo is your value.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top