Need help passing data from a custom table row and sqlite table.

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

I am having trouble passing data to the next table/window. How can I get the ID to pass to the next window?

var win = Ti.UI.currentWindow;
 
var db = Ti.Database.install('../2012.sqlite', 'codesTable');
 
function setArray() {
 
    var rows = db.execute('SELECT DISTINCT category, categorySubtitle FROM codes ORDER BY categorySubtitle');
 
var id = Ti.UI.currentWindow.id;
 
    // create the array
    var dataArray = [];
 
    while (rows.isValidRow()) {
        var id = rows.fieldByName('id');
        var row = Ti.UI.createTableViewRow({
            backgroundColor : 'transparent',
            height : 60,
            hasChild : true,
            path : '../products/producs2.js',
        });
 
        var lblTitle = Ti.UI.createLabel({
            text : rows.fieldByName('category'),
            top : 5,
            left : 45,
            width : 250,
            height : 20
        });
 
        row.add(lblTitle);
        var lblDesc = Ti.UI.createLabel({
            text : rows.fieldByName('categorySubtitle'),
            top : 30,
            left : 45,
            width : 215,
            bottom : 10,
            font : {
                fontSize : 12
            }
        });
        row.add(lblDesc);
        dataArray.push(row, id);
        rows.next();
    };
 
    // set the array to the tableView
    tableview.setData(dataArray);
};
// create table view
var tableview = Ti.UI.createTableView({
});
tableview.addEventListener('click', function(e) {
    if (e.rowData.path) {
        var win = Ti.UI.createWindow({
            url : e.rowData.path,
            title : e.rowData.title,
             id: e.rowData.id
        });
 
        Ti.UI.currentTab.open(win);
    }
});
 
setArray()
win.add(tableview);

— asked 10 months ago by mike zal
0 Comments

1 Answer

Accepted Answer

Hi Mike

You are very close, you need to add the id to the row.

var row = Ti.UI.createTableViewRow({
...
  id: id,
...
});
Or
var row = Ti.UI.createTableViewRow({
...
  id: rows.fieldByName('id');,
...
});
You can then retrieve this later using;
var id = Ti.UI.currentWindow.id;

— answered 10 months ago by Malcolm Hollingsworth
answer permalink
3 Comments
  • Malcom,

    I am trying to call it with this code, and it is telling me "WHERE=undefined"

    var rows = db.execute('SELECT * FROM codes ORDER BY subcategorySubtitle WHERE id="' + id + '"');
        var id = Ti.UI.currentWindow.id;
    If I use the following codes I get a blank table.
    var rows = db.execute('SELECT subcategory, subcategorySubtitle FROM codes WHERE id = ?', id);
        var id = Ti.UI.currentWindow.id;
    or
    var rows = db.execute('SELECT * FROM codes WHERE id = ?', id);
        var id = Ti.UI.currentWindow.id;

    — commented 10 months ago by mike zal

  • Go for the second example but put the var = id... ABOVE the rows line.

    You need to assign the id variable before you use it, and right now you are referencing the variable in the rows line BEFORE you have declare and assigned it.

    — commented 10 months ago by Malcolm Hollingsworth

  • Don't forget to call rows.close() and db.close() when you're done with them, or you're going to kill your memory and lock other DB calls.

    — commented 10 months ago by Shannon Hicks

Your Answer

Think you can help? Login to answer this question!