I have tried this a million different ways and I cant seem to get it to work. I have a page on a server that outputs a JSON dataset. I want to capture that data and put it into a table. it seems like the downloading of the JSON works but I cant figure out how to assign the data to the tabledata variable. What is not clear to me is how to set "tabledata = json;" here is my code which results in this error.
ERROR
[WARN] Exception in event callback. { line = 67; message = "'undefined' is not an object (evaluating 'tabledata.length')"; name = TypeError; sourceId = 140634112; }
CODE
function exhibitorsCat(_args) { var win = Ti.UI.createWindow({backgroundColor:'#fff'}); var isMobileWeb = Titanium.Platform.osname == 'mobileweb'; //THIS COMMENTED CODE WORKS //create table view data object /* var tabledata = [ {title:'Company 1', category:['Contract Fabrics'], hasChild:true, selectedBackgroundColor:'orange', test:'ui/common/showtime/exhibitor'}, {title:'Company 2', category:['Contract Fabrics'], hasChild:true, selectedBackgroundColor:'orange', test:'ui/common/showtime/exhibitor'}, {title:'Company 3', category:['Designer Friendly'], hasChild:true, selectedBackgroundColor:'orange', test:'ui/common/showtime/exhibitor'}, {title:'Company 4', category:['Eco Friendly'], hasChild:true, selectedBackgroundColor:'orange', test:'ui/common/showtime/exhibitor'}, ]; */ //THIS CODE DOES NOT // var f = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, '/showtime/exhibitors.JSON'); var tabledata; var url = "http://xxxxxxx.com/exhibitorsJSON.cfm"; var client = Ti.Network.createHTTPClient({ // function called when the response data is available onload : function(e) { var json = JSON.parse (this.responseText); tabledata = json; }, // function called when an error occurs, including a timeout onerror : function(e) { // need a real error handler here... Ti.API.debug(e.error); alert('error'); }, timeout : 5000 // in milliseconds }); // Prepare the connection. client.open("GET", url); // Send the request. client.send(); // Ti.include("/etc/readJSON.js"); // var tabledata = readJSON(exhibitors.JSON) Ti.include("/etc/filterExhibitors.js"); if (Titanium.Platform.name == 'iPhone OS') { Ti.include("/etc/version.js"); if (isiOS4Plus()) { } } // create table view for (var i = 0; i < tabledata.length; i++ ) { tabledata[i].color = '#000'; tabledata[i].font = {fontWeight:'bold'} }; var tableview = Titanium.UI.createTableView({ data: filterExhibitors(tabledata, _args.title), //filter the exhibitors list on the title passed style:1 }); // create table view event listener tableview.addEventListener('click', function(e) { if (e.rowData.test) { var ExampleWindow = require(e.rowData.test), win = new ExampleWindow({title:e.rowData.title,containingTab:self.containingTab,tabGroup:self.tabGroup}); if (Ti.Platform.name == "android") { } else { win.backgroundColor = "#fff"; win.barColor = "#111"; win.barColor='orange'; win.title=e.row.title; } if (e.index == 3) { if (Ti.Platform.name == "iPhone OS") { win.hideTabBar(); } } if (Ti.Platform.name==='android' && e.rowData.test.indexOf('window_properties') >= 0) { // As explained in apidoc for Window, if opacity is ever to be changed for an Android // activity during its lifetime, it needs to use a translucent background. We trigger // using a translucent theme by the presence of the opacity property, so we need to // set it here. Setting it to 1 means it's totally opaque, but gives us the property to // make it more transparent later with the "toggle opacity" test. win.backgroundColor = "#191919" win.opacity = 1; } _args.containingTab.open(win,{animated:true}); } }); // add table view to the window win.add(tableview); return win; }; module.exports = exhibitorsCat;
1 Answer
i think the problem, is that you set the data for your tableview before the download ist finished, so nothing appears in the tableview. 1. get the json data 2. parse the string in the onload callback 3. also refresh the tableview in the callback
var tabledata = []; var tableView = Ti.UI.createTableView(); var xhr = Ti.Network.createHTTPClient({ onload: function(e) { // parse the reponse string into json array tabledata = JSON.parse(this.responseText;) // populate the tableView tableView.setData(tabledata); }, onerror: function(e) { Ti.API.debug(e.error); alert('error'); }, timeout:5000 }); xhr.open("GET", url); xhr.send();
Your Answer
Think you can help? Login to answer this question!