R0b0tn1k
R0b0tn1k

Reputation: 4306

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?

Upvotes: 0

Views: 191

Answers (3)

Anand
Anand

Reputation: 5332

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

Upvotes: 1

Mitul Bhalia
Mitul Bhalia

Reputation: 1217

when you set any custom variable in your row object then you can get it back by below code so try it and tell me if you find any problem.

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

Upvotes: 1

Wahhab_mirza
Wahhab_mirza

Reputation: 1482

Why do you try this

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

Thanks

Upvotes: 0

Related Questions