Skip to content

Commit 1e2351e

Browse files
author
Marin Bratanov
committed
docs(grid): add info on traversing the DOM to get client side row object
1 parent 9c3d434 commit 1e2351e

File tree

1 file changed

+106
-1
lines changed

1 file changed

+106
-1
lines changed

controls/grid/rows/accessing-cells-and-rows.md

Lines changed: 106 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,112 @@ Read more in the [Batch editing mode]({%slug grid/data-editing/edit-mode/batch-e
339339

340340
## Accessing Cells and Values in Client-Side Code
341341

342-
Once you have a reference to the client-side object of a **GridDataItem**, there are several ways to access its cell values.
342+
Once you have a reference to the client-side object of a **GridDataItem**, there are several ways to access its cell values. There are several ways to get a reference to the grid data item object.
343+
344+
>caption How to access the data item object by traversing the DOM:
345+
346+
347+
````JavaScript
348+
function getDataClientSide(btn) {
349+
var $ = $telerik.$;//or use other jQuery
350+
351+
//get the DOM object of the grid row - the <tr> element
352+
var gridRowElem = $(btn).parents("tr").first()[0];
353+
354+
//here is how you can get the grid
355+
//var grid = $(btn).parents("div.RadGrid").first()[0].control;
356+
//console.log(grid);
357+
358+
//get a reference to the client-side object of the grid row to use its API
359+
var gridRowObj = gridRowElem.control;
360+
361+
//get the value of a column that you have added to the ClientDataKeyNames collection
362+
var nameFieldValue = gridRowObj.getDataKeyValue("name");
363+
364+
alert(nameFieldValue)
365+
366+
//you can use the other available methods of the item
367+
//alert(gridRowObj.get_itemIndexHierarchical())
368+
}
369+
function enumareteDataItems(sender, args) {
370+
//enumerate the data items so they are available
371+
//this puts them in memory, so you can move it to the button click handler
372+
//which will, however, cause the enumeration on every click
373+
sender.get_masterTableView().get_dataItems();
374+
}
375+
````
376+
377+
and the grid declaration. Note the `ClientDataKeyNames` so you can access those columns client-side.
378+
379+
````ASP.NET
380+
<telerik:RadGrid runat="server" ID="RadGrid1" RenderMode="Lightweight" OnItemCommand="RadGrid1_ItemCommand" OnNeedDataSource="RadGrid1_NeedDataSource">
381+
<ClientSettings>
382+
<ClientEvents OnGridCreated="enumareteDataItems" />
383+
</ClientSettings>
384+
<MasterTableView AutoGenerateColumns="false" ClientDataKeyNames="id,name">
385+
<Columns>
386+
<telerik:GridBoundColumn UniqueName="id" DataField="id"></telerik:GridBoundColumn>
387+
<telerik:GridBoundColumn UniqueName="name" DataField="name"></telerik:GridBoundColumn>
388+
<telerik:GridBoundColumn UniqueName="someData" DataField="data"></telerik:GridBoundColumn>
389+
<telerik:GridBoundColumn UniqueName="otherData" DataField="someColumn"></telerik:GridBoundColumn>
390+
<telerik:GridTemplateColumn UniqueName="myDeleteColumn">
391+
<ItemTemplate>
392+
<asp:Button ID="Button1" CommandName="Delete" Text="Get column value client-side by traversing the DOM" OnClientClick="getDataClientSide(this); return false;" runat="server" />
393+
</ItemTemplate>
394+
</telerik:GridTemplateColumn>
395+
</Columns>
396+
</MasterTableView>
397+
</telerik:RadGrid>
398+
````
399+
````C#
400+
protected void RadGrid1_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
401+
{
402+
(sender as RadGrid).DataSource = GetData();
403+
}
404+
405+
protected DataTable GetData()
406+
{
407+
DataTable tbl = new DataTable();
408+
tbl.Columns.Add(new DataColumn("id", typeof(decimal)));
409+
tbl.Columns.Add(new DataColumn("name", typeof(string)));
410+
tbl.Columns.Add(new DataColumn("data", typeof(decimal)));
411+
tbl.Columns.Add(new DataColumn("someColumn", typeof(string)));
412+
tbl.Rows.Add(new object[] { 1, "one", 2, "5" });
413+
tbl.Rows.Add(new object[] { 2, "two", 3, null/*SIMULATE EMPTY VALUE*/ });
414+
tbl.Rows.Add(new object[] { 3, "three", 4, "5" });
415+
tbl.Rows.Add(new object[] { 4, "four", 5, "5" });
416+
417+
return tbl;
418+
}
419+
````
420+
````VB
421+
Protected Sub RadGrid1_NeedDataSource(sender As Object, e As Telerik.Web.UI.GridNeedDataSourceEventArgs)
422+
TryCast(sender, RadGrid).DataSource = GetData()
423+
End Sub
424+
425+
Protected Function GetData() As DataTable
426+
Dim tbl As New DataTable()
427+
tbl.Columns.Add(New DataColumn("id", GetType(Decimal)))
428+
tbl.Columns.Add(New DataColumn("name", GetType(String)))
429+
tbl.Columns.Add(New DataColumn("data", GetType(Decimal)))
430+
tbl.Columns.Add(New DataColumn("someColumn", GetType(String)))
431+
tbl.Rows.Add(New Object() {1, "one", 2, "5"})
432+
'SIMULATE EMPTY VALUE
433+
tbl.Rows.Add(New Object() {2, "two", 3, Nothing})
434+
tbl.Rows.Add(New Object() {3, "three", 4, "5"})
435+
tbl.Rows.Add(New Object() {4, "four", 5, "5"})
436+
437+
Return tbl
438+
End Function
439+
````
440+
441+
>caption How to access the data item object by its index:
442+
443+
````JavaScript
444+
var grid = $find('<%= RadGrid1.ClientID %>');
445+
var masterTable = grid.get_masterTableView();
446+
var item = masterTable.get_dataItems()[3];//where 3 is the hierarchical index of the item you want
447+
````
343448

344449
* Using **get_cell()** method is the most straightforward approach to achieve that.
345450

0 commit comments

Comments
 (0)