Hi All, I am using TableView for the first time and I need to solve a little problem, I hope you can help.
I have a SQLite database with products, I am dynamically populating the TableView rows with the data. When the table is created, automatically, each row is assigned an index number (The first is 1, the second is 2 etc).
I want to click on the product, then open a dialogue window to update the price of the product. To do that, I need to know what the product ID is from the product db.
Assume I have 3 columns in my DB 'main' in table 'allProducts': - productID - productName - productPrice
So I open the database
var myProductDB = Titanium.Database.open('main'); var myResultSet = myAccountDB.execute('SELECT * FROM allProducts); while (myResultSet.isValidRow()) { newProductSection.add(Titanium.UI.createTableViewRow( { myResultSet.fieldByName('productName')'}) ); } // this all works, it returns a list of product namesBut the productID does not necessary match the index number, e.g. the first row has a table index number of 1 but the productID number could be 482.
When I click on the product name/row, I need to be able to access the associated product ID. Currently, all I can do is alert out the index number of the row in the context of the table.
Like so
myTableView.addEventListener('click', function(e) { Titanium.UI.createAlertDialog( { title:'Alert', message:'You selected row number: '+e.index }).show(); }
So the index number is related to the position in the table, not the actual ID of the product from the product database. How do I get/attach the product ID to the row? I need to be able to click on the row, select the product and then e.g. update the price of the product. How do I identify this product?
So what I'd like to achieve is something like this (but this is wrong)
myTableView.addEventListener('click', function(e) { Titanium.UI.createAlertDialog( { title:'Alert', message:'You selected product name' + myResultSet.fieldByName('productName') + ' and the associated product number is: '+myResultSet.fieldByName('productID') }).show(); }
Any thoughts? Thanks, Lee
1 Answer
Accepted Answer
F,
You're not explicitly assigning properties within your tableViewRow, Titanium is probably assigning the product name to the row title property automagically. Try this out (not tested)...
Instead of this...
newProductSection.add(Titanium.UI.createTableViewRow( { myResultSet.fieldByName('productName')}) );...try this:
newProductSection.add(Titanium.UI.createTableViewRow( { title:myResultSet.fieldByName('productName'), //set custom property productID:myResultSet.fieldByName('productID')}));Listener:
myTableView.addEventListener('click', function(e) { Titanium.UI.createAlertDialog( { title:'Alert', message:'You selected row number: '+e.index+' and productID: '+e.row.productID }).show();Give it a try, see also the "Master/Detail" template you can create with Ti Studio, it has a simple example of how properties are passed by event listeners, as well as a method to separate your data and view code.
I have a similar example here.
HTH. Bob
Your Answer
This question has been locked and cannot accept new answers.