DataTables - display array elements in 'reveal details' of table

DataTables - display array elements in 'reveal details' of table

TonyRTonyR Posts: 27Questions: 6Answers: 0

I am working with the 'reveal details' functionality that is outlined at https://datatables.net/examples/server_side/row_details.html. In the columns definition, elements of an array are separated by commas by using code like:

Javascript:

{ data: 'features_table', render: "[, ].feature_text"},

which shows in the column each 'feature' selected for a record in a many-to-many relationship. This is handy and works very well. How can I display the same comma-separated list of values in the 'reveal details' table methodology using:

function format ( d ) {
    return '<table>' +
        '<tr class="table-info">' +
        '<td>' + d.<data> .....
}

I think I have to establish a variable and access it from from the format function?

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,300Questions: 26Answers: 4,945

    d is the data in the row. Without seeing your data structure its hard to say exactly but you can access the data and use maybe use toString() or join() to create a comma separated string from an array. Before the return you can use console.log(d); to see the data structure passed to the format function.

    Kevin

  • TonyRTonyR Posts: 27Questions: 6Answers: 0

    Thanks for your response. The data's format as read from the console (it shows up when I click to reveal the details) looks like:

    console.log(d);
    

    Array(4)
    0: {id: "8", feature_text: "apples"}
    1: {id: "7", feature_text: "bananas"}
    2: {id: "6", feature_text: "peaches"}
    3: {id: "1", feature_text: "grapes"}
    length:4

    The data is accurate.

    console.log(d.dt_features.join());
    

    returns:
    [object Object], [object Object], [object Object], [object Object]

  • kthorngrenkthorngren Posts: 21,300Questions: 26Answers: 4,945
    Answer ✓

    You have an array of objects. You will need to iterate the array and process the objects. Maybe this code will get you started:

                 var output = [];
                 for (i = 0; i < d.length; i++) {
                   console.log(d[i].feature_text);
                   output.push(d[i].feature_text);
                 }
                 console.log(output.join());
    

    Kevin

  • TonyRTonyR Posts: 27Questions: 6Answers: 0

    Ah! Many thanks, Kevin, you helped me immensely. I did have to insert 'features_table' between the d and the [], like this:

        var output = [];
        for (i = 0; i < d.features_table.length; i++) {
            console.log(d.features_table[i].feature_text);
            output.push(d.features_table[i].feature_text);
        }
        console.log(output.join()); 
    

    But this is perfect and my understanding is much better. Thanks again.

This discussion has been closed.