getting attributes from a view in a loop

You must Login before you can answer or comment on any questions.

Am I missing something here? I thought this would work.

function showChildren(obj) {
 
    var data = [];
 
    for (var c = 0; c < obj.length; c++) {
 
        var title = obj[c].firstname + " " + obj[c].lastname;
        var id = obj[c].id;
 
        alert(title);
        var picture = obj[c].profilePic;
        if(picture === ""){
            picture = "http://localhost/default/no.jpg";
        }
 
 
        var post_view = Ti.UI.createView({
            height : '120dp',
            id :id,
            left : 5,
            top : 5,
            bottom : 5,
            right : 5,
        });
 
 
        var av = Ti.UI.createImageView({
            image : picture,
            left : 2,
            top : 2,
            height : 100,
            width : 100
        });
        post_view.add(av);
 
 
        var user_label = Ti.UI.createLabel({
            text : title,
            left : 105,
             top : 0,
            height : 'auto',
            textAlign : 'left',
            color : '#444444',
            font : {
                fontFamily : 'Trebuchet MS',
                fontSize : 30,
                fontWeight : 'bold'
            }
        });
 
        post_view.add(user_label);
 
        data[c] = post_view;
    }
 
    var view = Ti.UI.createView({
         layout : 'vertical',
 
        backgroundColor:'#D9D9D9'
    })
    view.add(data);
 
    view.addEventListener('click',function(e){
        alert(e.post_view.id);
    })
 
    self.add(view);
 
    }
It says it cannot find "e.post_view.id". I'm wanting it so when i click on the view it gives me the id from that view.

Any ideas?

Thanks.

1 Answer

Accepted Answer

Hi Michael, can you put it here whats the value of e you are getting on click event?

also i noticed semicolon is missing at event click and createView function call, so correct it as well.

— answered 7 months ago by Ashish Nigam
answer permalink
2 Comments
  • the value of e is:

    {
    globalPoint = {
         x = 111;
         y = 107;
         };
    source = "[object TiUILabel]";
        type = click;
        x = 1;
        y = 32;
       }
    I have found out i can use e.source.id but it only works if i don't click on the image or label. I could add the id attribute to the image and label but i don't think that would make for good code.

    — commented 7 months ago by Michael Sagar

  • yes you are right Michael that repeating a single property in all the views is not a good idea, but i think you are adding label and image view in a view so check the id property of parent.

    — commented 7 months ago by Ashish Nigam

Your Answer

Think you can help? Login to answer this question!