PDA

View Full Version : Simulating a cancelEdit event on a grid



malraux
29 Nov 2007, 9:56 AM
Here's my use case:

I have an Add button that creates a new row in a grid with the first field in edit mode.

If the user hits 'Escape' and cancels the edit, I'd like to remove the blank row. There's no cancelEdit event on the EditorGrid, so I did this:



function addNew()
{
detailStore.insert(0, detailReader.readRecords([{'id': '', 'desc': ''}]).records);
detailGrid.getView().focusCell(0, 1);
detailGrid.startEditing(0, 1);
}

// Used to catch edit cancels when adding a row
function cancelAdd(o)
{
if (!o.record.dirty)
{
// Remove the record that was being edited, because they didn't actually
// enter anything.
detailStore.remove(o.record);
}
}

detailGrid.on('beforeedit', function(o) {
// Hook the hide on the editor to know if it gets cancelled.
var ed = detailGrid.colModel.getCellEditor(1, 0);
// The delay is necessary because removing a row in the midst of a cancel
// causes errors.
ed.on("hide", cancelAdd, this, {single: true, delay: 100});
});


So here are the questions: is there a better way to do this? And are there plans to add a cancelEdit event in the future?

Hooking the hide event seems like a hack...

Regards,
-scott

brian.moeskau
3 Aug 2008, 12:38 PM
Added Editor canceledit event in SVN.