Problems with JSONified Table Data

You must Login before you can answer or comment on any questions.

This doesn't work as expected. Table ends up empty. Am I doing something wrong?

APP.JS
Ti.UI.backgroundColor = 'white';
var win = Ti.UI.createWindow();
var table = Ti.UI.createTableView({
  data: [{"title": "Apples"},{"title": "Bananas"},{"title": "Carrots"},{"title": "Potatoes"}]
});
win.add(table);
win.open();
 
win.addEventListener('click', function(e) {
    table.setData(JSON.parse(JSON.stringify(table.getData())));
    alert(table.data);
});
— asked 11 months ago by Chaim Krause
2 Comments
  • Try outputting

    JSON.stringify(table.getData())
    to see what it looks like before you try setting it back into the table. Maybe it's not exactly what you expect?

    — commented 11 months ago by Jason Priebe

  • Well, it's not returning what I thought it would.

    Returns an empty object before click and a object with an empty dictionary afterwards.

    app.js
    Ti.UI.backgroundColor = 'white';
    var win = Ti.UI.createWindow({
        layout: 'vertical'
    });
    var table = Ti.UI.createTableView({
        height: Ti.UI.SIZE,
      // data: [{"title": "Apples"},{"title": "Bananas"},{"title": "Carrots"},{"title": "Potatoes"}]
      data: [{title: "Apples"},{title: "Bananas"},{title: "Carrots"},{title: "Potatoes"}]
    });
    win.add(table);
    var textarea = Ti.UI.createTextArea({
        height: 300
    });
    win.add(textarea);
    textarea.value = JSON.stringify(table.getData());
    win.open();
     
    win.addEventListener('click', function(e) {
        table.setData(JSON.parse(JSON.stringify(table.getData())));
        textarea.value = JSON.stringify(table.getData());
    });

    — commented 11 months ago by Chaim Krause

2 Answers

Remove the quotes from title :.

— answered 11 months ago by Alexander Bauer
answer permalink
2 Comments
  • clean rebuild?

    — commented 11 months ago by Alexander Bauer

  • That won't matter. In JavaScript it will use those names as stings if they aren't surrounded with quotes.

    var tableData = [{"title":'Able'},{title:'Bravo'}];
    console.log(JSON.stringify(tableData));

    — commented 11 months ago by Chaim Krause

It get's weirder. Put a breakpoint on line 39 of the code below. Then just run it in the iPhone simulator. Not in debug mode. Just run it. TextArea shows []. Now run it in the debugger, and when it breaks, just resume (F8) and the TextArea will have: [{"headerTitle":"Fruit"},{"headerTitle":"Vegetables"}]

Ti.UI.backgroundColor = 'white';
var win = Ti.UI.createWindow({
    layout : 'vertical'
});
 
var sectionFruit = Ti.UI.createTableViewSection({
    headerTitle : 'Fruit'
});
var row1 = Ti.UI.createTableViewRow({
    title : 'Apples'
});
var row2 = Ti.UI.createTableViewRow({
    title : 'Bananas'
});
sectionFruit.add(row1);
sectionFruit.add(row2);
 
var sectionVeg = Ti.UI.createTableViewSection({
    headerTitle : 'Vegetables'
});
var row3 = Ti.UI.createTableViewRow({
    title : 'Carrots'
});
var row4 = Ti.UI.createTableViewRow({
    title : 'Potatoes'
});
sectionVeg.add(row3);
sectionVeg.add(row4);
 
var table = Ti.UI.createTableView({
    height : Ti.UI.SIZE,
    data : [sectionFruit, sectionVeg]
});
win.add(table);
var textarea = Ti.UI.createTextArea({
    height: 300
});
win.add(textarea);
textarea.value = JSON.stringify(table.data);  // <<< put breakpoint here
win.open();

Your Answer

Think you can help? Login to answer this question!