I have the following array, which is extracted to from MySQL database using PDO.
[{
"tbl":"1",
"orid":"915",
"date":"2021-12-30 12:46:48",
"flag":0
},
{
"tbl":"2",
"orid":"914",
"date":"2021-12-30 12:46:21",
"flag":0
},
{
"tbl":"3",
"orid":"913",
"date":"2021-12-30 12:45:00",
"flag":0
},
{
"tbl":"1",
"orid":"911",
"date":"2021-12-30 12:27:17",
"flag":1
}]
I am trying to retrieve this data via Ajax and build/attach div’s accordingly. It doesn’t work as I am failing at even displaying the data parameters and keys. This is the success(data) function:
$.each(data,function(i,j){
content ='<span>'+j[i]+'<br /></span>';
$('.timeline').append(content);
});
I want to place the data in the following format:
<div tbl="1">
<span class="header">Ref 1</span>
<div orid="915" class="order">...</div>
<div orid="911" class="order">...</div>
</div>
<div tbl="2">
<span class="header">Ref 2</span>
<div orid="914" class="order">...</div>
</div>
<div tbl="3">
<span class="header">Ref 3</span>
<div orid="913" class="order">...</div>
</div>
The tricky part is to attach each order/orid to an existing tbl div or create a new tbl div if it doesn’t exist yet because of recurring ajax calls (each time loading 10 rows).
2
Answers
Here is an example by your sample data, instead of
tbl, orid
I’ve useddata-tbl
anddata-orid
attributes.Consider the following.
You can see here how it might be easier to break up the different scenarios with Functions. This way you can make a new Table or update an existing table as needed.
Update
You may consider using DataTables. Example: https://jsfiddle.net/Twisty/u6bdcsqz/51/
Primary Code:
It’s more than you may want yet it give you all the features you are asking about.