简体   繁体   中英

Javascript Titanium, Looped label only show the last data out of the array

I've made a loop & used it in an var label. The idea was that it'll show every row out of the data array. Unfortunally it only shows the last row. Please help me out :/

This is my code:

for (var i=0; i<data.length; i++) {
item = data[i];



var togetherh = Titanium.UI.createLabel({

text : data[i].instelling_title,
font : {
    fontSize : 24,
    fontFamily : 'Helvetica Neue',
    fontWeight : 'bold'
},
color : 'black',
top : '10',
width : '100%',
textAlign : 'center',
height : 'auto',
left : 'auto',
touchEnabled : false

});

var together = Titanium.UI.createLabel({

text : data[i].instelling_id + '  ' + data[i].instelling_desc,
font : {
    fontSize : 12,
    fontFamily : 'Helvetica Neue'
},
color : 'black',
top : '50',
width : '85%',
textAlign : 'left',
height : 'auto',
left : 18,
touchEnabled : false

});
}

This is my Data Array:

var data = [
 {item: 'SCALDA 1', instelling_title: 'Scalda', instelling_desc: ''},
 {item: '14', instelling_title: 'Scalda', instelling_desc: ''},
 {item: '15', instelling_title: 'Scalda', instelling_desc: ''},
 {instelling_id: '16', instelling_title: 'Scalda', instelling_desc: ''},
 {instelling_id: '17', instelling_title: 'Scalda', instelling_desc: ''},
 {instelling_id: '18', instelling_title: 'Scalda', instelling_desc: ''},
 {instelling_id: '19', instelling_title: 'Scalda', instelling_desc: ''},
 {instelling_id: 'ROC22', instelling_title: 'ROC', instelling_desc: ''},
 {instelling_id: '21', instelling_title: 'Scalda', instelling_desc: ''},
 {instelling_id: '22', instelling_title: 'Scalda', instelling_desc: ''},
 {instelling_id: '13', instelling_title: 'Scalda', instelling_desc: ''},
 {instelling_id: '12', instelling_title: 'Scalda', instelling_desc: ''},
 {instelling_id: '3', instelling_title: 'Scalda', instelling_desc: ''},
 {instelling_id: '4', instelling_title: 'Scalda', instelling_desc: ''},
 {instelling_id: '5', instelling_title: 'Scalda', instelling_desc: ''},
 {instelling_id: '6', instelling_title: 'Scalda', instelling_desc: ''},
 {instelling_id: '799', instelling_title: 'ROC', instelling_desc: ''},
 {instelling_id: '8', instelling_title: 'Scalda', instelling_desc: ''},
 {instelling_id: '9', instelling_title: 'Scalda', instelling_desc: ''},
 {instelling_id: '10', instelling_title: 'Scalda', instelling_desc: ''},
 {instelling_id: '11', instelling_title: 'Scalda', instelling_desc: ''},
 {instelling_id: '23', instelling_title: 'Scalda', instelling_desc: ''}
];

Can anyone help me with this? Thanks in advance!!

try like this :

var lblArr = [];

for(var i=0;i<5;i++){
    lblArr[i] = Ti.UI.createLabel({
        text : i
    });
    Win.add(lblArr[i]);
}

You are creating label in the loop with same name, so all previous labels are getting overwritten and only the last label remains, thats why you are getting only the last label. By pushing labels in an array all labels are separated and you can get all labels.

Hope this will help you. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM