rows().every() not working

rows().every() not working

rf1234rf1234 Posts: 2,809Questions: 85Answers: 406

Hi, I have this rather simple table and want to manipulate the column cashflowAccum in every row. The problem is that rows().every() never executes. The 'init' event gets triggered correctly. Please help!

var cashFlowTable = $('#tblCashFlow').DataTable({
    dom: "Bfrltip",
    ajax: {
        url: 'actions.php?action=tblCashFlow',
        type: 'POST',
        data: function (d) {
            var selected = contractTable.row({selected: true});
            if (selected.any()) {
                d.contract_id = selected.data().contract.id;
            }
        }
    },
    pageLength: 50,
    lengthMenu: [5, 10, 20, 50, 100, 200, 500],
    columns: [
        {data: "cashFlowElement",
            render: function (data, type, row) {
                return renderCashFlowElement(data);
            }
        },
        {data: "cashflow.start_date",
            render: function (data, type, row) {
                return renderCashFlowStartDate(row.cashflow.start_date,
                                               row.cashflow.end_date,
                                               row.cashflow.rate,
                                               row.cashflow.is_fee);
            }
        },
        {data: "cashflow.end_date"},
        {data: "cashflow.amount_remaining",
            render: function (data, type, row) {
                return renderAmountCurrency(data, row.cashFlowCurrency);
            }
        },
        {data: "cashflow.rate",
            render: function (data, type, row) {
                return renderCashFlowRate(row.cashflow.start_date,
                                          row.cashflow.end_date,
                                          row.cashflow.rate,
                                          row.cashflow.is_fee);
            }
        },
        {data: "cashflow.repayment",
            render: function (data, type, row) {
                return renderAmountCurrency(data, row.cashFlowCurrency);
            }
        },
        {data: "cashflow.interest_fee",
            render: function (data, type, row) {
                return renderAmountCurrency(data, row.cashFlowCurrency);
            }
        },
        {data: "cashflowTotalAmount",
            render: function (data, type, row) {
                return renderAmountCurrency(data, row.cashFlowCurrency);
            }
        },
        {data: "cashflowAccum",
            render: function (data, type, row) {
                if (data !== '') {                        
                    return renderAmountCurrency(data, row.cashFlowCurrency);
                } else {
                    return data;
                }
            }
        },
        {data: null, //manual and date_change
            render: function (data, type, row) {
                return renderManual(row);
            }
        }
    ],
    columnDefs: [
        {type: 'formatted-num', targets: [3, 5, 6, 7]},
    ],
    order: [[2, 'asc']],
    select: {
        style: 'os',
        selector: 'tr.selectRow td:not(:first-child)'
    },
    buttons: [
        {extend: "edit", editor: cashFlowEditor},
        {extend: "editDates", editor: cashFlowDatesEditor},
                 "colvis"
    ],
    rowCallback: function (row, data) {
        if ( data.cashflow.manual > 0 || data.cashflow.date_change > 0 ) {
            $(row).addClass('fontThick');
        }
        //if it is not the summation row the row should be selectable
        if ( ( data.cashflow.start_date !== data.cashflow.end_date  && 
               data.cashflow.rate !== '0,0000'    &&
               data.cashflow.rate !== '0.0000'      ) ||
             data.cashflow.is_fee > 0  )                 {
            $(row).addClass('selectRow');
        }
    }
});

cashFlowTable
        .on('init', function () {
            cashFlowTable.rows().every(function (rowIdx, tableLoop, rowLoop) {
                var data = this.data();
                if (rowIdx <= 0) {
                    data.cashflowAccum = 0;
                }
                data.cashflowAccum += data.cashflowTotalAmount;
                this.invalidate();
            });
            cashFlowTable.draw();                
 });

Replies

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

    by the way this table is one of multiple tables that I have on that page. But I read this shouldn't matter ... I tried with a stand alone table. I have on a different page. There it worked immediately. If you have any advice how to handle this issue having multiple tables this would really help.

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

    Found it myself ... As you can see above this is a child table. it is only shown when a row of the parent table is being selected. On select of a row of the parent table I do this now:

    cashFlowTable.ajax.reload( function ( json ) {
        cashFlowTable.rows().every( function (rowIdx, tableLoop, rowLoop) {
            var data = this.data();
            if (rowIdx <= 0) {
                data.cashflowAccum = 0;
            }
            data.cashflowAccum += data.cashflowTotalAmount;
            this.invalidate();
        })
        .columns.adjust()
        .responsive.recalc()
        .draw();       
    } );
    

    Works now; problem solved.

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

    Well, not quite ... that wasn't a good solution. Since I do all the number rendering on the server side I needed to convert everything to float and then back again. I ended up not changing anything about my existing ajax.reloads. All I did was to add a draw event on the respective table:

    cashFlowTable
                .on('draw', function () {
                    calcCashFlowAccum(cashFlowTable);
                });
    

    And this is the function that I call:

    function calcCashFlowAccum(that) {
        var deNumberRenderer = $.fn.dataTable.render.number( '.', ',', 2 ).display;
        var enNumberRenderer = $.fn.dataTable.render.number( ',', '.', 2 ).display;
        var accum = 0;
        that.rows().every( function (rowIdx, tableLoop, rowLoop) {
            var data = this.data();
            var rowTotal;
            if (lang == 'de') {
                rowTotal = data.cashflowTotalAmount.replace(".", "");
                rowTotal = rowTotal.replace(",", ".");
            } else {
                rowTotal = data.cashflowTotalAmount.replace(",", "");
            }
            if ( ! isNaN( parseFloat(rowTotal) ) ) {             
                accum += parseFloat(rowTotal);
                if (lang == 'de') {
                    data.cashflowAccum = deNumberRenderer(accum);
                } else {
                    data.cashflowAccum = enNumberRenderer(accum);               
                }
            } else {
               data.cashflowAccum = '';
            }
            this.invalidate();
        });
    }
    
This discussion has been closed.