startRender count

startRender count

antoniocibantoniocib Posts: 277Questions: 62Answers: 1

Hello everyone, I can't figure out how to count how many subgroups there are, I am attaching photos to be clearer

I need to count how many elements circled in red there are

the code is this:

        startRender: function ( rows, group, level ) {

                     if ( level == 1 ) {
                       return group;

                     } else {

                      return group +' ('+rows.count()+' Viaggi)';
                     }
                 },

This question has an accepted answers - jump to answer

«1

Answers

  • kthorngrenkthorngren Posts: 21,330Questions: 26Answers: 4,951

    The level starts at 0 for the first group. For the second group level == 1 you are just returning the group.

    Kevin

  • antoniocibantoniocib Posts: 277Questions: 62Answers: 1

    @kthorngren
    I would like to count level 0 how many sublevels it has below, not how many total rows

  • antoniocibantoniocib Posts: 277Questions: 62Answers: 1

    for example do this:

                 if ( level == 0 ) {
                   return group +' ('+**level**.count()+' Viaggi)';
     
                 } else {
     
                  return group +' ('+rows.count()+' Viaggi)';
                 }
    
  • rf1234rf1234 Posts: 2,991Questions: 87Answers: 421
    edited February 2022

    Are you sure that this is your complete code? I think you have 3 nesting levels not just 2, don't you?
    Looks like in this example: https://datatables.net/extensions/rowgroup/examples/initialisation/multipleGroups.html

    Level 0 is the line with "Viaggi" in it.
    Level 1 is the line with the value from "Num Bxd" in it.
    Level 2 is all the rest.

    And you will also have this in your code:

          rowGroup: {
                dataSrc: [ 10, 8 ]
            },
    
  • antoniocibantoniocib Posts: 277Questions: 62Answers: 1
    edited February 2022

    Okay @rf1234 I know this, I only that I need to count how many NUM BXDs are associated with level 0 and write it in brackets

  • rf1234rf1234 Posts: 2,991Questions: 87Answers: 421

    Then post your code ...otherwise it is a lot of guess work for us. Even better: Post a test case.

  • antoniocibantoniocib Posts: 277Questions: 62Answers: 1
    var table = $('#Buono').DataTable( {
    
        dom: 'Bfrtip',
        paging: false,
        ajax: {
                url: 'php/table.Buono.php',
                type: 'POST'
    
        },
        order:[10,8],
        rowGroup: {
            dataSrc: ['buono.autista', 'buono.n_bxd'],
    
            startRender: function ( rows, group, level, data) {
    
                         if ( level == 0 ) {
                           return group+' ('+rows.count()+' Viaggi)';
    
    
                         } else {
                           return group +' ('+rows.count()+' Scarichi)';
                         }
                     },
    columns:[
    
    {
                    "data": "buono.id_spedizione",
                    orderable:false,
                searchable:false  //0
                },
                {
                    "data": "buono.data_spedizione",
                    orderable:false,
                searchable:false //1
                },
                {
                    "data": "buono.mittente",
                    orderable:false,
                searchable:false    //2
                },
                {
                    "data": "buono.prov_mittente",
                    orderable:false,
                searchable:false //3
                },
                {
                    "data": "buono.destinatario",
                    orderable:false,
                searchable:false //4
                },
                {
                    "data": "buono.loc_destinatario",
                    orderable:false,
                searchable:false //5
                },
                {
                    "data": "buono.prov_destinatario",
                    orderable:false,
                searchable:false //6
                },
                {
                    "data": "buono.n_bancali",
                    orderable:false,
                searchable:false //7
                },
                {
                    "data": "buono.n_bxd" //8
                },
                {
                    "data": "buono.data_bxd",
                    orderable:false,
                searchable:false    //9
                },
                {
                    "data": "buono.autista" //10
                },
                {
                    "data": "cond.zona",
                    orderable:false,
                searchable:false
            } //11
    
    
    ],
    buttons: [
        { extend: 'create', editor: editor },
        { extend: 'edit',   editor: editor },
        { extend: 'remove', editor: editor }
    ],
    language: {
    select: {
                            rows: {
                                    _: "%d righe selezionate",
                                    0: "",
                                    1: "1 riga selezionata"
                            }
                    },
    processing:     "Processando...",
    search:         "",
    lengthMenu:     " ",
    info:           " ",
    infoEmpty:      "Mostra 0 su 0 di 0 spedizioni",
    infoFiltered:   "",
    infoPostFix:    "",
    loadingRecords: "Caricamento in corso...",
    zeroRecords:    "Nessuna spedizione",
    emptyTable:     "Nessuna spedizione disponibile",
    paginate: {
            first:     "",
            previous:   "",
            next:       "",
            last:       ""
    },
    aria: {
            sortAscending:  ": Attivare per ordinare la colonna in modo crescente",
            sortDescending: ": Attivare per ordinare la colonna in modo decrescente"
    }
    },
    
    
    
    } );
    
    
  • antoniocibantoniocib Posts: 277Questions: 62Answers: 1

    This is the code complete.

  • kthorngrenkthorngren Posts: 21,330Questions: 26Answers: 4,951

    You will need to iterate through the rows that are passed in and get the unique values for the sub-level columns. I would try using pluck() and unique() for this. Maybe something like this:

    var subLevelCount = rows.pluck( 'my_second_level' ).unique().count();
    

    Replace my_second_level with whatever your second level object is.

    Kevin

  • rf1234rf1234 Posts: 2,991Questions: 87Answers: 421

    That is not the code that produced your picture because the picture doesn't have "Scarichi" in it.

  • antoniocibantoniocib Posts: 277Questions: 62Answers: 1

    @kthorngren
    It always returns 1 this variable that you made

  • antoniocibantoniocib Posts: 277Questions: 62Answers: 1

    @rf1234 non ho capito cosa intendi

  • antoniocibantoniocib Posts: 277Questions: 62Answers: 1
    edited February 2022

    usando questo codice:

    startRender: function ( rows, group, level, data) {
    if ( level == 0 ) {
     var subLevelCount = rows.pluck( 'buono.n_bxd' ).unique().count();
             return group+' ('+subLevelCount+' Viaggi)';
                         } 
      else {
    return group +' ('+rows.count()+' Scarichi)';
          }
                     },
    
  • kthorngrenkthorngren Posts: 21,330Questions: 26Answers: 4,951

    It will probably work better if you add .data() :smile: Something like this:

    var subLevelCount = rows.data().pluck( 'my_second_level' ).unique().count();
    

    Kevin

  • antoniocibantoniocib Posts: 277Questions: 62Answers: 1

    @kthorngren nope, not work..

  • kthorngrenkthorngren Posts: 21,330Questions: 26Answers: 4,951

    nope, not work..

    What happens. Please provide a test case that we can help you with.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • antoniocibantoniocib Posts: 277Questions: 62Answers: 1

    @kthorngren i dont know how to do a test case..

  • kthorngrenkthorngren Posts: 21,330Questions: 26Answers: 4,951

    Please explain what happens instead of just nope, not work.

    Kevin

  • antoniocibantoniocib Posts: 277Questions: 62Answers: 1
    edited February 2022

    @kthorngren Adding .data () doesn't change anything, it always returns 1

  • kthorngrenkthorngren Posts: 21,330Questions: 26Answers: 4,951

    It seems to work here:
    http://live.datatables.net/qiquhari/2/edit

    Kevin

  • antoniocibantoniocib Posts: 277Questions: 62Answers: 1

    in this way @kthorngren count all rows below

    in this way it counts the lines with the numbers in red, but i need to counts the numbers in blue:

    rowGroup: {
        dataSrc: ['buono.autista', 'buono.n_bxd'],
    
        startRender: function ( rows, group, level) {
    
                 if ( level === 0 ) {
             var subLevelCount = rows.data().pluck( 8 ).count();
    
                return group+' ('+subLevelCount+' Viaggi)';
    
    
                                     } else {
        return group +' ('+rows.count()+' Scarichi)';
                                     }
                             },
    
    },
    
  • kthorngrenkthorngren Posts: 21,330Questions: 26Answers: 4,951

    You are missing the unique() API call.

    Kevin

  • antoniocibantoniocib Posts: 277Questions: 62Answers: 1

    rowGroup: {
        dataSrc: ['buono.autista', 'buono.n_bxd'],
    
        startRender: function ( rows, group, level) {
    
         if ( level === 0 ) {
     var subLevelCount = rows.data().pluck( 8 ).unique().count();
    
             return group+' ('+subLevelCount+' Viaggi)';
    
    
         } else {
         return group +' ('+rows.count()+' Scarichi)';
                 }
             },
    
    },
    
  • antoniocibantoniocib Posts: 277Questions: 62Answers: 1

    @kthorngren with the .unique() return 1

  • kthorngrenkthorngren Posts: 21,330Questions: 26Answers: 4,951

    Looks like you are using objects. So pluck should be the object, for example .pluck('buono.n_bxd').

    Kevin

  • antoniocibantoniocib Posts: 277Questions: 62Answers: 1

    @kthorngren always 1 return

  • kthorngrenkthorngren Posts: 21,330Questions: 26Answers: 4,951

    I provided an example where it works. You will need to provide a link to your page or an example where its not working so we can help debug.

    Kevin

  • antoniocibantoniocib Posts: 277Questions: 62Answers: 1
    edited February 2022

    @kthorngren Can give you AnyDesk?

  • kthorngrenkthorngren Posts: 21,330Questions: 26Answers: 4,951

    You might be able to arrange purchased support from the developers if you want to arrange a remote login session. Contact Allan or Colin directly to find out details of setting up a remote login session.

    Kevin

  • antoniocibantoniocib Posts: 277Questions: 62Answers: 1
Sign In or Register to comment.