event pre filter?

event pre filter?

markzzzmarkzzz Posts: 49Questions: 8Answers: 1

Hi,

I've a datatable with deferRender: true, and some custom filters.
I need, before filter the data and display accordely, to reset some variables.

Is there any pre filter event?
In short: I need to calculate some values and display the sum in the footer, for the filtered row (even if they are not displayed due to pagination, of course).

So I need to set sum = 0 before any filtering.

Any clues?

Replies

  • kthorngrenkthorngren Posts: 20,275Questions: 26Answers: 4,765

    Here is a list of all the Datatables events. There isn't a pre filter event. Maybe preDraw will help??

    If you still need help please start by posting your Javascript code so we can see what you are doing to get a better idea of how to reset the variables. Better wold be a link to your page or a test case demonstrating the issue.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • markzzzmarkzzz Posts: 49Questions: 8Answers: 1
    edited December 2021

    Tried it, but it's called "after" the filters.

    Basically here's the filter I have:

        function FilterRowsByEarnedPayments(monthFilter1, yearFilter1, monthFilter2, yearFilter2, columnName) {
            return $.fn.dataTable.ext.search.push(
                function (settings, data, dataIndex, originalData) {
                    var tr = $(settings.aoData[dataIndex].nTr);
    
                    var found = false;
                    var payments = originalData[columnName];
    
                    for (var i = 0; i < payments.length; i++) {
                        if (someCondition) {
                            found = true;
                            myTable.row(tr).invalidate();
                            footerAmount += payments[i].Amount;
                        }
                    }
    
                    return found;
                }
            );
        }
    

    which must sum, for each row that is within someCondition, the amount value.

    Later, on the footer callback:

        footerCallback: function (tfoot, data, start, end, display) {
            // ...
    
            myTable.columns('.sum-total-earned-payments').every(function (columnIndex) {
                $(this.footer()).html(footerAmount);
            });
            
            // ...
        }
    

    I need to display the footerAmount. But I don't know how to reset the footerAmount before filtering.

    Any helps?

  • markzzzmarkzzz Posts: 49Questions: 8Answers: 1

    It seems my reply is vanished? Or do you need to moderate?

  • kthorngrenkthorngren Posts: 20,275Questions: 26Answers: 4,765

    Your post probably it the forums spam filter. If you are posting code please use Markdown code formatting. The problem might be pasting code that doesn't have Markdown formatting.

    Kevin

  • rf1234rf1234 Posts: 2,806Questions: 85Answers: 406

    if you have implemented your custom filters using ext.search.push something like this could work. "accum" is a global variable that you can use in your table footer or wherever it is required.

    var accum = 0;
    
    $.fn.dataTable.ext.search.push(
        function (settings, data, dataIndex, row, counter) {
            if (typeof row.yourTable === 'undefined') {
                return true;
            }
            accum += row.yourTable.value;
            
            //your filtering logic
            //if ok => return true;
            //if needs to disappear => return false;
        }
    );
    
    var yourTable= $('#yourTable').DataTable({ ....
    
  • colincolin Posts: 15,142Questions: 1Answers: 2,586

    Yep, sorry, @markzzz , the spam filter grabbed it - it's released now and it's your penultimate reply. Please let us know if you're having problems still - hopefully the replies above have got you going,

    Colin

Sign In or Register to comment.