Skip to content

Instantly share code, notes, and snippets.

@nazrdogan
Created May 1, 2014 08:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nazrdogan/1a7db965837d7f842c20 to your computer and use it in GitHub Desktop.
Save nazrdogan/1a7db965837d7f842c20 to your computer and use it in GitHub Desktop.
Titanium TableView Edit -Finish and Trash option
var win = Ti.UI.createWindow({backgroundColor:'white', title:'SWIPEABLE CELL'})
var navWin = Ti.UI.iOS.createNavigationWindow({
window:win
})
function genWindow(module) {
var ListTest = require(module);
var win = new ListTest();
return win;
}
var b1 = Ti.UI.createButton({title:'TABLE VIEW SAMPLE', top:100});
var b2 = Ti.UI.createButton({title:'LIST VIEW SAMPLE', bottom:100});
win.add(b1);
win.add(b2);
b1.addEventListener('click',function(e){
navWin.openWindow(genWindow('tableviewsample'));
});
b2.addEventListener('click',function(e){
navWin.openWindow(genWindow('listviewsample'));
});
navWin.open();
function leftSwipeHandler(e) {
if (e.direction == 'left') {
var theItem = e.section.getItemAt(e.itemIndex);
theItem.template = 't2';
e.section.updateItemAt(e.itemIndex,theItem,{animated:true,animationStyle:Titanium.UI.iPhone.RowAnimationStyle.LEFT});
}
}
function rightSwipeHandler(e) {
if (e.direction == 'right') {
var theItem = e.section.getItemAt(e.itemIndex);
theItem.template = 't1';
e.section.updateItemAt(e.itemIndex,theItem,{animated:true,animationStyle:Titanium.UI.iPhone.RowAnimationStyle.RIGHT});
}
}
function trashHandler(e) {
e.section.deleteItemsAt(e.itemIndex,1,{animated:true,animationStyle:Titanium.UI.iPhone.RowAnimationStyle.FADE});
}
function doneHandler(e) {
var theItem = e.section.getItemAt(e.itemIndex);
theItem.template = 't3';
e.section.updateItemAt(e.itemIndex,theItem,{animated:true,animationStyle:Titanium.UI.iPhone.RowAnimationStyle.FADE});
}
var baseTemplate = {
properties:{height:50},
childTemplates: [
{
type: 'Ti.UI.View',
events:{
swipe:leftSwipeHandler
},
childTemplates:[
{
type:'Ti.UI.Label',
bindId:'content',
properties:{left:10,touchEnabled:false,color:'black'}
}
]
}
]
};
var controlTemplate = {
properties:{height:50},
childTemplates: [
{
type: 'Ti.UI.View',
properties:{left:-150,width:'100%'},
events:{
swipe:rightSwipeHandler
},
childTemplates:[
{
type:'Ti.UI.Label',
bindId:'content',
properties:{left:10,touchEnabled:false,color:'black'}
}
]
},
{
type:'Ti.UI.Label',
properties:{color:'white',text:'Trash',width:75,right:75, height:50, backgroundColor:'red', textAlign:Ti.UI.TEXT_ALIGNMENT_CENTER},
events:{
click:trashHandler
}
},
{
type:'Ti.UI.Label',
properties:{color:'white',text:'Finish',width:75,right:0, height:50, backgroundColor:'green', textAlign:Ti.UI.TEXT_ALIGNMENT_CENTER},
events:{
click:doneHandler
}
}
]
}
var doneTemplate = {
properties:{height:50},
childTemplates: [
{
type: 'Ti.UI.View',
childTemplates:[
{
type:'Ti.UI.Label',
bindId:'content',
properties:{left:10,touchEnabled:false, color:'green'}
}
]
}
]
};
var data = [];
for(var i=0;i<20;i++) {
data.push({properties:{backgroundColor:'white',selectionStyle:Ti.UI.iPhone.ListViewCellSelectionStyle.NONE},content:{text:'THIS IS A TASK INDEX'+(i+1)}})
}
var section = Ti.UI.createListSection({items:data});
var listView = Ti.UI.createListView({
sections:[section],
templates:{'t1':baseTemplate,'t2':controlTemplate,'t3':doneTemplate},
defaultItemTemplate:'t1'
})
function listviewsample() {
var win = Ti.UI.createWindow({backgroundColor:'white', title:'LISTVIEW', backButtonTitle:'BACK'});
win.add(listView);
return win;
}
module.exports = listviewsample;
function swipeListener(e) {
if(e.row) {
if(e.row.contentHolder != null) {
if (e.direction == 'left') {
e.row.contentHolder.animate({left:-150, duration:250});
e.row.controlHolder.animate({opacity:1, duration:250});
} else if (e.direction == 'right'){
e.row.contentHolder.animate({left:0, duration:250});
e.row.controlHolder.animate({opacity:0, duration:250});
}
} else {
Ti.API.info('TASK IS DONE');
}
} else {
Ti.API.info('NO ROW INFORMATION')
}
}
function tapListener(e) {
if (e.source.taskid != undefined) {
if (e.source.taskid == 'Done') {
e.row.contentHolder.animate({left:0, duration:250});
e.row.controlHolder.animate({opacity:0, duration:250});
e.row.contentHolder.children[0].color = 'green';
e.row.contentHolder = null;
e.row.contentHolder = null;
} else if (e.source.taskid == 'Cancel') {
tableView.deleteRow(e.row,{animated:true});
}
} else {
Ti.API.info('NO TASK ID');
}
}
var rows = [];
//Set up row data
for(var i=0;i<20;i++){
//The Controls
var controls = Ti.UI.createView({width:150,right:0, height:50, opacity:0});
var l1 = Ti.UI.createLabel({color:'white',text:'Trash',width:75,left:0, height:50, backgroundColor:'red', taskid:'Cancel', textAlign:Ti.UI.TEXT_ALIGNMENT_CENTER});
var l2 = Ti.UI.createLabel({color:'white',text:'Finish',width:75,right:0, height:50, backgroundColor:'green', taskid:'Done', textAlign:Ti.UI.TEXT_ALIGNMENT_CENTER });
controls.add(l1);
controls.add(l2);
//The Content
var content = Ti.UI.createView({width:'100%',height:50, backgroundColor:'white'});
var l3 = Ti.UI.createLabel({touchEnabled:false, text:'THIS IS A TASK INDEX'+(i+1), left:10});
content.add(l3);
var row = Ti.UI.createTableViewRow({backgroundColor:'white',height:50, selectionStyle:Ti.UI.iPhone.TableViewCellSelectionStyle.NONE, contentHolder:content, controlHolder:controls});
row.add(controls);
row.add(content);
row.addEventListener('swipe', swipeListener);
row.addEventListener('singletap', tapListener);
rows.push(row);
}
var tableView = Ti.UI.createTableView({
data:rows
})
function tableviewsample() {
var win = Ti.UI.createWindow({backgroundColor:'white', title:'TABLEVIEW', backButtonTitle:'BACK'});
win.add(tableView);
return win;
}
module.exports = tableviewsample;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment