how to loop inside datatables?
how to loop inside datatables?
Sultan216
Posts: 6Questions: 0Answers: 0
so I have a json like this
{
"data": [
{
"ID": 2,
"CreatedAt": "2021-03-14T08:38:36.285+07:00",
"UpdatedAt": "2021-03-14T08:38:36.446+07:00",
"title": "Mercenary Enrollment",
"alternative": "입학용병; Ibhag yongbyeong",
"Chapter": [
{
"ID": 30,
"CreatedAt": "2021-03-14T08:42:31.244+07:00",
"UpdatedAt": "0001-01-01T00:00:00Z",
"Name": "Chapter 2",
"Slug": "chapte-2",
"ChapterRefer": 2
},
{
"ID": 31,
"CreatedAt": "2021-03-14T08:45:23.682+07:00",
"UpdatedAt": "0001-01-01T00:00:00Z",
"Name": "Chapte 2",
"Slug": "chapte-2",
"ChapterRefer": 2
}
]
}
]
}
then how to call only the name of the chapter to be included in the datables via ajax? I've tried the tutorial from this website and it's still failing. Please help me
This discussion has been closed.
Replies
I'm not clear what you're after, but if you want the Chapter data in the table, set
ajax.dataSrc
to bedata.Chapter
,Colin
@colin
after doing that, I got an error
"Uncaught TypeError: Cannot read property 'length' of undefined"
this is the code i use
That's because you've defined both
columns
andcolumnDefs
. RemovecolumnDefs
, and add those options tocolumns
, something like:Colin
@colin still error "Uncaught TypeError: Cannot read property 'length' of undefined
"
this is the code
json
Trying using the
defaultContent
with thedata: null
column. Something like this:Kevin
@kthorngren
it doesn't work
The
data
object of the JSON response is an array. So you will need something like this:This accesses the first element in the array.
Kevin
@kthorngren it doesn't work either
"DataTables warning: table id=DataTables_Table_0 - Requested unknown parameter 'ID' for row 0, column 0. For more information about this error, please see http://datatables.net/tn/4"
printing the text below into the table
[
o
b
j
e
c
t
]
etc
I copied your code and the sample data into this test case:
http://live.datatables.net/tozebovi/1/edit
It does work. The JSON response will need to be debugged. Look at the JSON response using the browser's network inspector. Is it the correct structure?
Or post a link to your page or a test case replicating the issue. You could grab a debugger trace for the developers to look at. See the instructions here.
Kevin
@kthorngren
I don't know why it worked. but when i put on ajax from url it didn't work. and I think the json format is correct
As Kevin said, to progress this. please post a link to your page for us to look at, or update the test case to do so.
Colin