This is partly an extension to a Previous Question of mine:
I can now successfully returned JSON data from my CI controller which returns:
{"results":[{"id":"1","Source":"My Source","Title":"My Title". . . .
The Controller:
function outputAjax()
{
$this->load->model('my_model');
$data['results'] = $this->site_model->getInfo();
$this->output->set_output(json_encode($data));
}
This is my current script I am using to retrieve the .ajax
request:
$(document).ready(function()
{
$.ajax(
{
url: 'http://localhost/project/index.php/controller/outputAjax',
dataType:'json',
success: function(data)
{
var limit = 19; //how many json objects are needed initially
var arr = []; // item.id of objects that have been used
$.each(data.results, function(index,item)
{
if (index > limit) return false; // gets required initial json objects
arr.push(item.id); // stores used json objects id's
// whole bunch of checks on item.id , item.Title, item.Description, etc...
$("#masonry-content").append('<div class="item" ' + item.id + item.Title + ' ... </div>');
});
As the above code shows, I am currently taking the first 20 objects of JSON data and appending them to the page container. This works fine but..
The issue I am having is that I would like to update the content in those 20 divs that have been appended to the page on a time interval
with other objects of the initially returned JSON data. I feel like the best way to do this is to layout the div's statically on the page since there will always be the same amount and then just update the data in those divs accordingly.
My question is how can I still access the returned JSON data once outside the scope of the initial .ajax call.
For example: If I would leave the script setup the way it is, after I make the initial .ajax
call and append()
the content to the container with the first 20 JSON data objects ( item
). How do I access the rest of the objects in the JSON data?
I already assumed my best bet to keep track of the objects returned was to store the item.id of each object in an array arr[ ]
Please correct me if this does not make sense or their is a much easier way. But, I still don't know how to access the leftover JSON data objects?
I feel as though I am missing something obvious such as making my javascript more object oriented would help so then I could access each json object individually and perform different functions on it but I feel as though I may be lost a little.