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

Get value of parent row from client side OnCommand

7 Answers 700 Views
Grid
This is a migrated thread and some comments may be shown as answers.
JohnH
Top achievements
Rank 1
JohnH asked on 12 Aug 2010, 08:01 PM
How can I get the value of a datakey in the parent row on the client side if I click on a command item in my child radgrid?

I can do

var netID = sender.get_parent().get_masterTableView().get_dataItems()[0].getDataKeyValue("network_id");

but that always returns the first row in the parent gridview.  I need the data key value of the parent for the child grid command button click.  Ideas?



RadGrid code
<telerik:RadGrid ID="RadGrid1" DataSourceID="SqlDataSource1" AllowSorting="True"
      runat="server" GridLines="None" Width="95%">
      <MasterTableView DataKeyNames="network_id" ClientDataKeyNames="network_id">
          <NestedViewTemplate>
              <telerik:RadGrid ID="RadGrid2" DataSourceID="SqlDataSource2" runat="server">
                  <ExportSettings IgnorePaging="true" OpenInNewWindow="true" ExportOnlyData="false" FileName="NetworkHorseblanketListExport" />
                  <MasterTableView CommandItemDisplay="Top">
                      <CommandItemSettings ShowRefreshButton="false" ShowAddNewRecordButton="false" ShowExportToExcelButton="true"  />     
                  </MasterTableView>
                  <ClientSettings>
                      <ClientEvents OnCommand="OnCommand" />
                  </ClientSettings>                        
             </telerik:RadGrid>
          </NestedViewTemplate>
      </MasterTableView>
</telerik:RadGrid>

Javascript code:
function rgNetworkEquipment_OnCommand(sender, args)
{
   var cmd = args.get_commandName();
                     
   if (cmd == "ExportToExcel")
   {
      var parentRadGrid = sender.get_parent();
 
      // get the data key of the parent here
 
 
   }
}

7 Answers, 1 is accepted

Sort by
0
Rosen
Telerik team
answered on 17 Aug 2010, 12:17 PM
Hello John,

You should find the row element which is the parent of the NestedViewItem and get a reference to its object representation using the element's id. For example:

<script type="text/javascript">
    function OnCommand(sender, args) { 
                                     
        //find command item owner grid's html element 
        var ownerGridElement = sender.get_element();                
        //find parent item html element's id
        var parentRowId = $(ownerGridElement).parents("tr").first().prev("tr.rgRow, tr.rgAltRow")[0].id;
        if (parentRowId != "") {
            //ensure that the outer grid dataItem are generated
            sender.get_parent().get_dataItems();
            //get referance to parent item object and retrive the datakey value
            alert($find(parentRowId).getDataKeyValue("network_id"));
        }
    }
</script>


All the best,
Rosen
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
JohnH
Top achievements
Rank 1
answered on 17 Aug 2010, 02:37 PM
var parentRowId = $(ownerGridElement).parents("tr").first().prev("tr.rgRow, tr.rgAltRow")[0].id;

Does not work for me...(ownerGridElement).parents -> Object doesn't support this property or method

If I inspect the properties of the sender grid in IE Dev Tools (ownerGridElement) object does not have a parent property.  It has a parentElement property but if I try (ownerGridElement).parentElement
("tr"), it also doesn't work.  I understand your approach but I can't figure out how to get there.  getDataKeyValue will certainly work if we can find the element id for the correct row.

Any other ideas?





0
JohnH
Top achievements
Rank 1
answered on 17 Aug 2010, 02:46 PM
Disregard previous post.  Your solution works fine as soon as I realized you were using jquery and included the library in the page.  Thanks!
0
Doug
Top achievements
Rank 1
answered on 04 Feb 2014, 11:21 PM
Is this answer still valid? When I evaluate sender after capturing a CommandButton Click client side, the sender is the RadGrid, not the button, so sender.get_parent returns not the grid, but the ajax panel that contains the grid. Similarly, when I use:
$(sender.get_element()).first().prev("tr.rgRow, tr.rgAltRow")[0]
I get null (undefined) results.
What is the point of imbedding command buttons in a grid if one cannot tell their row/column location? It seems this is a weekness of the client side implementation and shold be fixed in your next release.
0
Kostadin
Telerik team
answered on 07 Feb 2014, 02:21 PM
Hi Doug,

Thank you for contacting us.

Could you please verify that you are using a NestedViewTemplate? I prepared small sample and on my side seems to work correct. Could you please give it a try and let me know how it differs from your real setup.

Regards,
Kostadin
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the UI for ASP.NET AJAX, subscribe to the blog feed now.
0
Doug
Top achievements
Rank 1
answered on 07 Feb 2014, 06:47 PM
Oh, sorry. I am not using a child Grid, as the original post (John's) stated. So, my question was not realy related to this thread.

That said, I am just trying to discern which row a command button is from when i deal with the client side click. I add the GridButtonColumn to the MasterTableView.Coluns dynamically on the server and then hope to use the OnCommand ClientEvent to act on the button click, however, neither the sender (the grid), nor the event argument seem to carry the button's row information. So my question was about how that data can be found.
0
Doug
Top achievements
Rank 1
answered on 11 Feb 2014, 01:16 AM
I found a way to calculate the absolute row index by way of the command arguements:
var tableView = eventArgs.get_tableView();
var rowIndex = (tableView.PageSize * tableView.CurrentPageIndex) + parseInt(eventArgs.get_commandArgument());
Tags
Grid
Asked by
JohnH
Top achievements
Rank 1
Answers by
Rosen
Telerik team
JohnH
Top achievements
Rank 1
Doug
Top achievements
Rank 1
Kostadin
Telerik team
Share this question
or