Forked from tripitakit/confirmTableViewDelete.js
Last active
August 29, 2015 14:23
-
-
Save almsx/20d6d20f84f11a769dff to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*** | |
* Appcelerator Titanium example of custom confirm dialog for TableView's delete events. | |
* Asks to confirm/cancel a delete operation. On cancel restores the deleted row in | |
* its former position in the table data. | |
* | |
* @Copyleft 2013 Patrick De Marta | |
* @License GNU GPL | |
*/ | |
var win = Titanium.UI.createWindow({ | |
layout:'vertical', | |
backgroundColor:'#fff', | |
}); | |
var testData = [ | |
{title: 'Apples'}, {title: 'Bananas'}, | |
{title: 'Carrots'}, {title: 'Potatoes'} ]; | |
var table = Ti.UI.createTableView({ | |
top:40, | |
height:Ti.UI.SIZE, | |
editable:true, | |
data: testData | |
}); | |
table.addEventListener("dragend", function(){ | |
log("Reset table with testData on dragend"); | |
table.setData(testData); | |
}); | |
table.addEventListener("delete", function(e){ | |
var row_index = e.index; | |
var row_bkup = e.row; | |
log("Swipe delete invoked, show the confirm dialog... \n\n" + | |
"But the " + testData[row_index].title + " are already gone (ouch!)"); | |
var dialog = Ti.UI.createAlertDialog({ | |
cancel: 1, | |
buttonNames: ['Confirm', 'Cancel'], | |
message: 'Confirm delete?', | |
title: 'Delete' | |
}); | |
dialog.addEventListener('click', function(e){ | |
if (e.index === e.source.cancel){ | |
log("Delete cancelled. Keeping " + testData[row_index].title + | |
"\n\nTable Data after cancel: " + JSON.stringify( table.sections[0].getRows() ) + | |
"\n\nTest data: " + JSON.stringify( testData ) + | |
"\n\nForce pushing the vanished row I choose to keep back into table data"); | |
var restored = table.sections[0].getRows(); | |
restored.splice(row_index, 0, row_bkup); | |
table.setData(restored); | |
} else if (e.index === 0) { | |
log( testData.splice(row_index, 1) + " Deleted!" + "\n\nTable data: " + | |
JSON.stringify( table.sections[0].getRows() ) + | |
"\n\nTest data: " + JSON.stringify( testData )); | |
} | |
}); | |
dialog.show(); | |
}); | |
var logView = Ti.UI.createLabel({ | |
top:20, | |
text: "Actions log" | |
}); | |
function log(text){ | |
logView.setText(text); | |
} | |
win.add(table); | |
win.add(logView); | |
win.open(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment