How to access object inside array

How to access object inside array

mgilangmgilang Posts: 6Questions: 1Answers: 0

hi all
i have tried using column render, and it return when theres single object on it
i've got data like this

    "data":[
   {
      "id":"7",
      "item_type":"",
      "location_id":"1,3",
      "product_id":"101",
      "menu":"Mala Traditional Soup Handmade",
      "category":{
         "id":"1",
         "category_name":"Soup",
         "category_slug":"soup",
         "softDelete":0,
         "created_at":"2020-04-27 13:34:14",
         "updated_at":"2020-04-03 21:54:49"
      },
      "location":[
         {
            "id":"1",
            "location_name":"Chongqing Senopati",
            "totalpax":"20"
         }
      },
{
         "id":"3",
         "location_name":"Chongqing Serpong",
         "totalpax":"50"
      }
   }
]
},

i want to access data in "location_name"

already tried with this format, and just got "undefined"

columns: [
             {
               data: null,
                         render: function(data,type,row){
                               return 'data '+data['location']['location_name']+''},
                         orderable: false
            },
]

thanks!

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,171Questions: 26Answers: 4,922

    Take a look at the documentation for the columns.render function. You should be using row instead of data to access the row data. if this doesn't help then please provide a test case showing the issue.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • mgilangmgilang Posts: 6Questions: 1Answers: 0

    hi
    thanks for response

    i could get the data with following format

      data: null,
                             render: function(data,type,row){
                                   return 'data '+data.location[0].location_name+''},
                             orderable: false
    

    but it just show first data only,
    "location_name":"Chongqing Senopati",

    how can i get all data?

  • mgilangmgilang Posts: 6Questions: 1Answers: 0

    hi

    just for update, i can get this in console log
    but it trow alert error

    DataTables warning: table id=items - Requested unknown parameter 'null' for row 0, column 1. For more information about this error, please see http://datatables.net/tn/4

          data: null,
         render: function(data,type,row){
    
           $.each(data.location, function(index, value) {
             console.log(value.location_name);
            return 'data '+value.location_name+'';
           })
         },
    

    what data should i put there?

    thanks!

  • colincolin Posts: 15,237Questions: 1Answers: 2,599

    As Kevin said, please link to a test case - a test case that replicates the issue will ensure you'll get a quick and accurate response. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.

    Cheers,

    Colin

  • mgilangmgilang Posts: 6Questions: 1Answers: 0

    hi, right, sorry
    on my way to create test case
    thanks

  • mgilangmgilang Posts: 6Questions: 1Answers: 0

    Hi
    sorry cant get working on codepen or etc
    here the test page : http://hotpot.didev.id/items
    the json http://hotpot.didev.id/items/json

    many thanks!

  • kthorngrenkthorngren Posts: 21,171Questions: 26Answers: 4,922
    Answer ✓

    According to the jQuery each() docs it seays this about using return:

    Returning non-false is the same as a continue statement in a for loop; it will skip immediately to the next iteration.

      $.each(data.location, function(index, value) {
        console.log(value.location_name);
       return 'data '+value.location_name+'';
      })
    

    Once the loop is done the render function isn't returning anything for the column, resulting in the error you are seeing.

    I took an example of you JSON response and put it in this example:
    http://live.datatables.net/yetoqice/1/edit

    It builds an array of location_name from the location array. The example shows a couple of options of how to return the array depending on how you want to display the data.

    Kevin

  • mgilangmgilang Posts: 6Questions: 1Answers: 0

    many thanks! @kthorngren

    the correct path i tried before is just "data:location"
    i new in js, never use function like "push" before.

    now i learn some!

    thanks!

This discussion has been closed.