Adding child rows without toggle sign
Adding child rows without toggle sign
I want to dynamically add child rows below each parent rows in HTML table. This link shows how to do it by adding hide/show option:- https://datatables.net/examples/api/row_details.html, but I want to avoid that and display child rows without having to click show/hide option. Also the data are dynamically loaded from ajax call. I parse the JSON response from ajax call and append in HTML table using JavaScript and DataTable in following way:-
function displayFruits() {
$.ajax({
type: "GET",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
dataType: "json",
url: "http://www.dummyexample.com",
success: function(data) {
tr = $('<tr/>');
tr.append("<td>" + data[i].name + "</td>");
tr.append("<td>" + data[i].type + "</td>");
tr.append("<td>" + data[i].cost + "</td>");
tr.append("<td>" + data[i].season + "</td>");
tr.append("<td>" + data[i].vitaminContent + "</td>");
$('#tableId').append(tr);
}
$('#tableId').DataTable();
},
error: function(xhr, ajaxOptions, thrownError) {
// handle your fail response here
}); //end of ajax
} // end of displayFruits() function
In above example, instead of displaying season and vitaminContent in separate column, I want to display them as child row below parent row. How can I achieve that?
Answers
The example you link to uses a simple
click
event handler to open / close the child rows. Instead of doing that, just userows().every()
to iterate over the rows and open each in turn.Allan
I am new to DataTables, so excuse me if it is too general question. The thing is that I already have a for loop that loops through the JSON data that I get from ajax call. My code with for loop:-
2.1 So my question is in order to have child row feature, do I need to configure
in my code ? If so, how could I achieve that ?
3. Because the data is not static as mentioned in the example in link https://datatables.net/examples/api/row_details.html, how/where can I configure this function:
Thank you.
Yes you would because DataTables cannot read child rows from the DOM. You need to create them using the DataTables API.
No. That column is used in the demo to act as a click handler for
row().child()
. You need to activate that function for every row in the DataTable, which can be done usingrows().every()
after the table has been created.I don't really understand the question I'm afraid. Its a function that is entirely under your control. You'd modify it to output what you need in the child row.
Allan
Thank you very much. I was able to achieve what I wanted by adding following code which is demonstrated in https://datatables.net/reference/api/rows%28%29.every%28%29 :
However the problem is that I want the child rows to autospan, rather than me having to hard code colspan="18". How can I autospan child rows as shown in the attached screenshot? The screenshot was taken from https://datatables.net/examples/api/row_details.html.
I'd forgotten that example was there - sorry!
To have it auto span, you'd need to just give it something like:
i.e. give it a string, not a
tr
element.Allan
Thank you very much. It worked for one child row. How can we add multiple child rows ?
It looks like there might be a bug there - the jQuery addition should actually work there.
I'll try to make some time to make a test case and see what is going wrong.
Thanks,
Allan
I've finally managed to get around to looking into this - sorry!
The problem is that Responsive is being used to hide the columns. If
column().visible()
were being used, then it would update thecolspan
no problem. But that isn't the case here - Responsive implements its own column hiding that DataTables core isn't aware of - so it can't update the child rows.Equally, since the child rows have been added externally, Responsive can't update them either since it knows nothing about them. But it does trigger
responsive-resize
so you know can know the columns have changed and update thecolspan
attribute at that time.Allan