javascriptdom-eventstitaniumappcelerator-mobile

Get parent property which is a table from a child in titanium


I have a checkbox as a button and have added it to table's row view. How to access the row index by using the checkbox. Sample code is like below. The EmpList[i] I get it from another function.

var createTablerow = function(){
var empSelectView = Ti.UI.createView({
     width : '30%',
     height : '100%',
     layout : 'vertical',
     id: EmpList[i].empID
 });
 
var typeCheckBox = Titanium.UI.createButton({
    backgroundImage: 'images/uncheck.png',
    id: EmpList[i].empID,
    checked:false,
    width: 25,
    height: 25,
    left: 5,
    right: 5,
    top:15
    });
    
  empSelectView.add(typeCheckBox);
  labelView.add(empSelectView);
  labelView.add(empNameView);
  rowView.add(labelView);
  
 typeCheckBox.addEventListener('click',function(e){
if (e.source.checked == true){
    e.source.image = 'images/uncheck.png';
    e.source.checked = false;
    var index = empDelList.indexOf(e.source.id);
    var selRowId = deleteIndexList.indexOf(e.index);
    if (index > -1) {
        empDelList.splice(index, 1);
        deleteIndexList.splice(selRowId, 1);
    }
} else {
    e.source.image = 'images/check.png';
    e.source.checked = true;
    empDelList.push(e.source.id);
    deleteIndexList.push(e.index);
        }
  });
}

How to access e.index of the table inside the listener of typeCheckBox which is a child of the table row. In the deleteIndexList Array I want to push the table index that are selected.


Solution

  • I'd add an click event listener to the TableView (which gives you the index) and then look at the source property of the event to determine if the user tapped on the button.