Question about api.column().data().filter().count(): help needed

Question about api.column().data().filter().count(): help needed

GargiucnGargiucn Posts: 104Questions: 29Answers: 0

In the following script:

let testo2ab = api
                .column( 14 )
                .data()
                .filter( function ( value, index ) {
                    return value == 0 ? true : false;
                } )
                .count();
            if ( testo2ab != 0 ) {
                api.column(4).visible( false );
            }       

count() is executed for all rows from the database.
is it possible to use a filter to be able to count only rows that olso have a certain value (ex: id = 1)?
I thank you for your support,
Giuseppe

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 20,255Questions: 26Answers: 4,761

    I tried your code snippet here:
    https://live.datatables.net/fujuputi/1/edit

    Adjusted like this to work with the data in the table:

    let testo2ab = table
                    .column( 1 )
                    .data()
                    .filter( function ( value, index ) {
                        return value == 'Director' ? true : false;
                    } )
                    .count();
    
    alert('Number of Directors: ' + testo2ab);
    

    The code works and results in only 4 Directors found. Maybe I'm not understanding your question.

    Please provide a link to your page or a test case, update mine if you want, replicating the issue so we can help debug.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • GargiucnGargiucn Posts: 104Questions: 29Answers: 0

    Referring to your example, it's as if I would like to find the number of Directors who have offices in London...

    Giuseppe

  • kthorngrenkthorngren Posts: 20,255Questions: 26Answers: 4,761
    Answer ✓

    Right. Instead of using filter() use rows() with a row-selector as a function to select the rows based on values in multiple columns. Updated example:
    https://live.datatables.net/fujuputi/2/edit

    Kevin

  • GargiucnGargiucn Posts: 104Questions: 29Answers: 0

    Actually I don't need it because it still respects the selection filter I applied on the table.
    Thanks for the help.

    Giuseppe

  • GargiucnGargiucn Posts: 104Questions: 29Answers: 0

    Actually I don't need it because it still respects the selection filter I applied on the table.
    Thanks for the help.

    Giuseppe

  • GargiucnGargiucn Posts: 104Questions: 29Answers: 0

    Sorry, I wrote before I saw your reply.
    It is very useful your example and I will need it.
    Thanks for the help.

    Giuseppe

Sign In or Register to comment.