Introduction
This blog tells you how to get the selected row data item from the Grid using external button click event. Fetching the data item from the selected row of Kendo Grid can be easily done using the select function in Kendo Grid. Let us see how.
Please refer to this link to get the basic idea about KendoGrid and its events.
Prerequisites
- Basic knowledge in Kendo UI framework
- jQuery
Select function in kendo Grid
I have created one sample Grid to demonstrae how to fetch the selected row dataItem using Select function.
Create one new HTML page. In my case, I named it as GridSelect.html. Write the code given below in it.
GridSelect.html
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8"/>
- <title>Kendo UI Snippet</title>
-
- <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.3.1028/styles/kendo.common.min.css"/>
- <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.3.1028/styles/kendo.rtl.min.css"/>
- <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.3.1028/styles/kendo.silver.min.css"/>
- <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.3.1028/styles/kendo.mobile.all.min.css"/>
-
- <script src="http://code.jquery.com/jquery-1.12.4.min.js"></script>
- <script src="http://kendo.cdn.telerik.com/2016.3.1028/js/kendo.all.min.js"></script>
- </head>
- <body>
-
- <div id="grid"></div>
- <br/>
- <button type="button" id="btnSelectRow" value="GetValue" class="k-button">Get Row Value</button>
- <br/>
- <br/>
- <b> Product Name:</b> <label id="lblProductName" ></label>
- <br/>
- <br/>
- <b> Category:</b><label id="lblCategoryName"></label>
- <script>
- $("#grid").kendoGrid({
- selectable: "true",
- allowCopy: true,
- columns: [
- { field: "productName" },
- { field: "category" }
- ],
- dataSource: [
- { productName: "Tea", category: "Beverages" },
- { productName: "Coffee", category: "Beverages" },
- { productName: "Ham", category: "Food" },
- { productName: "Bread", category: "Food" }
- ]
- });
- $("#btnSelectRow").click(function(){
-
- var grid = $("#grid").data("kendoGrid");
- var selectedItem = grid.dataItem(grid.select());
-
- $("#lblProductName").text(selectedItem.productName);
- $("#lblCategoryName").text(selectedItem.category);
-
- })
- </script>
- </body>
- </html>
From the above code, you can observe that the button click event is written where we have fetched the data item of the selected row in the Kendo Grid using Select function. From the data item, we have extracted the product name and category and set it to the Product Name and Category Label respectively.
Result
I hope you have enjoyed this blog. Your valuable feedback, questions, or comments about this blog are always welcome.