Sum the data of one column but filter

Sum the data of one column but filter

Massimo1974Massimo1974 Posts: 27Questions: 2Answers: 0

Hello,
I would like to add data to a column but add only the filtered rows.....dovrei usare ".filter()" ???

    "footerCallback": function ( row, data, start, end, display ) {                    
          var api = this.api(), data;              
          var intVal = function ( i ) {
              return typeof i === 'string' ?
                  i.replace(/[\$,]/g, '')*1 :
                  typeof i === 'number' ?
                      i : 0;
          };
          var Total5 = api
                  .rows( { selected: true } ).indexes()
                  .column(5,{page:'all'})
                  .data()
                  .reduce( function (a, b) {
                      return intVal(a) + intVal(b);
                  }, 0 );

$( api.column(5).footer()).html(Total5);

Replies

  • colincolin Posts: 15,210Questions: 1Answers: 2,592

    Hi @Massimo1974 ,

    I don't entirely follow, but I think you just need to change line 10 to be

    .rows( { selected: true, search: 'applied' } ).indexes()
    

    If that's not what you're after, please can you provide a test case, showing the current behaviour and what you want instead. 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

  • Massimo1974Massimo1974 Posts: 27Questions: 2Answers: 0

    http://live.datatables.net/temuguhi/1/edit
    in this table table export-option does not work, i have another problem, exportOption:{columns:[ 7,8,9,10, ':visible' ]} no work

  • colincolin Posts: 15,210Questions: 1Answers: 2,592

    Hi @Massimo1974 ,

    That fiddle doesn't run - please can you fix the errors. As I said before, can you show "the current behaviour and what you want instead" - simply saying it doesn't work doesn't help.

    Thanks,

    Colin

  • Massimo1974Massimo1974 Posts: 27Questions: 2Answers: 0
    edited March 2019

    .rows( { selected: true, search: 'applied' } ).indexes()
    not work

  • kthorngrenkthorngren Posts: 20,691Questions: 26Answers: 4,840

    I'm not sure why you have .rows( { selected: true } ).indexes() in your original code snippet. But if you want to sum the value based on the search results then you can change .column(5,{page:'all'}) to use a different selector-modifier: {search:'applied'}. I updated the FooterCallback Example to sum the values based on the search results:
    http://live.datatables.net/losejowi/1/edit

    likely you will want to remove .rows( { selected: true } ).indexes() and just use .column(5,{search:'applied'}). If you aren't able to get this to work then please create a simple example with your data and your FooterCallback. The reset of your code isn't needed for a test case.

    Kevin

  • Massimo1974Massimo1974 Posts: 27Questions: 2Answers: 0

    good now is work thanks
    var Total5 = api
    .column(5,{search: 'applied'})
    .data()
    .reduce( function (a, b) {
    return intVal(a) + intVal(b);
    }, 0 );

This discussion has been closed.