This is a migrated thread and some comments may be shown as answers.

open row for edit by js code in batch mode

9 Answers 334 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Ali
Top achievements
Rank 1
Ali asked on 24 Jun 2014, 07:21 AM
 hi
i have grid in batch mode in last textbox of row i wont if press Enter select next row and open it for edit .i use get_batchEditingManager().openRowForEdit
in another button it works good but in key press of textbox it doesn't work
what should i do?

9 Answers, 1 is accepted

Sort by
0
Angel Petrov
Telerik team
answered on 26 Jun 2014, 03:17 PM
Hi Ali,

Actually in order to make things work you should subscribe to the grid key press event since it will be fired first.

ASPX;
<ClientSettings>
                <ClientEvents OnKeyPress="keyPress" />
            </ClientSettings>

JavaScript:
function keyPress(sender,args) {
            if (args.get_keyCode() === 13) {
                args.set_cancel(true);
                args.get_domEvent().stopPropagation();
                args.get_domEvent().preventDefault();
                var row = sender.get_batchEditingManager().get_currentlyEditedRow();
                if (row.nextSibling) {
                    sender.get_batchEditingManager().openRowForEdit(row.nextSibling);
                }
            }
        }

When including the above code in the project the new next row will be opened for edit once the user presses enter inside an editor in the edited row.

Hope the example provided proves helpful.

Regards,
Angel Petrov
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Ali
Top achievements
Rank 1
answered on 29 Jun 2014, 03:40 PM
thanks angel
but i have a problem i want do this function in last column and not in all of row i can handle it by a flag but i want another solution

and i check your code i comment this lines
args.set_cancel(true);
args.get_domEvent().stopPropagation();
args.get_domEvent().preventDefault();
and it works too but if call this function in keydown of textbox and clear event from grid event not work cloud you   explanation for me why

i sorry for my poor english
0
Angel Petrov
Telerik team
answered on 02 Jul 2014, 02:16 PM
Hi Ali,

You can accomplish the desired goal by subscribing to the KeyPress event of the input element used as an editor instead of the grid. For example if the last column has a definition similar to this.

ASPX:
<telerik:GridTemplateColumn DataField="SomeField" HeaderStyle-Width="210px" HeaderText="SomeField" SortExpression="SomeField"
                       UniqueName="SomeField">
                       <ItemTemplate>
                            <%# Eval("SomeField") %>
                       </ItemTemplate>
                      <EditItemTemplate>
                          <telerik:RadTextBox runat="server" ID="SomeFieldTextBox" ClientEvents-OnKeyPress="keyPress"></telerik:RadTextBox>
                      </EditItemTemplate>
                   </telerik:GridTemplateColumn>
you can open the next row by calling the below provided JavaScript.

JavaScript:
function keyPress(sender, args) {
                if (args.get_keyCode() === 13) {
                    args.set_cancel(true);
                    args.get_domEvent().stopPropagation();
                    args.get_domEvent().preventDefault();
                    var grid = $find('<%=RadGrid1.ClientID%>');
                    var row = grid.get_batchEditingManager().get_currentlyEditedRow();
                    if (row.nextSibling) {
                        grid.get_batchEditingManager().openRowForEdit(row.nextSibling);
                    }
                }
            }

Note that the example uses a RadTextBox as an editor but you should be able to achieve the same effect using other editor types as well.

As for the executing the provided logic in the KeyDown event of the text box probably a JavaScript error is thrown which is breaking the logic. A possible reason for this is that the event is raised too early. If however the KeyPress event is used you should not experience any problems.

Regards,
Angel Petrov
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Bunyamin
Top achievements
Rank 1
answered on 05 Jul 2014, 09:15 AM
Thanks for this code. But after next row grid cancel change before cell value. how can submit for changed cell value?
0
Angel Petrov
Telerik team
answered on 10 Jul 2014, 06:31 AM
Hi Bunyamin,

The problem may be related to the fact that the provided code opens the new but the editors value changing events are not fired before the cell closes. Please try modifying the code as demonstrated below and let us know if this helps you resolve the issue.

ASPX:
<telerik:GridTemplateColumn DataField="SomeField" HeaderStyle-Width="210px" HeaderText="SomeField" SortExpression="SomeField"
                      UniqueName="SomeField">
                      <ItemTemplate>
                           <%# Eval("SomeField") %>
                      </ItemTemplate>
                     <EditItemTemplate>
                         <asp:TextBox runat="server" ID="SomeFieldTextBox"  onkeypress="keyPress(this,event);"></asp:TextBox>
                     </EditItemTemplate>
                  </telerik:GridTemplateColumn>


JavaScript:
function keyPress(sender, args) {
                if (args.keyCode === 13) {
                    args.stopPropagation();
                    args.preventDefault();
                    var grid = $find('<%=RadGrid1.ClientID%>');
                    var row = grid.get_batchEditingManager().get_currentlyEditedRow();
                    if (row.nextSibling) {
                        var nextRow = row.nextSibling;
                        grid.get_batchEditingManager()._tryCloseEdits(document.body);
                        setTimeout(function myfunction() {
                            grid.get_batchEditingManager().openRowForEdit(nextRow);
                        }, 10);
                    }
                }
            }


Regards,
Angel Petrov
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Ali
Top achievements
Rank 1
answered on 09 Oct 2014, 07:08 AM
hi angel
thank  you for your answer
I have problem like Bunyamin I lose my value and  your code don't work for me
what should i do?
0
Eyup
Telerik team
answered on 14 Oct 2014, 04:43 AM
Hello Ali,

You can add the following script on your page to make the Enter key to function just as Tab:
Copy Code
Copy Code
Telerik.Web.UI.GridBatchEditing.prototype._handleKeyboardNavigationOrg =
    Telerik.Web.UI.GridBatchEditing.prototype._handleKeyboardNavigation;
Telerik.Web.UI.GridBatchEditing.prototype._handleKeyboardNavigation = function (e) {
    if (e.keyCode == 13) {
        e.keyCode = 9;
    }
    this._handleKeyboardNavigationOrg(e);
};

Hope this helps.


Regards,
Eyup
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Andy
Top achievements
Rank 1
answered on 30 Jun 2016, 11:32 AM

Hi,

I'm trying to get the same functionality, but for some reason when I call openRowForEdit of batchEditingManager, it throws exception

ScriptResource.axd?d=NTwIlNkcPTCNaLryRlXgiipyAr_jUOonE04EQqz7c4MCDcI8IH9FX0KACOcLfjX5pPJjrvXzFX87JF…:235 Uncaught TypeError: Cannot read property '_data' of undefined

Here is the js code, and exception is thrown on v._data._batchEditingSettings.highlightDeletedRows

openRowForEdit:function(u,w){var x=this;
var o;
var n;
var q;
var r;
var s=null;
var p;
var v;
if(typeof u==="string"){u=$get(u);
}if(b.DomElement.containsCssClass(u,g)){return;
}if(x._currentlyEditedRow===u||(u.className.indexOf("rgRow")===-1&&u.className.indexOf("rgAltRow")===-1)){return true;
}if(!x._validate()){return true;
}o=u.children;
for(var t=0;
t<o.length;
t++){n=o[t];
if(!s){q=x._getCellDataToOpenEdit(n);
}if(q){v=q.tableView;
p=x._getColumn(v,n);
if(p&&p.Display&&v._isColumnEditable(p,u)){q.cell=n;
q.column=p;
r=x._openCellInEditMode(q);
if(!s||n===w){s=r;
}}}}if(s){x._focusControl(s);
x._currentlyEditedRow=u;
x._hideValidators();
}if(v._data._batchEditingSettings.highlightDeletedRows){x._adjustBatchDeletedRows();
}return true;
}

 

Thanks!

0
Andy
Top achievements
Rank 1
answered on 02 Jul 2016, 10:05 AM

Ok, actually found a way around myself, maybe it'll help somebody. The idea is to trigger click event on the cell you want to switch to. Here's javascript function which is called from keyDown handler. 

It takes two arguments:

step - index of next row to edit relative to the current row. so if you need to set into edit mode the next row after current row, step is 1, if next row above current it should be (-1)

cellName - unique column name of cell which will get focus.

 

          function switchRow(step, cellName)
          {
              var grid = $find('<%=gvSales.ClientID%>');
                  var masterView = grid.get_masterTableView();
                  var currentRow = masterView.get_selectedItems()[0];
                  if (currentRow) {
                      var rowIndex = currentRow.get_itemIndex();
                      var nextRow = masterView.get_dataItems()[rowIndex + step];
                      if (nextRow) {
                          var cell = nextRow.get_cell(cellName);
                          $(cell).trigger("click");
                      }
                  }
          }

Tags
Grid
Asked by
Ali
Top achievements
Rank 1
Answers by
Angel Petrov
Telerik team
Ali
Top achievements
Rank 1
Bunyamin
Top achievements
Rank 1
Eyup
Telerik team
Andy
Top achievements
Rank 1
Share this question
or