Count rows depending on a criteria

Count rows depending on a criteria

microshopmicroshop Posts: 11Questions: 4Answers: 1

Hi,

I want to count the rows of a column, but only count if matches the criteria.

For example, we have that drawed table:

Name-Hired

James-Yes
Olivia-Yes
Paul-No
Sonya-Yes
Donna-No

The criteria is: Hired = Yes. And the result is Count = 3.

But I don't want draw anything, only count.

Thank you.

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,303Questions: 26Answers: 4,947

    You can use filter() for this. The length of the result will be the Count.

    Kevin

  • microshopmicroshop Posts: 11Questions: 4Answers: 1

    Hi Kevin,

    I've used the example of filter() but it returns always 0.

    var table = $('#example').DataTable();
     
    var filteredData = table
        .column( 0 )
        .data()
        .filter( function ( value, index ) {
            return value > 20 ? true : false;
        } );
    

    If I console.log filteredData, or if I add .count(), always returns 0.

  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin

    That suggests there is no data point in the first column which is > 20. Are you able to link to an example showing the issue so we can see the data as well?

    Allan

  • kthorngrenkthorngren Posts: 21,303Questions: 26Answers: 4,947
    edited January 2018

    return value > 20 ? true : false;

    For your case this should be:

    return value === "Yes" ? true : false;

    And

    .column( 0 )

    Should be the column containing the "Yes" / "No" values. In your example this would be:

    .column( 1 )

    Resulting in this:

    var filteredData = table
        .column( 1 )
        .data()
        .filter( function ( value, index ) {
            return value === "Yes" ? true : false;
        } );
    

    Kevin

  • microshopmicroshop Posts: 11Questions: 4Answers: 1
    edited January 2018

    I've made a simple example and it works, then should be my own code.

    But, if I do a search, the filter doesn't apply based on the search.

    https://jsfiddle.net/e2098fcz/1/

  • kthorngrenkthorngren Posts: 21,303Questions: 26Answers: 4,947
    Answer ✓

    You need to use a selector-modifier to control which data is looked at. You would use something like this to filter only on the search results:

        var filteredData = table
            .column(3, {search: 'applied'})
            .data()
            .filter( function ( value, index ) {
                return value > 20 ? true : false;
            } );
    

    The column() docs have more info.

    Kevin

  • microshopmicroshop Posts: 11Questions: 4Answers: 1
    edited January 2018

    Thank you very much, I've made some changes and now it works in the simple example.

    https://jsfiddle.net/e2098fcz/3/

  • microshopmicroshop Posts: 11Questions: 4Answers: 1

    I've improved the script for search in several columns.

    https://jsfiddle.net/e2098fcz/6/

This discussion has been closed.