Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Click Event Propagation

When adding a view with buttons to a table row, is there anyway to stop the click event propagation so that it doesn't bubble up to the table row when I click a button. here's the example code below:

    var row = Ti.UI.createTableViewRow({
        hasChild: true, 
        id: win.allTodos[i].id, 
        zIndex:1, 
        test: 'privateTodos.js'
    })

    var checkBox = Ti.UI.createButton({
        backgroundImage: '../images/icons/slug-check.png', 
        zIndex: 10,
        height: 22, 
        width: 22, 
        left: 0, 
        top: 10, 
        zIndex:10
    }); 

    checkBox.addEventListener('click', function(e){
        alert('you just clicked the button'); 
    }); 


    row.addEventListener('click', function(e){
        alert('you clicked a row'); 
    });
— asked March 15th 2010 by John Wilson
  • event
  • propagation
  • row
  • stop
  • table
  • view
0 Comments

1 Answer

  • Hi John Wilson,

    You can follow my sample code. I think it will be helpful for you. Hear button and row has added different click events which you want.

    var win = Ti.UI.createWindow({
        backgroundColor : 'white'
    });
    var tableData = [];
    var row = Ti.UI.createTableViewRow({
        backgroundColor : "green",
        width : Titanium.UI.FILL,
        height : "100",
    });
    
    var fstName = Ti.UI.createLabel({
        color : '#000',
        font : {
            fontFamily : 'Arial',
            fontSize : 20,
            fontWeight : 'normal'
        },
        text : "Name",
        left : 20,
        top : 30,
        width : 360
    });
    
    row.add(fstName);
    var btn = Ti.UI.createButton({
        title : 'Button',
        backgroundColor : 'red',
        top : 20,
        //left : 350,
        right : 60,
        height : 50,
        id : "test",
        width : 100
    });
    
    row.add(btn);
    tableData.push(row);
    
    var tableView = Ti.UI.createTableView({
        data : tableData
    });
    
    tableView.addEventListener('click', function(e) {
        if (e.source.id == "test") {
            alert('you just clicked the button');
        } else {
            alert('you clicked the row');
        }
    
    });
    
    win.add(tableView);
    win.open();
    

    Thanks

    — answered September 4th 2013 by Motiur Rahman
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.