Filtering a recordset

Filtering a recordset

rmawanirmawani Posts: 6Questions: 1Answers: 1
edited March 2017 in Free community support

Hi,

First off, thank you so very much for the great plug-in!

I'm trying to get a handle on filtering and am having some trouble figuring it out.

My code is as follows:

$(document).ready(function() {
    var table = $('#example').DataTable({
        columnDefs: [{
            targets: [2, 4, 5],
            visible: false
        }, {
            targets: 1,
            width: '30%'
        }]
    });

    var filteredAge = table
        .column(3)
        .data()
        .filter(function(value, index) {
            return value > 40 ? true : false;
        });
    console.log(filteredAge);

    var filteredData = table
        .columns([0, 1, 2, 3, 4, 5])
        .data()
        .eq(3)
        .filter(function(value, index) {
            return value > 40 ? true : false;
        });
    console.log(filteredData);
});

As you will see from the following Fiddle, the first filter on a single column works fine and logs the correct values to the console, as expected. However, the second filter returns a "Uncaught TypeError: Cannot read property 'filter' of null" error to the console. For the life of me, I just cannot figure out the cause.

Is my syntax wrong or did I miss something important somewhere?

Thanks in advance!

R

This question has an accepted answers - jump to answer

Answers

  • rmawanirmawani Posts: 6Questions: 1Answers: 1
    edited March 2017

    After doing a little more research on the issue I'm having as well as re-reading the docs for eq(), I believe I have found a good solution for my needs.

    Please take a look at the updated Fiddle with my workaround. Having said this, however, I would still love to get a better understanding as to why my original filter() based solution would not work.

    Do you see any issues with this way of filtering the original recordset and redrawing the DataTable with the newly derived (filtered) data?

    Thanks again!
    Riz

  • kthorngrenkthorngren Posts: 20,346Questions: 26Answers: 4,776
    Answer ✓

    Take a look at this thread:
    https://datatables.net/forums/discussion/40981/filtering-multiple-columns

    Your question sparked my interest and that thread. Hope it helps.

    Kevin

  • rmawanirmawani Posts: 6Questions: 1Answers: 1
    edited March 2017

    Just went through it a few hours ago and then again just now to see Allan's answer.

    Found a workaround to get what I want but was also expecting the filter() call to return whatever columns were being filtered using the columns() call.

    Thanks for the link to your thread though. Helped me to better understand what eq() is actually supposed to do in the context of filter().

    Riz

This discussion has been closed.