Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
289 views
in Technique[技术] by (71.8m points)

javascript - Nested JSON data into Datatable not working

So I have a pretty long (nested) JSON array (I've minified it):

      {
  "items" : [ {
    "track" : {
      "album" : {
        "name" : "Pressure Makes Diamonds"
      },
      "artists" : [ {
        "name" : "Danny Vera"
      } ],
      "href" : "https://api.spotify.com/v1/tracks/7rO7Pc5dkC2EIW1OKsCJtQ",
      "name" : "Roller Coaster"
    }
  }, {
    "track" : {
      "album" : {
        "name" : "Amigo"
      },
      "artists" : [ {
        "name" : "Chef'Special"
      } ],
      "href" : "https://api.spotify.com/v1/tracks/6yvxKrf9KZLTodXFTBmdR1",
      "name" : "Nicotine"
    }
  }, {
    "track" : {
      "album" : {
        "name" : "Lines (New Version)"
      },
      "artists" : [ {
        "name" : "Niels Geusebroek"
      } ],
      "href" : "https://api.spotify.com/v1/tracks/62En58kZelNGnGvmJUMVC0",
      "name" : "Take Your Time Girl (live at ruud de wild/538)"
    }
  } ]
}

and I'm trying to display it in a Datatable with this code:

<script>
        $(document).ready(function() {
            $('#table_id').DataTable( {
                processing: true,
                data: {!! $response !!}, // <= this is where the JSON comes from (Laravel)
                columns: [
                    { "track" : "name" },
                    { "track" : "album.name" },
                    { "track" : "artists[0].name" }
                ]
            })
        })
    </script>

Anyone got any idea what I'm doing wrong?
I looked up the Datatables (Datatables) and followed it, but it still doesn't work...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Ait got it:

<script>
        $(document).ready(function() {
            $('#table_id').DataTable( {
                processing: true,
                data: {!! $response !!}["items"],
                columns: [
                    { "data" : "track.name" },
                    { "data" : "track.album.name" },
                    { "data" : "track.artists[0].name" }
                ]
            })
        })
    </script>

Thanks yall!!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.7k users

...