Skip to content

Instantly share code, notes, and snippets.

@khajer
Created July 5, 2011 10:10
Show Gist options
  • Save khajer/1064601 to your computer and use it in GitHub Desktop.
Save khajer/1064601 to your computer and use it in GitHub Desktop.
database titanium
var win = Titanium.UI.currentWindow;
win.backgroundImage = 'images/bg.png';
//database
var db = Titanium.Database.open('todolist');
db.execute('CREATE TABLE IF NOT EXISTS TODOLIST (ID INTEGER, TITLE TEXT, DETAIL TEXT, CREATEDATE DATETIME)');
var rows = db.execute('select * from todolist');
var data = [];
var cnt = 0;
while(rows.isValidRow()){
data[cnt] = {title:rows.field(0), detail:rows.fieldByName('detail')};
cnt++;
}
rows.close();
db.close();
/*
var data = [
{title:'Insert Row Above (no anim)', header:'Section 0'},
{title:'Row2'},
{title:'Insert Row Below - 1', name:'3'},
{title:'Row4'},
{title:'Row5'},
{title:'Row6'},
{title:'Insert Row Below - 2', name:'7'},
{title:'Insert Row Above - Header - 1', name:'8', header:'Section 1'},
{title:'Row9'},
{title:'Insert Row Above - Header - 2', name:'10'},
{title:'Row11'},
{title:'Row12'},
{title:'Insert Row Below - Header', name:'13'},
{title:'Row14'},
{title:'Row15'},
{title:'Insert Row w/o animation (below)'}
];
*/
var searchbar = Titanium.UI.createSearchBar({
barColor:'#385292',
showCancel:false,
hintText:'+ add task'
});
var tableView = Titanium.UI.createTableView({
data:data,
search:searchbar,
searchHidden:false
});
win.add(tableView);
// ----------------------------------
// event
// ----------------------------------
searchbar.addEventListener('return', function(e){
//e.value - string input
//insert to db
var db = Titanium.Database.open('mydb');
var rowcount = db.execute('select count(*) from todolist');
var id = 0;
while(rowcount.isValidRow()){
id = rowcount.getfield(0);
rowcount.next();
}
db.execute('INSERT INTO TODOLIST (ID, TITLE) VALUES(?, ?)', id, e.value);
db.close();
//update row in table
var row = tableView.UI.createTableViewRow();
data = {title:e.value};
tableView.insertRowAfter(0,data,{animationStyle:Titanium.UI.iPhone.RowAnimationStyle.DOWN}); // 0 is first row
});
tableview.addEventListener('click', function(e){
var w = Titanium.UI.createWindow({
title:e.rowData.title,
backgroundColor:'#fff',
barColor:'#111'});
//open
Titanium.UI.currentTab.open(win,{animated:true});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment