east1999
east1999

Reputation: 199

Merge columns in Datatables

I need to merge two columns in Datatables. I tried to use their suggested method for concatenation in columnRender, but the result is blank (or simply di). But even repeating code seems stupid. I simply want to merge the columns' functions, which are the same, but I don't know how:

( data, type, full ) => {
    return $.map( data, function ( d, i ) {
    return d.given +' '+ d.family;
    } ).join( ',<br />' );
    }

See the full JSFiddle for a working example.

FYI, I've been able to amalgamate other JSON fields using this idea, but these fields are different, as they contain arrays, and I cannot find them using full[]family or full.given. Thank you for your time!

Edit: Solved below. I used the answer to load the mapped array2 only if array1 is blank. See fiddle.

Upvotes: 1

Views: 7029

Answers (1)

Gyrocode.com
Gyrocode.com

Reputation: 58880

Use the code below:

"columnDefs": [
    {
        "render": (data, type, full) => {
            return $.map(full['author'].concat(full['editor']), function ( d, i ) {
                return d.given +' '+ d.family;
            }).join( ',<br />' );
        },
        "targets": [2] 
    }
] 

See updated example for code and demonstration.

Upvotes: 2

Related Questions