appending rows to tableview from sqlite (pagination)

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

i'm trying to add a "view more rows" from an sqlite db - i had working code when i pulled from an online mysql (via json) so ive tried to amend it

i've got so far that i get the view more but clicking seems to get the next 8 records but only shows the last 2 added rows in the tableview and doesnt add another view more at the end - ive looked at this code all day and i'm obviously got too far into a mix & match code and cant see where i'm going wrong

var win = Ti.UI.currentWindow;
 
var page = 1; 
var page_size = 8;  
var more_index = 0;
 
var categoryArray = [];
 
var tableView = Ti.UI.createTableView({
    top:0,
    left:0,
    right:0,
    zIndex:10,
    backgroundColor:'transparent',
    separatorColor:'#ccc'
});
 
win.add(tableView);
 
 
 
function addRow(buses){
 
    Ti.API.info('Adding row for : '+buses.direction);   
 
    var row = Ti.UI.createTableViewRow({height:'auto',hasChild:false,selectedBackgroundColor:'#4eaaf0'});   
 
    var title_label = Ti.UI.createLabel({
        text:buses.direction,
        left:4,
        top:4,
        height:'auto',
        font:{fontFamily:'Helvetica Neue',fontSize:16,fontWeight:'bold'},
        color:'#fff',
        right:0,
        textAlign:'left',
        touchEnabled:false
    });
 
    row.add(title_label);
    row.id = buses.id;
    row.className = 'buses';
    tableView.appendRow(row);
 
}
 
 
 
function getBuses(page){
 
    var db = Titanium.Database.open('bus');
    var dbrows = db.execute('select * FROM bus LIMIT 0,'+page_size);
    while (dbrows.isValidRow()) {
        categoryArray.push({
            id:dbrows.fieldByName('id'),
            direction:dbrows.fieldByName('direction'),
        }); 
 
 
        dbrows.next();
    }
    dbrows.close();
    db.close();
        var buses = categoryArray; 
 
 
        Ti.API.info(buses);
 
        Ti.API.info('there are '+buses.length);
 
        for(var i in buses){
            if(i){
                addRow(buses[i]);
            }
        Ti.API.info('i: '+i);
 
        if(i == page_size-1){
 
 
            var more_row = Ti.UI.createTableViewRow({height:'auto',hasChild:true,selectedBackgroundColor:'#4eaaf0'});   
            var title_label = Ti.UI.createLabel({
                text:'View more ... ',
                left:10,
                top:12,
                height:20,
                font:{fontFamily:'Helvetica Neue',fontSize:16,fontWeight:'normal'},
                color:'#fff',
                right:0,
                textAlign:'left',
                touchEnabled:false
            });
            more_row.add(title_label);
            more_row.id = 'MORE'; 
 
            tableView.appendRow(more_row);
 
        } 
 
        if(page > 0 && more_index!=0){
            tableView.deleteRow(more_index);
        }
 
 
    };
 
}
 
 
 
tableView.addEventListener('click',function(e){
 
    Ti.API.info('You clicked the tableView : '+e.rowData.id);  
 
    if(e.rowData.id=='MORE'){
 
        more_index = e.index;
        page_size+=8;
        getBuses(page);
 
 
    } else {
 
//link to stuff....
 
 
    }
 
});
 
 
 
getBuses(page);

— asked 7 months ago by adrian harris
1 Comment
  • ive amended code to set the sql offset and it now sort of works - the view more adds 2 more rows to the table, but subsequent clicks adds 2 more more but also adds again the previous 2 rows and also cant seem to workout out how to remove the "view more" row:

    ive amended it so you dont need the original sqlite table to test it - the js should work standalone...

    var win = Ti.UI.currentWindow;
     
     
    var page = 1; 
    var page_size = 2;  
    var more_index = 0;
    var limit1 = 0;
     
    var categoryArray = [];
     
     
    var myDatabase = Ti.Database.open('myDatabase');
    var sql = 'CREATE TABLE IF NOT EXISTS my_table(' +
        'id INTEGER PRIMARY KEY AUTOINCREMENT, line_no TEXT, direction  TEXT' +
    ')';
     
    myDatabase.execute(sql);
     
     
    var sql = 'INSERT INTO my_table (line_no, direction) VALUES (?,?)';
     
    myDatabase.execute(sql, '1', 'a');
    myDatabase.execute(sql, '2', 'b');
    myDatabase.execute(sql, '3','c');
    myDatabase.execute(sql, '4','d');
    myDatabase.execute(sql, '5','e');
    myDatabase.execute(sql, '6','f');
    myDatabase.execute(sql, '7','g');
    myDatabase.execute(sql, '8','h');
    myDatabase.execute(sql, '9','i');
    myDatabase.execute(sql, '10','j');
    myDatabase.execute(sql, '11','k');
    myDatabase.execute(sql, '12','l');
    myDatabase.execute(sql, '14','m');
    myDatabase.execute(sql, '14','n');
     
     
     
     
    var tableView = Ti.UI.createTableView({
        top:0,
        left:0,
        right:0,
        zIndex:10,
        backgroundColor:'transparent',
        separatorColor:'#ccc'
    });
     
    win.add(tableView);
     
     
     
    function addRow(buses){
     
        Ti.API.info('Adding row for : '+buses.direction);   
     
        var row = Ti.UI.createTableViewRow({height:'auto',hasChild:false,selectedBackgroundColor:'#4eaaf0'});   
     
        var title_label = Ti.UI.createLabel({
            text:buses.direction,
            left:46,
            top:4,
            height:'auto',
            font:{fontFamily:'Helvetica Neue',fontSize:16,fontWeight:'bold'},
            color:'#fff',
            right:0,
            textAlign:'left',
            touchEnabled:false
        });
     
        row.add(title_label);
        row.className = 'buses';
     
            var line_label = Ti.UI.createLabel({
            text:buses.line_no,
            left:1,
            top:4,
            height:'auto',
            font:{fontFamily:'Helvetica Neue',fontSize:16,fontWeight:'nomral'},
            color:'#fff',
            right:0,
            textAlign:'left',
            touchEnabled:false
        });
     
        row.add(line_label);
     
     
     
     
        tableView.appendRow(row);
     
    }
     
     
     
     
     
    function getBuses(page){
    Ti.API.info('limit1: '+limit1+' - page_size: '+page_size);
     
     categoryArray.length=0;
     
        var db = Titanium.Database.open('myDatabase');
        var dbrows = db.execute('select * FROM my_table LIMIT '+limit1+','+page_size);
        while (dbrows.isValidRow()) {
            categoryArray.push({
          //    id:dbrows.fieldByName('id'),
                direction:dbrows.fieldByName('direction'),
                line_no:dbrows.fieldByName('line_no'),
            }); 
     
     
            dbrows.next();
        }
        dbrows.close();
        db.close();
            var buses = categoryArray; 
     
     
            Ti.API.info('there are '+buses.length);
     
            for(var i in buses){
                if(i){
                    addRow(buses[i]);
                }
            Ti.API.info('i: '+i);
     
            if(i == page_size-1){
     
     
                var more_row = Ti.UI.createTableViewRow({height:'auto',hasChild:true,selectedBackgroundColor:'#4eaaf0'});   
                var title_label = Ti.UI.createLabel({
                    text:'View more ... ',
                    left:10,
                    top:12,
                    height:20,
                    font:{fontFamily:'Helvetica Neue',fontSize:16,fontWeight:'normal'},
                    color:'#fff',
                    right:0,
                    textAlign:'left',
                    touchEnabled:false
                });
                more_row.add(title_label);
                more_row.id = 'MORE'; 
     
                tableView.appendRow(more_row);
     
            } 
     
            if(page > 0 && more_index!=0){
            //  tableView.deleteRow(more_row);
            }
     
     
        };
     
    }
     
     
     
    tableView.addEventListener('click',function(e){
     
        Ti.API.info('You clicked the tableView : '+e.rowData.id);  
     
        if(e.rowData.id=='MORE'){
        tableView.deleteRow(e.rowData.id=='MORE');
            more_index = e.index;
            page_size+=1;
            limit1+=1;
            getBuses(page);
     
     
        } else {
     
    //link to stuff....
     
     
        }
     
    });
     
     
     
    getBuses(page);
     
        Ti.UI.currentWindow.addEventListener('close',function()
    {
        Ti.API.info("window was closed");
    });

    — commented 7 months ago by adrian harris

Your Answer

Think you can help? Login to answer this question!