Search code examples
titaniumtitanium-mobile

Titanium: Passing a variable not working


Trying to pass a parameter to addevent, but it's not working.

Putting the table together:

for (var i=0; i<json.objects[0].tour.length; i++){
    var row = Ti.UI.createTableViewRow(
            title = json.objects[0].tour[i].name,
            leftImage = json.objects[0].tour[i].icon,
            hasChild = true,
            rowIndex = json.objects[0].tour[i].id,
            height = 68,
            rowId = i
    );

    var tourdetdatattlwrvw = Ti.UI.createView({top:10, left: 10, width:'100%',height:28});

    var tourdetdatattllbl = Ti.UI.createLabel({
        text:json.objects[0].tour[i].name,
        height:23,
        width:'100%',
        font:{fontSize:20,fontWeight:'bold'},
        color:'#000',
        textAlign:'left'
    });

    tourdetdatattlwrvw.add(tourdetdatattllbl)

    var tourdetdatadescwrvw = Ti.UI.createView({left:10, width:'100%', top:40, height:60});

    var tourdetdatadesclbl = Ti.UI.createLabel({
        text:json.objects[0].tour[i].description,
        font:{fontSize:12,fontWeight:'normal'},
        color:'#000',
        width:'100%',
        height:60
    });

    tourdetdatadescwrvw.add(tourdetdatadesclbl)

    row.add(tourdetdatattlwrvw);
    row.add(tourdetdatadescwrvw);
    tableData.push(row); 
}

var table = Ti.UI.createTableView({
    data:tableData
});

Then the event listener:

table.addEventListener('click', function(e) {
    activeTour = e.source.rowId;
    alert(activeTour)
        alert(rowIndex)
})

And of course it's not working.

What am i doing wrong?


Solution

  • I think there is a mistake in the following part of your code

    var row = Ti.UI.createTableViewRow(
        title = json.objects[0].tour[i].name,
        leftImage = json.objects[0].tour[i].icon,
        hasChild = true,
        rowIndex = json.objects[0].tour[i].id,
        height = 68,
        rowId = i
    );
    

    Try changing it to

    var row = Ti.UI.createTableViewRow({
        title : json.objects[0].tour[i].name,
        leftImage : json.objects[0].tour[i].icon,
        hasChild : true,
        rowIndex : json.objects[0].tour[i].id,
        height : 68,
        rowId : i
    });
    

    and you can take the value as

    table.addEventListener('click', function(e) {
        activeTour = e.rowData.rowId;
         alert(activeTour);
    });
    

    Hope it helped you. Let me know if you have any issues