Sum of numbers in column that updates when a filter is applied

Sum of numbers in column that updates when a filter is applied

EvertAlbersEvertAlbers Posts: 5Questions: 2Answers: 0

Hi all,

I have a datatable with some filters on top, and a column with numbers, showing the total in the footer. The data comes from a WordPress-like CMS (Boltcms.io) as a normal <table> and currently the total is calculated in twig/php.

When a visitor applies a filter, and only selected rows are shown, I would like to update the total so only the numbers in the selected/shown rows are counted in the sum. But how?

I am pretty sure that something like this is possible with DataTables, but I cannot seem to find it in the docs or this forum, so I am probably using the wrong search terms.

Thanks in advance for your help - or a link to the right page in the docs.

Answers

  • kthorngrenkthorngren Posts: 21,174Questions: 26Answers: 4,923

    currently the total is calculated in twig/php.

    Not sure about how this works.

    The FooterCallback example shows how to use Datatables to sum the columns.

    Kevin

  • EvertAlbersEvertAlbers Posts: 5Questions: 2Answers: 0

    I think that is what I'm looking for, I'll give it a spin and keep you posted. Thanks a lot!

  • EvertAlbersEvertAlbers Posts: 5Questions: 2Answers: 0

    Update: I have got it working, and added some extras: reformatting the number and only showing the sum when a filter is active. I may come back here with another question about filters, but thanks so far.

                // Format number in JS (notjQuery)
                var localNumber = pageTotal.toLocaleString("en");
                // Update footer - only show this number if a subselection has been made
                if (pageTotal != total) {
                    $( api.column( 6 ).footer() ).html(
                        '(Selected: $ '+ localNumber +' Million)'
                   );
                }
    
This discussion has been closed.