RowGroup loop all TD cells

RowGroup loop all TD cells

itajackassitajackass Posts: 155Questions: 47Answers: 3

Hi, I need to loop all TD cells when a rowGroup is enable. I need to read a data-xxx attribute from each cell to estabilish if I need to sum or subtract.

I tried this code but I get error in console.

<tr>
   <td>LONDON</td>
   <td data-typecell="sum">10</td>
</tr>
<tr>
   <td>LONDON</td>
   <td data-typecell="subtract">3</td>
</tr>
<tr>
   <td>LONDON</td>
   <td data-typecell="sum">5</td>
</tr>
<tr>
   <td>NY</td>
   <td data-typecell="sum">50</td>
</tr>



rowGroup: {
   enable: true,
        startRender: null,
        endRender: function ( rows, group ) {

            var rowsToCheck = rows.data().pluck(1); // <---- column index 1

            var sum = 0;

            $(rowsToCheck).each(function(index, value) {

              var typeCell = $(this).data("typecell");

              var value = parseFloat($(this).html()) || 0;

              if (typeCell == "sum" ) { 
                  sum = sum + value;
              } else {
                   sum = sum - value;
              }

            });

                           return $('<tr/>')
                           .append( '<td>Totals '+group+'</td>' )
                           .append( '<td class="text-right">'+sum.toFixed(2)+'</td>' )
                          .append( '<td/>' );


        },
        dataSrc: 0
    },
...

Any suggest? Thanks

Replies

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395

    I get error in console

    What error?

  • itajackassitajackass Posts: 155Questions: 47Answers: 3

    Error: TypeError: t is undefined

  • kthorngrenkthorngren Posts: 21,144Questions: 26Answers: 4,918

    Not sure if your code is actually going to retrieve the data attribute. Its not obvious why you would get the TypeError. Please put together a test case so we can help you workout what you want to do.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • itajackassitajackass Posts: 155Questions: 47Answers: 3

    Ok this is my fiddle after some edits. Still waiting for a solution :(

    http://live.datatables.net/wenocuho/1/edit

    Result expected written after the table

  • colincolin Posts: 15,237Questions: 1Answers: 2,598
    edited April 2019

    Hi @itajackass ,

    Still waiting for a solution :(

    You've just got a solution in a few hours of posting, I'd say that was pretty good service! As we always say, post a test case upfront, you'll get faster and more accurate responses.

    Cheers,

    Colin

  • itajackassitajackass Posts: 155Questions: 47Answers: 3

    Hi @colin , sorry i don't want to say "waiting" but i wanted to wrote "looking" :wink:

    I don't know how to loop inside grouped rows, and for each row of it, manipulate the (n)-td with jquery to read attributes etc...

  • colincolin Posts: 15,237Questions: 1Answers: 2,598

    Did you see I posted the solution in my response?

  • itajackassitajackass Posts: 155Questions: 47Answers: 3

    Ops i don't seen it! Thank you! It works like a charm!! :wink:

This discussion has been closed.