Ajax server-side data

Ajax server-side data

fmkfmk Posts: 3Questions: 1Answers: 0

Hi,
I use server-side datatables with ajax.I get data without problem.

The data from the server is like this:

data: [
    0: {
        id: 35
        name: "myname lastname"
        created_at: "2020-12-20T21:28:41.000000Z"
        photos: [
            0: {
                file_name: "img_001.jpg"
                folder_name: "avatars"
            }
            1: {
                file_name: "img_002.jpg"
                folder_name: "avatars"
            }
        ]
    }
    1: {
        // similar with data(0)
    }
]

datatables configurations:

columns: [
    {
         data: 'name',   // it displayed in the table.
    },
    {
         data: 'photos.0.file_name'  // it displayed in the table.
    },
    {
         data: null,
         render: function (data, type, row) {
             let path = row['photos.0.folder_name'] + row['photos.0.file_name'];
             return path;   // it doesn't work. Nothing show up
    }
]

How can I display file_name and folder_name at render level?
Thanks

Answers

  • kthorngrenkthorngren Posts: 21,173Questions: 26Answers: 4,923
    edited December 2020

    Its an array of objects. You can loop through the array and build an HTML string that you return after the loop.

    EDIT: You would loop through row.photos.

    Kevin

  • fmkfmk Posts: 3Questions: 1Answers: 0

    I am a beginner at js. I have googled it, but i couldnt find any object loop for my problem.

    Could you send me any link? or just write how can I do this:

    render: function (data, type, row) {
        return row['photos.0.folder_name'] + row['photos.0.file_name'];
    } // I just want the first path of photo, not all paths of photos
    
  • fmkfmk Posts: 3Questions: 1Answers: 0

    never mind. I have already solved it.

    row.photos[0].file_name

    So simple!

This discussion has been closed.