How to merge 2 json properties into 1 column

How to merge 2 json properties into 1 column

howster2024howster2024 Posts: 2Questions: 1Answers: 0
edited April 5 in Free community support

Link to test case:
Debugger code (debug.datatables.net):
Error messages shown:
Description of problem:

Reading https://datatables.net/manual/data/renderers and wondering how to merge 2 json properties into 1 column.

Let's say I have this data

courseSubjectDescription => Math is great
termCodeDescription => Term 1
courseId => 123
courseTitle => Math 101
courseDescription => Very hard course
courseYear => 2024

This code works

  new DataTable('#example', {
    ajax: {
        url: 'data.json',
        dataSrc: ''
    },
    columns: [
        { data: 'courseSubjectDescription' },
        { data: 'termCodeDescription' },
        { data: 'courseId' },
        { data: 'courseTitle' },
        { data: 'courseDescription' }
    ]
});

Output:

[Math is great] [Term 1] [123] [Math 101] [Very hard course] => 5 cols

Now I want to merge data: 'courseId' with data: 'courseYear'

  new DataTable('#example', {
    ajax: {
        url: 'data.json',
        dataSrc: ''
    },
    columns: [
        { data: 'courseSubjectDescription' },
        { data: 'termCodeDescription' },
        { data: 'courseId'},
        { data: 'courseId' + '-' + 'courseYear' },  <=whats the correct syntax??
        { data: 'courseDescription' }
    ]
});

Desired Output:

[Math is great] [Term 1] [123-2024] [Math 101] [Very hard course] => 5 cols

How do I achieve that?

Tried below unsuccessfully.

{data: null,
     render: function ( data, type, row ) {
        return data.courseId +' -'+ data.courseTitle;
    },

and

 { data: null,
    render: function ( data, type, row ) {
        return ('row.courseId' + '-' + 'row.courseYear');
    },

Thanks

Answers

  • howster2024howster2024 Posts: 2Questions: 1Answers: 0
    edited April 5

    This works...may help others

         {
        data: null,
        render: function ( data, type, row ) {
            return data.courseId +'-'+ data.courseYear;
        }
    },
    
  • kthorngrenkthorngren Posts: 20,452Questions: 26Answers: 4,802

    Glad you found a solution. This example also shows the same technique.

    Kevin

Sign In or Register to comment.