Combine multiple columns with comma separated lists

Combine multiple columns with comma separated lists

peterbrownepeterbrowne 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

Answers

  • kthorngrenkthorngren Posts: 20,302Questions: 26Answers: 4,769

    Please post an example of the data structure you have and how you want to display it.

    Kevin

  • allanallan Posts: 61,722Questions: 1Answers: 10,108 Site admin

    Do you mean you want the data from emergency_medicine, general_practice and internal_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:

    render(data, type, row) {
      return []
        .concat(row.emergency_medicine, row.general_practice, row.internal_medicine)
        .map(r => r.discipline_outcome)
        .join(', ');
    }
    

    I think is what you want (ES6 style - you might need to rewrite it for ES5 if you need to support older browsers).

    Allan

  • peterbrownepeterbrowne Posts: 314Questions: 54Answers: 0

    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:

     .concat(row.emergency_medicine, row.general_practice, row.internal_medicine)
    

    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

  • allanallan Posts: 61,722Questions: 1Answers: 10,108 Site admin

    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

  • peterbrownepeterbrowne Posts: 314Questions: 54Answers: 0

    Thanks again Allan.

    I might need just a bit more help with that. An example would be nice.

  • kthorngrenkthorngren Posts: 20,302Questions: 26Answers: 4,769
    edited November 2020

    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

  • peterbrownepeterbrowne Posts: 314Questions: 54Answers: 0

    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:

    Emergency Medicine
    [object Object]
    General Practice
    [object Object]
    Internal Medicine
    

    data looks like:

    "emergency_medicine":[{"discipline_outcome_pk":133,"discipline_outcome":"emergency medicine outcome"}],
    

    I tried the following, but not getting the discipline_outcome names:

    ,  {
            data: null,
            render: function (data, type, row) {
              var cellData = ['<b>Emergency Medicine</b>'];
              cellData = cellData.concat(row.emergency_medicine['discipline_outcome']);
              
              cellData.push('<b>General Practice</b>');
              cellData = cellData.concat(row.general_practice['discipline_outcome']);
    
              cellData.push('<b>Internal Medicine</b>');
              cellData = cellData.concat(row.internal_medicine['discipline_outcome']);
              
              return cellData.join('<br>');
            }
          },
    
  • kthorngrenkthorngren Posts: 20,302Questions: 26Answers: 4,769
    Answer ✓

    Your discipline_outcome object is inside an array:

    "emergency_medicine":
    [
      {"discipline_outcome_pk":133,"discipline_outcome":"emergency medicine outcome"}
    ],
    

    You will need to use something like this:

    row.emergency_medicine[0].discipline_outcome
    

    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

  • peterbrownepeterbrowne Posts: 314Questions: 54Answers: 0

    Many thanks, that works.

This discussion has been closed.