Skip to content

Instantly share code, notes, and snippets.

@tripitakit
Last active December 27, 2015 20:48
Show Gist options
  • Save tripitakit/7386775 to your computer and use it in GitHub Desktop.
Save tripitakit/7386775 to your computer and use it in GitHub Desktop.
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.
/***
* 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();
@almsx
Copy link

almsx commented Jul 2, 2015

I found an issue in the code that you kindly share with us.

Using your original code, I did an adaptation for use in my app. My app uses WebServices and show the results of my query in a tableview.

However, if I have 2 or more records (tableviewrow) shares Confirm and Cancel function successfully. But if I have only one record (tableviewrow) code displays the following error, which I accompany with a picture.

http://www.luebbert.mx/tableview.png

Likewise, I did check with the code that you provide and the same case.

I will appreciate all your possible support and greetings from mexico

@addieljuarez
Copy link

Hi almsx.

This is because first delete the row, and when you try to read it no longer exists, it is solved very easy just to limit it.
I put the solution here taking the original code here.

https://gist.github.com/addieljuarez/31c802c2afac79026b96

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment