Accessing nested array of objects from dataTable row

Accessing nested array of objects from dataTable row

iampapagrayiampapagray Posts: 2Questions: 1Answers: 0

I have an api response of data that im using to populate my table. the data looks like this:
{ code: 'CPX154', identity: '108', name: 'Joshua Laryea', created_at: '12:23 pm', phone: '0559546287', pickup: [ {desc: 'Shirts', quantity: '15'}, {desc: 'Suit', quantity: '2'}, {desc: 'Jeans', quantity: '6'} ] }, { code: 'CGH154', identity: '108', name: 'Gloria Fynn', created_at: '15:23 pm', phone: '0249563287', pickup: [ {desc: 'Shirts', quantity: '5'}, {desc: 'Trouser', quantity: '2'}, {desc: 'Shorts', quantity: '1'}, {desc: 'Suit', quantity: '2'}, {desc: 'Jeans', quantity: '6'} ] }
After doing a JSON.parse(data) i am able to access the data like any other normal object and have used it to populate the datatable. My issue is that on click of any of the table row, when i get access to that row's data the field 'pickup' shows [object object] and i cannot parse it with JSON also. it results in an error.

please help

Answers

  • kthorngrenkthorngren Posts: 21,303Questions: 26Answers: 4,947

    Maybe this example will help:
    https://datatables.net/examples/ajax/deep.html

    Without seeing your Datatables init code its difficult to help move you in the right direction.

    Kevin

  • iampapagrayiampapagray Posts: 2Questions: 1Answers: 0

    this is my init code

    $('#ultronData').DataTable({
                    dom: "lBfrtip",
                    columns: [
                        { data: 'id', orderable: false },
                        { data: "code", className: 'details-control' },
                        { data: "identity" },
                        { data: "full_name" },
                        { data: "created_at" },
                        { data: "phone" },
                        { data: "pickup" }
                    ],
                    columnDefs: [
                        { targets: [0,6], visible: false, searchable: false}
                    ],
                    order: [ 2, 'asc' ],
                    buttons: [/*{ extend: "create", editor: editor }*/]
    
                
                });
    
  • kthorngrenkthorngren Posts: 21,303Questions: 26Answers: 4,947

    One option is to use columns.render to loop through the pickup array and append each object to a string that will be returned from the function for display.

    Is the question you have around editing the field? Maybe bubble editing would be a good option?
    https://editor.datatables.net/examples/bubble-editing/index.html

    Kevin

This discussion has been closed.