0

I have problem with datatables. Until now, I use them with a lot of data. I have build two checkboxes for negative values and positive values like this:

<label id="mf123_filter">
        <input type="checkbox" id="filterP">Positive Growth
      </label>
      <br>
      <br>
      <label id="mf123_filter">
        <input type="checkbox" id="filterN">Negative Growth
      </label>

On JS I implement an on.change function to exclude or include negative or positive values. The code is here:

$('#filterP').on('change', function () {
                                if ($(this).is(':checked')) {
                                    $.fn.dataTable.ext.search.push(
                                        function (settings, data, dataIndex) {
                                            return parseFloat(data[-2]) > 0 //PROBLEM
                                        }
                                    )
                                } else {
                                    $.fn.dataTable.ext.search.pop();
                                }
                                graph2Table.draw();
                            });

My problem is that datatables does not recognize the negative index (data[-2]). Is there any way to target the second column from the end?

  • 1
    Replace `data[-2]` with `data[data.length -2]`. Be careful, though - this assumes the `data` array actually has 2 or more items in it. See also [Get the last item in an array](https://stackoverflow.com/questions/3216013/get-the-last-item-in-an-array) for more discussion and alternatives. – andrewJames Jan 21 '21 at 14:48
  • Yes, it worked!! Thanks :) – Nick Zagkanas Jan 21 '21 at 16:52

0 Answers0