0

I'm using the jQuery datatables plugin with the rowGroup addon.

I need to use the 'startRender' function to display some information in the 'headline' of each group.

Links:

Datatables: https://datatables.net/

RowGroup Addon: https://datatables.net/extensions/rowgroup/examples/initialisation/startAndEndRender.html

But the problem is, that I cant read the row tr Id to get the needed data.

How can I access the row Id inside the startRender function to pass the data in the headline of the group?

Thats my jQuery rowGroup:

rowGroup: {
    dataSrc: 1,
    startRender: function ( rows, group ) {
        var kundenId = table.row( this ).index();
        var kundenDomainAnzahl = 1;
        var kundenMaxDomainAnzahl = 2;
        return group +' ( '+ kundenDomainAnzahl +' / '+ kundenMaxDomainAnzahl +' / '+ kundenId + ')';
    }
}

And my table is filled by PHP vars, but in general the rows looks like this:

<tr id="number">
    <td>Name</td>
    <td>Type</td>
    <td>Age</td>
</tr>
2
  • if you provide a small and to the point snippet it would be much easier to help you. with that said consider adding links to reference the plugins you are using so we can maybe get easier access to the docs of the plugin you are using Commented Jul 4, 2017 at 11:42
  • did, thank you for your advise! Commented Jul 4, 2017 at 11:56

1 Answer 1

1

startRender rows is actually a dataTables API holding the rows matching the group. So work directly on rows, no need for the general table API.

In order to get the <tr> id's, loop over rows and grab the id through nodes(). Example :

startRender: function ( rows, group ) {
  var ids = '';
  rows.every(function() {
    if (ids.length) ids+=', ';
    ids+=this.nodes().to$().attr('id'); 
  })
  return 'test id´s : ' + ids;
}

Have jotted down this demo that more or less correspond to the issue you a facing -> http://jsfiddle.net/0x6m94ha/


Update. Using an array and join() in order to prevent duplicated ids :

startRender: function ( rows, group ) {
  var ids = [];
  rows.every(function() {
    var id = this.nodes().to$().attr('id');
    if (!~ids.indexOf(id)) ids.push(id);
  })
  return 'test id´s : '+ ids.join(', ');
}

demo -> http://jsfiddle.net/0x6m94ha/1/

Sign up to request clarification or add additional context in comments.

2 Comments

Thats the correct thing, but i made a mistake with the explanation. The ids are like this: 1, 1, 3, 5, 5 so a lot of ids are doubled. In your example it would be that name is doubled like: Name: a, Name: a, Name: b, Name: c, Name: c.
@hublrs They are not doubled, I just used the same text for the rows, since the content of the columns is irrelevant, besides the one column you are grouping on. I assumed the id's were unique, since id's should be unique, but have updated with an example of how to prevent duplicated id's in the returned caption text.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.