How to count records

How to count records

lancwplancwp Posts: 85Questions: 18Answers: 1
    $(document).ready(function() {
        $('#example').DataTable( {
            "footerCallback": function ( row, data, start, end, display ) {
                var api = this.api(), data;

                // Remove the formatting to get integer data for summation
                var intVal = function ( i ) {
                    return typeof i === 'string' ?
                        i.replace(/[\$,]/g, '')*1 :
                        typeof i === 'number' ?
                            i : 0;
                };

                // Total over all pages
                total = api
                    .column( 4 )
                    .data()
                    .reduce( function (a, b) {
                        return intVal(a) + intVal(b);
                    }, 0 );

                // Total over this page
                pageTotal = api
                    .column( 4, { page: 'current'} )
                    .data()
                    .reduce( function (a, b) {
                        return intVal(a) + intVal(b);
                    }, 0 );

                // Update footer
                $( api.column( 4 ).footer() ).html(
                    '$'+pageTotal +' ( $'+ total +' total)'
                );
            }
        } );
    } );

That code just sums the totals. For an average, divide that total by the number of entries ,How to count records? thinks

This question has an accepted answers - jump to answer

Answers

  • rf1234rf1234 Posts: 2,808Questions: 85Answers: 406
    edited August 2021

    table.rows().count()

    table.rows( {page:'current'} ).count()

    https://datatables.net/reference/api/count()

    https://datatables.net/reference/api/rows()

  • colincolin Posts: 15,144Questions: 1Answers: 2,586

    Yep, as @rf1234 said, use those for the table totals. For the total records on the page, use the selector-modifier for rows(), so something like table.rows({page:'current'}).count(),

    Colin

  • lancwplancwp Posts: 85Questions: 18Answers: 1
    edited August 2021
        $('#example').DataTable( {
            
    //求和
    
     "footerCallback": function ( row, data, start, end, display ) {
                var api = this.api(), data;
     
                // Remove the formatting to get integer data for summation
                var intVal = function ( i ) {
                    return typeof i === 'string' ?
                        i.replace(/[\$,]/g, '')*1 :
                        typeof i === 'number' ?
                            i : 0;
                };
     
       // Total over all pages
                total = api
                    .column( 13 ,{ search: 'applied' })
                    .data()
                    .reduce( function (a, b) {
                        return intVal(a) + intVal(b);
                    }, 0 );
     
                // Total over all pages2
                total2 = api
                    .column( 11,{ search: 'applied' } )
                    .data()
                    .reduce( function (a, b) {
                        return intVal(a) + intVal(b);
                    }, 0 );
                    
                 // Total over all pages2
                total3 = api
                    .column( 12,{ search: 'applied' } )
                    .data()
                    .reduce( function (a, b) {
                        return intVal(a) + intVal(b);
                    }, 0 ); 
     
     
                // Total over this page
                pageTotal = api
                    .column( 13, { page: 'current'} )
                    .data()
                    .reduce( function (a, b) {
                        return intVal(a) + intVal(b);
                    }, 0 );
                    
                    
                    
                // Total counter
                pcount = api
                 .column( 1, {  page: 'current'} )
                    .data()
                    .reduce( function (c) {
                        return table.rows().count();
                    }, 0 ); 
    
     
                // Update footer
            $("#example tfoot tr:eq(0) th:eq(1) ").html(
                    Math.round(total2*100)/100
                );  
                
              $("#example tfoot tr:eq(0) th:eq(2) ").html(
                    Math.round(total3*100)/100
                );
                 $("#example tfoot tr:eq(0) th:eq(3) ").html(
                  pcount
                    
                );
                
            },
    

    Thanks. The number of records can be obtained by summing. when the filter conditions are changed, total3 and total2 can automatically change according to the filter conditions, but pcount(the number of records) will not change. What is the reason?

  • colincolin Posts: 15,144Questions: 1Answers: 2,586

    I don't think it's doing what you think it is. Line 56 is just always returning the number of rows in the table, and nothing else. What are you expecting it to do?

    Colin

  • lancwplancwp Posts: 85Questions: 18Answers: 1

    When the table filter changes, its value can change at the same time,Just like the other two values

  • lancwplancwp Posts: 85Questions: 18Answers: 1

    Now its value is fixed. When there are fewer filtering records in my table, it still displays the total number of records in the table. I want to display the number of records after filtering

  • rf1234rf1234 Posts: 2,808Questions: 85Answers: 406
    edited August 2021

    totals 2 and 3 have "search: applied".

    pcount does not have this in your code.

    table.rows( { search: 'applied' } ).count()
    

    it's all in the docs ...

    Please read: https://datatables.net/reference/type/selector-modifier

  • lancwplancwp Posts: 85Questions: 18Answers: 1
           pcount = api
                 .column( 1, {search: 'applied'} )
                    .data()
                    .reduce( function (c) {
                        return table.rows().count();
                    }, 0 ); 
    

    Yes, if I change it to search: applied, clicking the next page will be slow, and the returned value is also the total number of records, not after filtering

  • rf1234rf1234 Posts: 2,808Questions: 85Answers: 406
    edited August 2021 Answer ✓

    I have no idea why you are coding it that way. My code looks different. And you still are not applying "search" to table.rows().count().

  • lancwplancwp Posts: 85Questions: 18Answers: 1

    Thanks ,I was wrong <3 <3 <3

Sign In or Register to comment.