Karlos Garcia Karlos Garcia - 7 months ago 12
Javascript Question

Add rows to a tableview from database

i need append rows to a tableview reading data from database. My problem is all rows are mixed.

My code is

var db = Ti.Database.open('MyData');
var rows = db.execute('SELECT * FROM TablaAuxiliar ORDER BY TablaAuxiliar.Nombre');

var i;
var nuevaRow=Ti.UI.createTableViewRow({height:80});

for (i=0;i<NRegistros;i++){

var titulo=Ti.UI.createLabel({
id: "NombreTerm",
text: rows.field(2),
color: "black" ,
textAlign: Ti.UI.TEXT_ALIGNMENT_LEFT,
left:20,
top:5
});
var titulo2=Ti.UI.createLabel({
id: "EstadoTerm",
text: rows.field(3),
font: {fontSize: '30'},
color: "#888" ,
textAlign: Ti.UI.TEXT_ALIGNMENT_LEFT,
left:20,
top:30
});
var titulo3=Ti.UI.createLabel({
id: "TemperaturaTerm",
text: rows.field(4)/10,
font: {fontSize: '50'},
color: "#888" ,
textAlign: Ti.UI.TEXT_ALIGNMENT_LEFT,
left:200,
top:5
});
nuevaRow.add(titulo);
nuevaRow.add(titulo2);
nuevaRow.add(titulo3);
$.MenuLateral.appendRow(nuevaRow);
rows.next();
}

db.close();


$.MenuLateral is the TableView created with Alloy

<TableView id="MenuLateral" top="20%" height="60%" left="0%" zindex="20" width="100%" backgroundColor="white" separatorColor="gray">
</TableView>


I need a row for each record in the database. Where is the problem??

Thanks

Answer

Just move this line of code var nuevaRow=Ti.UI.createTableViewRow({height:80}); inside the for loop.

Actually, you have created only a single TableViewRow variable and if you will use that same variable to appendRow, then it will be a single row only with having many labels added from inside for loop.

So just try this code:

var i;

for (i=0;i<NRegistros;i++){
    var nuevaRow=Ti.UI.createTableViewRow({height:80});
...