Render multiple fields into one column
Render multiple fields into one column
Hey guys,
I'd like to have one "details"-data column for my datatable. This column contains information from multiple tables of the database depending of the genre type of the movie.
For example:
ID title Genre Details
1 test1 comedy Rel. 2016, Comedy-Type: romcom, Duration: 120min
2 test2 horror Rel.2000, Age recommended: 21
In this very basic example table you can see, that we have two different movie types (comedy and horror) and both have their own table in the database with specific information. As you can see these specific information for each type need to be rendered into the one "Details" column.
How can I achieve this functionilty in the datatable instance? I thought about using the row-parameter of the render function (third parameter) where all needed information of the row exists in a nested object.
I really don't know how to access the individuell data-fields, first I want to access the "genre" field and depending on that, the "Detail"-column should be filled with all information for the given genre.
Thank you for your help.
Best regards
This question has an accepted answers - jump to answer
Answers
I'm not sure I follow your example. But you can access the individual data fields within your datatable instance by using the fnRowCallback. The second parameter is the raw data array. So data[0] is column one for nRow and so on.
Using a renderer is the way I would suggest you do this. Its a little more efficient than
rowCallback
.What does your JSON data structure look like (assuming it is JSON)?
Allan
Thanks for your suggestions, I tried to use a function for the "data" parameter and it seems that it could work like that. The function looks like that:
{
"data": function(row, type, set) {
var mGenre = row.movie.genre;
var details = '';
switch(mGenre) {
//movie genre = comedy
case '1':
details += 'Genre: ' + row.movie.genre;
break;
default:
details += 'no genre set';
}
return details;
}
```},````
Do you think that this is a good idea? I really dont know how to do it with the render method. any help is appreciated.
Here is the json data structure. As you can see, every movie has not only his information but also the other genre information because of the joins in the php script (although they are filled with nulls in the json data). In that example I would like to have the "detail"-column to display a string of details informations depending on the genre type. If its a comedy movie, have a string of the comedy detail data and if its a horror movie, access the horror details and display them in one column.
{
"data":[
{
"DT_RowId":"row_1",
"movie":{
"id":"1",
"title":"Test title",
"year":"2011",
"director":"2",
"deleted":"0",
"genre":"1" //comdey
},
"comedy":{
"comedy_detail_1":"Testdetails",
"comedy_detail_2":"very funny",
"comedy_detail_3":null
},
"horror":{
"horror_detail_1":null,
"horror_detail_2":null,
"horror_detail_3":null,
}
}
]
}