Search code examples
jquerytablesorter

Tablesorter math widget use data-*


I am using Mottie's Tablesorter (https://github.com/Mottie/tablesorter) and i am trying to use the Math widget on cells that are formatted like this;

<td data-number="50">Fifty</td>

However i cant seem to find out how to pass data-* through to arry on the math-complete option;

  math_complete : function($cell, wo, result, value, arry) {
    var txt = '<span class="align-decimal">' +
      ( value === wo.math_none ? '' : '$ ' ) +
      result + '</span>';
    if ($cell.attr('data-math') === 'all-sum') {
      // when the "all-sum" is processed, add a count to the end
      return txt + ' (Sum of ' + arry.length + ' cells)';
    }
    return txt;
  }

Also, I am using v2.18.2.

Documentation Link


Solution

  • The math widget is set up to get information from a data-attribute set by the textAttribute option. So if you want to use data-number, set that attribute to "data-number" (demo):

    $(function () {
        $('table').tablesorter({
            theme: 'blue',
            textAttribute: 'data-number',
            widgets: ['zebra', 'math']
        });
    });