2

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

glend
  • 1,592
  • 1
  • 17
  • 36
  • http://stackoverflow.com/questions/4187032/get-list-of-data-attributes-using-javascript-jquery – Mohamed-Yousef Nov 02 '15 at 14:49
  • I require that it is integrated into TableSorter because I wish to use the `math_event` option. This solution is just a general jQuery solution. – glend Nov 02 '15 at 15:54

1 Answers1

1

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']
    });
});
Mottie
  • 84,355
  • 30
  • 126
  • 241
  • What if you need to use `textAttribute` in some columns and the cell value in other columns? – glend Nov 04 '15 at 09:57
  • The math widget only grabs data-attribute values if it exists, so if you want to use the cell value, don't add a `data-number` attribute. Or are you saying that non-math cells would get their sort messed up with this method? – Mottie Nov 04 '15 at 13:56
  • I understand that, however, I have a case where I want to use the math widget on two different columns, `Fifty` and `20` it seems to work correctly if i set textAttribute to `data-number`, where `data-number` isn't available it uses the default instead. – glend Nov 04 '15 at 14:04