Edit on double click & updating db on keypress
I know there was a couple of threads already on this matter, but I'm having some difficulty with the example(s) that were provided in the following thread:
http://www.activewidgets.com/javascript.forum.1394.55/textbox-template-edit-on-double.html
What I'm trying to do is 1) have the user double-click on a cell to go into edit mode, 2) the user changes the cell value, and then 3) presses the enter key to submit a form with these new cell changes to update the database.
The one problem I'm having is that once the enter key is pressed I'm not getting the new updated cell contents, but the old value. But, what is strange is that that after I put an alert statement (for debugging only) at the beginning of the event funtion for the keypress then I end up getting the correct value.
Can someone explain why I'm not getting the updated cell contents? I have no idea why the alert statement (dialog) will cause the code to work once that dialog is dismissed.
Here's the code, which was provided in the above thread:
if (!window.My) My=[];
if (!My.Templates) My.Templates=[];
My.Templates.Input = Active.Templates.Text.subclass();
My.Templates.Input.create = function()
{
var obj = this.prototype;
// editor is not part of the template,
// there is only one single instance of editor object.
var editor = new Active.HTML.INPUT;
editor.setClass("templates", "input");
editor.setAttribute("type", "text");
editor.setAttribute("value", function(){
return template.getItemProperty("text");
});
// template variable provides temporary reference to the parent template during edit mode.
var template;
function switchToEditMode(){
if (template) {
switchToTextMode()
}
template = this;
template.element().className += " active-edit-mode ";
//template.element().style.padding = 0;
template.element().innerHTML = editor;
editor.element().focus();
}
obj.setEvent("ondblclick", switchToEditMode);
function switchToTextMode(){
var value = editor.element().value;
template.setItemProperty("text", value);
template.refresh();
template = null;
}
editor.setEvent("onblur", switchToTextMode);
};
My.Templates.Input.create();
var gridObj = new Active.Controls.Grid;
gridObj.setId("grid1");
// set number of rows/columns
gridObj.setRowProperty("count", data1.length);
gridObj.setColumnProperty("count", 5);
// set the column and row data
gridObj.setDataProperty("text", function(i, j){return data1[i][j]});
gridObj.setColumnProperty("text", function(i){return columns1[i]});
// turn off multiple row selection
gridObj.setSelectionProperty("multiple", false);
// create editable text template
var template = new My.Templates.Input;
// assign new template to all columns
//gridObj.setColumnTemplate(template);
// assign new template to just the second column in grid
gridObj.setColumnTemplate(template, 1);
// disable arrow key nevigation
gridObj.setEvent("onkeydown", null);
// make input box selectable
gridObj.getTemplate("top").setEvent("onselectstart", gridObj.getEvent("onselectstart"));
gridObj.setEvent("onselectstart", null);
// provide methods for getting and setting data
gridObj.getDataText = function(i, j){return data1[i][j]};
gridObj.setDataText = function(value, i, j){data1[i][j] = value};
var row = gridObj.getTemplate("row");
row.setEvent("onkeydown",function(){this.action("myAction")});
gridObj.setAction("myAction",function(src){
if (event.keyCode==13) {
//alert("myAction called...");
var i=gridObj.getProperty("selection/index");
alert(gridObj.getDataProperty("text",i , 1));
//document.form.submit();
}
});
document.write(gridObj);
http://www.activewidgets.com/javascript.forum.1394.55/textbox-template-edit-on-double.html
What I'm trying to do is 1) have the user double-click on a cell to go into edit mode, 2) the user changes the cell value, and then 3) presses the enter key to submit a form with these new cell changes to update the database.
The one problem I'm having is that once the enter key is pressed I'm not getting the new updated cell contents, but the old value. But, what is strange is that that after I put an alert statement (for debugging only) at the beginning of the event funtion for the keypress then I end up getting the correct value.
Can someone explain why I'm not getting the updated cell contents? I have no idea why the alert statement (dialog) will cause the code to work once that dialog is dismissed.
Here's the code, which was provided in the above thread:
if (!window.My) My=[];
if (!My.Templates) My.Templates=[];
My.Templates.Input = Active.Templates.Text.subclass();
My.Templates.Input.create = function()
{
var obj = this.prototype;
// editor is not part of the template,
// there is only one single instance of editor object.
var editor = new Active.HTML.INPUT;
editor.setClass("templates", "input");
editor.setAttribute("type", "text");
editor.setAttribute("value", function(){
return template.getItemProperty("text");
});
// template variable provides temporary reference to the parent template during edit mode.
var template;
function switchToEditMode(){
if (template) {
switchToTextMode()
}
template = this;
template.element().className += " active-edit-mode ";
//template.element().style.padding = 0;
template.element().innerHTML = editor;
editor.element().focus();
}
obj.setEvent("ondblclick", switchToEditMode);
function switchToTextMode(){
var value = editor.element().value;
template.setItemProperty("text", value);
template.refresh();
template = null;
}
editor.setEvent("onblur", switchToTextMode);
};
My.Templates.Input.create();
var gridObj = new Active.Controls.Grid;
gridObj.setId("grid1");
// set number of rows/columns
gridObj.setRowProperty("count", data1.length);
gridObj.setColumnProperty("count", 5);
// set the column and row data
gridObj.setDataProperty("text", function(i, j){return data1[i][j]});
gridObj.setColumnProperty("text", function(i){return columns1[i]});
// turn off multiple row selection
gridObj.setSelectionProperty("multiple", false);
// create editable text template
var template = new My.Templates.Input;
// assign new template to all columns
//gridObj.setColumnTemplate(template);
// assign new template to just the second column in grid
gridObj.setColumnTemplate(template, 1);
// disable arrow key nevigation
gridObj.setEvent("onkeydown", null);
// make input box selectable
gridObj.getTemplate("top").setEvent("onselectstart", gridObj.getEvent("onselectstart"));
gridObj.setEvent("onselectstart", null);
// provide methods for getting and setting data
gridObj.getDataText = function(i, j){return data1[i][j]};
gridObj.setDataText = function(value, i, j){data1[i][j] = value};
var row = gridObj.getTemplate("row");
row.setEvent("onkeydown",function(){this.action("myAction")});
gridObj.setAction("myAction",function(src){
if (event.keyCode==13) {
//alert("myAction called...");
var i=gridObj.getProperty("selection/index");
alert(gridObj.getDataProperty("text",i , 1));
//document.form.submit();
}
});
document.write(gridObj);
David
February 16,