Pulling out database data and pass them into rows.

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

Hi, I have managed to pull out the required database data.

However, i need to put them into rows(i) so that i am able to CreateTableViewRow and put them inside CreateTableViewSection.

How do i do that from here?

// set the data from the database to the array
function setData() {    
 
 
    var rows = db.execute('SELECT DISTINCT col_1 FROM bygrace');
 
    // create the array
    var dataArray = [];
 
 
    while (rows.isValidRow())
    {
        dataArray.push({title:'' + rows.fieldByName('col_1') + '', hasChild:true, path:'areas.js'});
        rows.next();    
    };
 
    // set the array to the tableView
    section2.setData(dataArray);
    Ti.API.info(dataArray);
 
};

not sure whether i can do this...

for(i=0;i<10;i++){
 
var dataRow(i) = Titanium.UI.createTableViewRow();
dataRow(i).height = 'auto';
 
dataRow(i).add(dataArray(i));
 
}

4 Answers

Accepted Answer

try this one and comment this line in your code tableview.setData([section1,section2]);

var data = [];
data.push(section1);
function setData() {
 
    var rows = db.execute('SELECT DISTINCT col_1 FROM bygrace');
 
    // create the array
    var dataArray = [];
 
    while(rows.isValidRow()) {
        var r = Ti.UI.createTableViewRow({
            title : '' + rows.fieldByName('col_1') + '',
            hasChild : true,
            path : 'areas.js'
        });
        section2.add(r);
 
        rows.next();
    };
 
    // set the array to the tableView
    section2.setData(dataArray);
    data.push(section2);
    tableview.setData(data);
};

— answered 2 years ago by Mitul Bhalia
answer permalink
2 Comments
  • hey what happend....?

    — commented 2 years ago by Mitul Bhalia

  • hmm.. thanks Mitul.

    at least now i can pull out the correct data. now need to do something about the text formatting.

    — commented 2 years ago by Colin International

hi Colin,

first of all setData is not method for section

so you can do like that... var data = [];

while (rows.isValidRow())
    {
        section2.add({title:'' + rows.fieldByName('col_1') + '', hasChild:true, path:'areas.js'});
        rows.next();    
    };
data.push(section2);
 
tableview.setData(data);

for a clearer picture

// create var for the currentWindow
var currentWin = Ti.UI.currentWindow;
var dbVersion = 0.01;
var db = Ti.Database.install('bygrace.sqlite','bygrace'+ dbVersion);
 
currentWin.orientationModes = [
    Titanium.UI.PORTRAIT,
    Titanium.UI.LANDSCAPE_LEFT,
    Titanium.UI.LANDSCAPE_RIGHT
];
 
 
var buttonabout = Titanium.UI.createButtonBar({
    labels:['About']
});
 
currentWin.setRightNavButton(buttonabout);
 
buttonabout.addEventListener('click', function() {
    var buttonaboutWindow = Titanium.UI.createWindow({
        url:'about.js',
        title:'About'
    });
//  buttonaboutWindow.open({modal:true,modalTransitionStyle:style,modalStyle:presentation,navBarHidden:true});
    Titanium.UI.currentTab.open(buttonaboutWindow);
 
});
 
 
// ---start main body codings here
 
var tableview =  Titanium.UI.createTableView({
    style:Titanium.UI.iPhone.TableViewStyle.GROUPED,
    //footerView:footerView,
    backgroundColor:'white'
});
 
var section1 = Titanium.UI.createTableViewSection();
 
var row1 = Titanium.UI.createTableViewRow();
row1.height = 'auto';
 
var item1 = Ti.UI.createLabel({
    color:'#084B8A',
    text:'FEED YOUR FAITH, AND DOUBT WILL STARVE TO DEATH! ',
    font:{fontSize:16, fontWeight:'bold'},
    top:10,
    left:10,
    height:'auto',
    width:'auto'
});
row1.add(item1);
 
var text1 = Ti.UI.createLabel({
    text:'Read Me when... ',
    font:{fontSize:16},
    top:50,
    left:10,
    height:'auto',
    width:'auto',
    bottom:20
 
});
row1.add(text1);
section1.add(row1);
 
 
var section2 = Titanium.UI.createTableViewSection();
 
// set the data from the database to the array
function setData() {    
 
 
    var rows = db.execute('SELECT DISTINCT col_1 FROM bygrace');
 
    // create the array
    var dataArray = [];
 
    while (rows.isValidRow())
    {
        dataArray.push({title:'' + rows.fieldByName('col_1') + '', hasChild:true, path:'areas.js'});
        rows.next();    
    };
 
    // set the array to the tableView
    section2.setData(dataArray);
    Ti.API.info(dataArray);
 
};
 
 
tableview.setData([section1,section2]);
 
tableview.addEventListener('click', function(e)
{
    if (e.rowData.path)
    {
        var win = Ti.UI.createWindow({
            url:e.rowData.path,
            title:e.rowData.title
        });
 
        var prodCat = e.rowData.title;
        win.prodCat = prodCat;
        Ti.UI.currentTab.open(win);
    }
});
 
 
 
// add the tableView to the current window
currentWin.add(tableview);
 
 
// call the setData function to attach the database results to the array
setData();

database subsequent pages not appearing!

Mitul can you spot any error? Been on it for 5 hours already...

areas.js

// create var for the currentWindow
var currentWin = Ti.UI.currentWindow;
 
var dbVersion = 0.01;
var db = Ti.Database.install('bygrace.sqlite','bygrace'+ dbVersion);
 
var prodName = Ti.UI.currentWindow.prodName;
 
var rows = db.execute('SELECT * FROM bygrace WHERE col_1="' + prodName + '"');
var data = [
rows.fieldByName('col_2'),
rows.fieldByName('col_3')
];
 
 
var tableview = Ti.UI.createTableView({
data:data,
rowHeight:'80'
});
 
currentWin.add(tableview);
db.close();

Your Answer

Think you can help? Login to answer this question!