Using child rows to keep 2 rows together when sorting
Using child rows to keep 2 rows together when sorting
I would like to create a DataTable and populate it in the following way:
row 1a
row 1b
row 2a
row 2b
row 3a
row 3b
When sorting by a column, I want to have the pair <row Xa, row Xb> to stay together.
For example:
row 2a
row 2b
row 1a
row 1b
row 3a
row 3b
It seems that adding row Xb's as a child row could be a solution, where the child row is always shown (I don't want to have a + button to expand this second row).
The problem is that I am getting an error.
var rowNode = theDataTable.row.add([ a1, a2, a3]).draw().node();
var rowIndex = theDataTable.row(rowNode).index();
var theData = theDataTable.row(rowIndex ).data(); // This is just to test that I can have access to the row with index rowIndex.
theDataTable.row(rowIndex).child([ b1, b2, b3]); // I am getting "r is null"
- is child row the solution for what I am trying to achieve?
- What is wrong with the way I am calling .child()?
Thanks,
Jessica
Answers
Hi Jessica,
I don't immediately see anything wrong with that. I'd need a test case showing the issue to be able to understand what is going wrong please.
And yes, this is probably the best way to make sure rows stay together.
Thanks,
Allan
I have made progress. The table looks the way I want.
What I need now is to write callbacks when clicking on the parent row or the child row.
$(document).on('click', '.parentRow', function() {
doSomethingWith(this);
});
$(document).on('click', '.childRow', function() {
doSomethingWith(this);
});
The parentRow looks like that:
<tr id="id1" class="parentClass" specialUniqueId="special1">...</tr>
The childRow looks like that:
<tr id="id2" class="childClass" specialUniqueId="special1">...</tr>
In the doSomethingWith() function, I want to be able to retrieve the specialUniqueId and it must work in both types of row (parent or child).
I tried your example:
var tr = $(this).parents('tr');
var theRow = myDataTable.row(tr);
var result = $(theRow).prop('specialUniqueId');
My result remains undefined. Something is wrong with those 3 lines.
Thanks,
Jessica
Another question.
When the child row is created, how to add classes and properties to the child row <tr>?
How are you reading the data for the table? Is it Ajax sourced, or being read from the DOM? If it is being read from the DOM you would need to have the child row's data (or rather the data destined for the child row) in the parent row (i.e. just one row for each record). DataTables cannot read child data from the DOM.
When you create the child row, you define the HTML to be shown inside it, so you would add the classes, etc, to whatever you are using to create the child row.
Can you show me the full code please?
Allan
First of all: initialization of the table with fake data.
Then the callback functions for a click:
My problem is that I don't know how I can retrieve relevant information in the callback functions. If you show me how to retrieve the uniqueId property from the parent and child <tr> (I have the feeling that for the child it will not be possible at all), I think I can move on.
Final question: I would like to have a table that looks like that:
row1 class="parentRow odd"
row2 class="childRow even"
row3 class="parentRow odd"
row4 class="childRow even"
so that the row background colors alternate nicely (I always have 1 row in the child row).
But I don't know why row3 never gets its class updated from 'even' to 'odd'.
Thanks,
Jessica
Now I know how to get the uniqueId for the parentRow:
I fixed also the childRow callback function but I still can't get the uniqueId.
Indeed when I run under the debugger, I see no property of name "uniqueId" for the <tr> of any child row. And later on, I would like to be able to check for the class 'row_selected' as shown on the code, but I am worried that this will not work...
That's because it is being set as a property rather than an attribute. DOM properties are only really useful in Javascript, while the attributes can be seen in the HTML.
The code above to get the
uniqueId
for the parent row looks good. The child row code actually looks good as well - although since it is an attribute, you should probably use.attr()
. If that doesn't work, if you give me a link to a page showing the issue I can take a look.Allan