/**
* DBView.js
*/
var self = Ti.UI.currentWindow;
self.backgroundColor = '#333';
self.updateData = updateData;
// create view
var view = Ti.UI.createView();
var myFont = {fontSize: 18};
var cols = [20, 300];
var table = Ti.UI.createTableView();
updateData();
table.addEventListener("click", function(e){
Ti.API.info("row clicked with id: " + e.row.myIndex);
var win = Ti.UI.createWindow({
title: 'Update Data',
url:'/ui/common/UpdateData.js',
id:e.row.myIndex,
prevWindow:self
});
win.open();
});
view.add(table);
self.add(view);
function updateData(){
var tableData = [];
// get data
// install db in device
var db = Ti.Database.install('/mydata/mydb.sqlite', 'mydb');
var rows = db.execute('SELECT * FROM hello_tbl');
while (rows.isValidRow())
{
var name = rows.fieldByName('name');
var age = rows.fieldByName('age');
var id = rows.fieldByName('id');
Ti.API.info(name + ' ' + age);
var tablerow = Ti.UI.createTableViewRow({
className:'row',
objName:'row',
myIndex:id,
height: 50
});
var label = Ti.UI.createLabel({text:name, font:myFont, left: cols[0]});
var label2 = Ti.UI.createLabel({text:age, font:myFont, left: cols[1]});
tablerow.add(label);
tablerow.add(label2);
tableData.push(tablerow);
rows.next();
}
rows.close();
table.setData(tableData);
}
/**
*
* UpdateData.js
*/
var self = Ti.UI.currentWindow;
self.backgroundColor = '#333';
// create view
var view = Ti.UI.createView();
var myFont = {fontSize: 18};
var myHeight = 50;
var myWidth = '80%';
var cols = [20, 300];
var rows = [20, 50, 120, 150, 220];
var db = Ti.Database.install('/mydata/mydb.sqlite', 'mydb');
var nameLbl = Ti.UI.createLabel({text:"Name: ", left:cols[0], top: rows[0], width:myWidth, font:myFont});
view.add(nameLbl);
var nameTxt = Ti.UI.createTextField({hintText :"Enter your name", left:cols[0], top: rows[1], width:myWidth, font:myFont});
view.add(nameTxt);
var ageLbl = Ti.UI.createLabel({text:"Age: ", left:cols[0], top: rows[2], width:myWidth, font:myFont});
view.add(ageLbl);
var ageTxt = Ti.UI.createTextField({hintText :"Enter your age", left:cols[0], top: rows[3], width:myWidth, font:myFont});
view.add(ageTxt);
var button = Ti.UI.createButton({title: 'Update Data', top: rows[4], left:cols[0], height:myHeight});
view.add(button);
button.addEventListener('click', function(e){
// get data
// install db in device
var rows = db.execute('UPDATE hello_tbl SET name = ?, age=? WHERE id = ?',
nameTxt.value, ageTxt.value, self.id);
Ti.API.info("data updated: " + nameTxt.value + " , " + ageTxt.value);
self.close();
self.prevWindow.updateData();
});
// populate data into UI
var rows = db.execute('SELECT * FROM hello_tbl WHERE id=?', self.id);
if (rows.isValidRow())
{
nameTxt.value = rows.fieldByName('name');
ageTxt.value = rows.fieldByName('age');
}
self.add(view);