Search code examples
uitableviewtitanium

Titanium TableView row column


I have the following thing to do:

  • Create a TableView with 2 columns
  • When I click on the first column, the row should be removed
  • When I click on the second column, the associated product should be displayed

I put an eventListener on the TableView which works fine. Unfortunately I have no idea how to separate the 1st column event from the 2nd column event. Any ideas?

Here is the source code:

var viewResults = Titanium.UI.createView({
  ...
});
...
for (rowId in rows) {
  var tableRow = Titanium.UI.createTableViewRow();
  var rowDelete = Titanium.UI.createView({
    ...
  });
  tableRow.add(rowDelete);
  var rowProduct = Titanium.UI.createView({
    ...
  });
  tableRow.add(rowProduct);
  tblData.push(tableRow);
}
tblResults.setData(tblData);
... 
tblResults.addEventListener('click', function(e){
   if (firstColumn) {
     ...
   }else{
     ...
   }
});

Solution

  • You can add custom property to views which you are creating:

    var rowDelete = Titanium.UI.createView({
        action: 'delete',
        ...
    });
    var rowProduct = Titanium.UI.createView({
        action: 'product',
        ...
    });
    

    and then in eventListener check event.source property:

    tblResults.addEventListener('click', function(e){
        if (e.source.action === 'product') {
            ...
        } else if (e.source.action === 'delete') {
            ...
        }
    });