Combining row() and column() for footerCallback

Combining row() and column() for footerCallback

OenselOensel Posts: 19Questions: 4Answers: 1

I have many rows which I want to filter by content of a special column.
If column[12] contains "...gelöscht..." it's rows index ist pushed into rowIndexes.
This works. rowIndexes contains only rowIds of rows, that don't contain "mySearchString".

sumEur() still returns sum of all rows!
Please help me, I'm stuck.

var columnIndexForSum = 5;
var rowIndexes = [];
tableApi.rows().eq(0).each( function ( ix ) 
{
    var row = tableApi.row( ix );                    
    var data = row.data();
    if(data[12] != mySearchString)
    {
        rowIndexes.push(ix);
     }
});
value = formatCurrency(tableApi.row([1,2]).column( columnIndexForSum, {filter:'applied'} ).data().sumEUR());



jQuery.fn.dataTable.Api.register( 'sumEUR()', function ( ) 
{
    var data = this.flatten();  
    data = $.grep(data, function(value)
    {
        return value.indexOf('EUR') > 0;
    });
    return data.reduce( function ( a, b )
    {   
        if(String(a).indexOf('<') >= 0)
        {
            a = a.replace(/(&nbsp;|<([^>]+)>)/ig, '');
        }

        if(String(b).indexOf('<') >= 0)
        {
            b = b.replace(/(&nbsp;|<([^>]+)>)/ig, '');
        }

        if(String(a).indexOf(',') >= 0)
        {
            a = a.replace(/\./g,'');
            a = a.replace(',','.');
            a = a.replace('EUR','');
            a = a.trim();
            a = parseFloat(a);
        }
        if(String(b).indexOf(',') >= 0)
        {
            b = b.replace(/\./g,'');
            b = b.replace(',','.');
            b = b.replace('EUR','');
            b = b.trim();
            b = parseFloat(b);
        }
        return (((a*100) + (b*100)) / 100);
    }, 0 );
});

Answers

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

    We're happy to take a look, but as per the forum rules, please link to a test case - a test case that replicates the issue will ensure you'll get a quick and accurate response. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.

    Cheers,

    Colin

  • kthorngrenkthorngren Posts: 20,292Questions: 26Answers: 4,768

    I would look at using cells().data() instead of column().data(). Using tableApi.row([1,2]) will return 2 rows but that won't filter the rows for column(). Use rows() with a row-selector as a function with your data[12] != mySearchString comparison. Use rows().indexes() to get the indexes needed for the cells(). Here is an example:
    http://live.datatables.net/lidexeve/1/edit

    You should be able to chain the sumEUR() function.

    Kevin

  • OenselOensel Posts: 19Questions: 4Answers: 1

    Thank you so much!
    I got it now.

    var tableApi = $('#'+tableName).DataTable();
    value = formatCurrency(
                    tableApi.cells(
                        tableApi.rows(function ( idx, data, node ) 
                        {                               
                        return data[12] === '<img src="images/cm_3.png" title="gelöscht">' ?
                         false : true;
                        }) 
                .indexes(), index)
         .data()
    .sumEUR());
    
  • OenselOensel Posts: 19Questions: 4Answers: 1

    I got a new problem.
    Additional to the code above I want only to sum() visible rows.
    If I change row 4 to the following:
    tableApi.rows({filter:'applied'}, function ( idx, data, node )
    I get all rows, the rows that returned false in line 6 as well.

  • kthorngrenkthorngren Posts: 20,292Questions: 26Answers: 4,768

    According to the rows() API the parameter order is this:

    rows( rowSelector [, modifier ] )

    Looks like you need to swap the {filter:'applied'} and the function. If this doesn't help then please update the test case example I provided to show the problem.

    Kevin

  • OenselOensel Posts: 19Questions: 4Answers: 1

    Shame on me. You are totally right!
    I just needed to put "{filter:'applied'}" behind the functoin, of rows().
    Thank you

This discussion has been closed.