GetJSON
query data from json file
by Meily Chhon
JavaScript
window.addEvent('domready', function(){
//create table as a global variable
var table;
//create an html <table>
var table = new HtmlTable ({
properties: {
border: 1,
cellspacing: 2
},
headers: ['Name','Address']
});
var request = new Request({
url: 'json/data.json',
method: 'get',
headers: {
'Accepts': 'Application/JSON'
},
onSuccess: function(text) {
var data = JSON.decode(text);
//this is a function to add rows to table;
var li = $$('li');
li.addEvent('click', function(){
addRow();
});
function addRow() {
//loop to create 10 table rows
for(var i=0; i<data.length; i++){
console.log(data.length);
var linkTmp = data[i].name;
var linkTextTmp = data[i].href;
var addressTmp = data[i].address;
//console.log(linkTextTmp);
var aTag = new Element ('a', {
href: linkTextTmp,
title: linkTmp,
html: linkTmp
});
//add row to table
table.push([aTag, addressTmp]);
}
//inject table to div pop-up-content
table.inject($('pop-up-content'));
}
},
}).send();
});