Combine multiple columns with comma separated lists
Combine multiple columns with comma separated lists
peterbrowne
Posts: 314Questions: 54Answers: 0
I want to combine multiple columns which use comma separated lists. E.g.
, {
data: "emergency_medicine",
render: "[, ].discipline_outcome"
}, {
data: "general_practice",
render: "[, ].discipline_outcome"
}, {
data: "internal_medicine",
render: "[, ].discipline_outcome"
},
I have looked at the manual on combining columns using render, but can't see anything specifically on columns that have comma separated lists and multiple data sources, e.g.
, {
data: null,
render: function ( data, type, row ) {
return emergency_medicine.discipline_outcome +' '+ general_practice.discipline_outcome;
},
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
Please post an example of the data structure you have and how you want to display it.
Kevin
Do you mean you want the data from
emergency_medicine
,general_practice
andinternal_medicine
to display all in a single column?You'd need a rendering function to do that similar to what you have, but you need to remember that
row.emergency_medicine
(and friends) are arrays:So:
I think is what you want (ES6 style - you might need to rewrite it for ES5 if you need to support older browsers).
Allan
Thanks Allan.
That works fine. I can see that the results are grouped according to tables from the concat with the following which is great:
Is there a way to add text as headings to each group based on the table, e.g. using a switch?
So that the results are like:
Emergency Medicine
ed outcome 1
ed outcome 2
General Practice
gp outcome 1
gp outcome 1
Internal Medicine
im outcome 1
im outcome 1
Sure - it is just a function, so you can modify it to suit your needs. It wouldn't be as compact, but add the title for the emergency medicine first, then loop over that array adding the values. Then concat the title for general practice, loop over the values adding them, etc.
Allan
Thanks again Allan.
I might need just a bit more help with that. An example would be nice.
There maybe more efficient ways to do this with Javascript but here is one example:
http://live.datatables.net/piyewisi/1/edit
I'm making assumptions about your data structure. If you need further help please update the test case to show your data structure.
Kevin
Thanks for this kthorngren.
I can see that can work. However, I need to return the column
discipline_outcome
. With your code, on my data, it just returns:data looks like:
I tried the following, but not getting the
discipline_outcome
names:Your
discipline_outcome
object is inside an array:You will need to use something like this:
This gets the first element in the array which is the object then gets the
discipline_outcome
key.You may need to loop through the array if you have/want more values.
Kevin
Many thanks, that works.