Add new child rows
Add new child rows
Hello:
I want to add new child rows, similar to blog example at https://datatables.net/blog/2017-03-31 but can't make it work. The child rows has the same structure the parent table has, without the details-control column. My format function is
function format ( rowData ) {
var fila = $('<tr/>')
.addClass( 'loading' )
.text( ' Cargando ... ' );
$.ajax( {
url: 'http://webservice/ajax.php',
data: {
exp: rowData["Expedition"]
},
dataType: 'json',
type: 'post',
success: function ( json ) {
fila
.rows.add(json)
.draw()
.removeClass( 'loading' );
}
} );
return fila;
}
The code that call it it's the same as in blog entry, but I get fila.rows is undefined. Is rows.add() only for DataTable object?, How can I add new rows in this case? Should I use a DataTable inside the Datatable? (I tried it also, without luck)
function format ( rowData ) {
var fila = $('<div/>')
.addClass( 'loading' )
.text( ' Cargando ... ' );
$.ajax( {
url: 'http://webservice/ajax.php',
data: {
exp: rowData["Expedition"]
},
dataType: 'json',
type: 'post',
success: function ( json ) {
fila
.html(fila.DataTable(
"columns": [
// Elliminated details-control colum
{"data": "Column1", "title": "Column1" },
{"data": "Column2", "title": "Column2"},
{"data": "Column3", "title": "Column3"}
]
)
.draw()
.removeClass( 'loading' );
}
} );
return fila;
}
Any clue or advice about what I'm doing wrong.
Thanks
This question has an accepted answers - jump to answer
Answers
For the record, my html table is:
Columns names and titiles are set with columns:[{data: , title: }]
How are you calling your format() function? Are you sure you followed the example thoroughly?
I think it's verbatim, except for table name
You have this:
fila
is not a Datatables API instance so you can't use rows.add(). The example has this:Are you wanting something that looks like this example?
https://datatables.net/examples/api/row_details.html
In the example you posted
json.html
has the data of interest. You will need to take whatever is returned in yourjson
variable and structure like the row_details.html example or however you want it to look.Hope this makes sense.
Kevin
Yes and no, the rows to show has the same columns the table, they don't show additional info about the parent row, but new rows related to the parent.
If we have this table
And press the in "Bradley Greer" row, I want to get related data from my webservice (this part works, I get the new data)
My full code is now (datatable inside datatable)
But shows an error on
var fila = $('<div/>')
because div not foundSimilar with
fila = $('</tr>')
(the close tr of parent row)Tried to include the ajax call inside the datatable also,
But neither worked
I know I'm close to get it work, but don't know what's is failing
You don't want to initialize a new table in your format function. You need to build and return the HTML table structure you want to display. Similar to the format function in this example:
https://datatables.net/examples/api/row_details.html
Kevin
I'll try that, but still will have the problem that the first line stops execution because
isn't found (nor </tr>, <tr/>, </div>).
Thanks
after because, I wrote up <d i v / > (added spaces now to force it be written)