count grouping item

count grouping item

m75sam75sa Posts: 132Questions: 30Answers: 0

Considering this example:
https://jsfiddle.net/lbriquet/ua4yLscx/

i need to count grouped items and this works but what i want is also to count items grouped by two columns.
To be clear:
In this case i see that it counts items grouped by OFFICE, is it possible also to group by OFFICE and POSITION?

Thanks for your help

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,692Questions: 1Answers: 10,500 Site admin

    Do you mean you want two levels of grouping? If so, then yes, that is possible in RowGroup and is shown in this example.

    If I've misunderstood and you are looking for something like more control over the grouping text, can you clarify what you are looking for please?

    Thanks,
    Allan

  • m75sam75sa Posts: 132Questions: 30Answers: 0
    edited January 2023

    @allan i replicated the table i have.
    So what i want is count the grouped items by letter and by status so:

    result like this:
    AA FULL 3
    AA EMPTY 1
    BB EMPTY 1
    CC EMPTY 1
    DD EMPTY 1

    live.datatables.net/vosuwixo/1/edit?html,js,output

  • kthorngrenkthorngren Posts: 21,459Questions: 26Answers: 4,975

    The rows parameter of rowGroup.startRender contains the rows displayed on the page. You can use rows().every() to loop through these rows. Something like this:

            rows.every( function ( rowIdx, tableLoop, rowLoop ) {
              var data = this.data();
              // ... do something with data(), or this.node(), etc
            } );
    

    Kevin

  • m75sam75sa Posts: 132Questions: 30Answers: 0

    @kthorngren thanks. Can you help me by updating the script posted on the live.datatable ? (live.datatables.net/vosuwixo/1/edit?html,js,output)
    Sorry, for me it's easier to understand by example. Thanks in advance for the support!

  • kthorngrenkthorngren Posts: 21,459Questions: 26Answers: 4,975

    I fixed your test case so it runs:
    http://live.datatables.net/zawijijo/1/edit

    I'm not sure what you want to count so I just count the number of FULL cells. I used $(data[1]).text() to get the text out of the div in the cell.

    Kevin

  • m75sam75sa Posts: 132Questions: 30Answers: 0

    mm... AA FULL will be 4 and you count 3
    AA EMPTY is just 1
    BB full is 1 and you don't count...
    maybe something doesn't work...

  • kthorngrenkthorngren Posts: 21,459Questions: 26Answers: 4,975

    I'm confused. In the example you provided AA has three rows with FULL. The other groups don't have any rows with FULL. The example is just counting the number of times FULL is found. You are welcome to change the code to count the items the way you want.

    Kevin

  • m75sam75sa Posts: 132Questions: 30Answers: 0

    have a look on this, i cleaned the table:
    live.datatables.net/tofebufe/1/edit?html,js,output

    considering this table i would like to have a result like this:

    AA EMPTY. 2
    AA FULL. 1
    BB EMPTY 3
    CC EMPTY 2
    CC FULL 1

    possible?

  • kthorngrenkthorngren Posts: 21,459Questions: 26Answers: 4,975
    edited January 2023

    Are you saying you want AA EMPTY as a group and AA FULL as a different group? If yes then you can use rowGroup.dataSrc as a function to combine the two columns to build the groups.

    Otherwise update my code to count the rows with EMPTY and display both counts.

    If you want further help please make sure the test case runs properly.

    Kevin

  • m75sam75sa Posts: 132Questions: 30Answers: 0
    edited January 2023

    hi @kthorngren, yes, you're right. The first case is what i need:
    AA EMPTY as a group and AA FULL as a different group.
    but also BB EMPTY as group and BB FULL as a group...etc...

    Can you help me to adapt the test case? Sorry and thanks

  • kthorngrenkthorngren Posts: 21,459Questions: 26Answers: 4,975

    I fixed your test case so it runs properly. You had two tbody tags which won't work - Datatables didin't find any rows. Going forward please make sure your test cases run properly so we don't have to fix syntax and other errors that are not part of the question.
    http://live.datatables.net/tofebufe/3/edit

    I added order to sort the columns appropriately and rowGroup.dataSrc as a function to combine the two columns.

    Kevin

  • m75sam75sa Posts: 132Questions: 30Answers: 0

    ah fantastic!
    thanks. last thing: if i don't want to display all the items below each group? so if i want only grouped items with the total ?

  • m75sam75sa Posts: 132Questions: 30Answers: 0

    anyway you are the number one, my friend!

  • kthorngrenkthorngren Posts: 21,459Questions: 26Answers: 4,975
    Answer ✓

    Add the collapsed code back into the solution.

    Kevin

  • m75sam75sa Posts: 132Questions: 30Answers: 0
    edited January 2023

    hi @kthorngren , thanks a lot. work perfectly

Sign In or Register to comment.