event Listener in tableView
Hi guys.
I have a few lines in a tableView that a want with a eventlistener (with a message).. Theres is alredy an eventListener added.. but how do i link it with each "title" ?? Heres the Code:
if (Ti.Platform.name == 'android')
{
Titanium.UI.currentWindow.backgroundColor = '#4e5c4d';
}
else
{
Titanium.UI.currentWindow.backgroundColor = '#aebcad';
}
// create table view data object
var data = [
{title:'Turtyp', header:'Viktigt att tänka på'},
{title:'Manuell lossning'},
{title:'Pant returer'},
{title:'Kund signatur'},
];
if (Titanium.Platform.name == 'iPhone OS')
// create table view
var tableViewOptions = {
data:data,
style:Titanium.UI.iPhone.TableViewStyle.GROUPED,
backgroundColor:'transparent',
rowBackgroundColor:'white'
};
var tableview = Titanium.UI.createTableView(tableViewOptions);
// create table view event listener
tableview.addEventListener('click', function(e)
{
if (e.rowData.test)
{
var win = Titanium.UI.createWindow({
url:e.rowData.test,
title:e.rowData.title
});
Titanium.UI.currentTab.open(win,{animated:true});
}
});
// add table view to the window
Titanium.UI.currentWindow.add(tableview);
4 Answers
-
evt.index is the row number that was clicked.<br>
evt.section is the TableViewSection that contains the row that was clicked.<br>
evt.row is the TableViewRow that was clicked.<br>
evt.rowData is the row properties "object" for the row that was clicked.<br>
<br>
Your code looks fine, except that you are referencinge.rowData.testand I don't see that any of your rows have a "test" property. What is it that isn't working? -
Like Shawn said, you don't have e.rowData.test in your table data, so that if condition never passes. I think that the Kitchen Sink examples may not be clear for you here. To get it to work, try adding the window urls to the table date like this:
var data = [ {title:'Turtyp', header:'Viktigt att tänka på', test:'turtyp.js'}, {title:'Manuell lossning', test:'manuell.js'}, {title:'Pant returer', test:'pant.js'}, {title:'Kund signatur', test:'pant.js'}, ];Of course you need those files to exist and contain your window data. For more info on tableview rows, check out the TableViewRow object and consider using it as you build your app.
-
Ah… about the "test" . the code is copied from another of my windows.. i Dont want it to link typ another .js file.. i just want to add an Alert-message to the Title-text ('turtyp', 'manuell lossning' etc). Are you guys following or am i explaining it like an idiot :P PS. What is the 'rowData.test'? I have it in my other codes (from kitchensink)..
-
At the top of your code, add
var window = Ti.UI.currentWindow;Then, at the bottom, change
Titanium.UI.currentWindow.add(tableview);towindow.add(tableview);.Last, make your tableview eventlistener look like this:
tableview.addEventListener('click', function(e) { window.title = e.rowData.title; });Also, when working in the Q&A, please comment on answers using the 'add comment' link at the bottom of the answer, and only use the 'Your Answer' box when adding an answer to the question. It helps keep the q&a readable for future visitors to the question.