Reputation: 130
I have a kendo grid with a custom command. Now issue is I want to fire that custom command for specific row but not able to do it. Here is my code:
Grid:
@(Html.Kendo().Grid<RxConnectEntities.UserDTO>
().Name("UserList")
.Columns(columns =>
{
columns.Bound(p => p.UserID).Visible(false);
columns.Command(command => { command.Custom("Select").Click("OnSelectRow"); }).Width("80px");
columns.Bound(p => p.LastName).Title("Last Name");
columns.Bound(p => p.FirstName).Title("First Name");
})
.Filterable(filterable => filterable
.Extra(false)
.Operators(operators => operators
.ForString(str => str.Clear()
.StartsWith("Starts with")
.IsEqualTo("Is equal to")
.IsNotEqualTo("Is not equal to")
.Contains("Contains")
))
).ToolBar(x => x.Template("<a class='k-button k-button-icontext k-grid-export pull-right' title='Export to Excel' data-title='Users'><div class='k-grid-export-image k-icon'></div>Export To Excel</a>"))
.HtmlAttributes(new { @Style = "height:580px" })
.Sortable()
.Groupable()
.Scrollable()
.Events(events => events.DataBound("onDataBound").ColumnShow("onColumnShow"))
.Pageable(p => p.PageSizes(new int[] { 10, 20, 30, 40, 50 }).Enabled(true).Refresh(true))
.DataSource(dataSource => dataSource
.Ajax().ServerOperation(true).PageSize(20)
.Model(m => m.Id(p => p.PrescriberID))
.Read(read => read.Action("GetUserList", "User").Data("PassData"))
))
Javascript:
function OnSelectRow(e) {
var grid = $("#UserList").data("kendoGrid");
var dataItem = this.dataItem($(e.currentTarget).closest("tr"));
alert("User Detail: " + dataItem.UserID + " " + dataItem.FirstName + " " + dataItem.LastName");
}
Now I have a textbox and a button, in textbox user enter UserID and I have to fire onSelectRow event of kendo grid for that UserID. I know this trick
var row = $("#" + gridName).data("kendoGrid").table.find('tr[data-id="' + id + '"]')
but it's not working for me. It is not giving me anything in row var.
Note: I am using asp.net mvc5 with c#.
Upvotes: 1
Views: 1334
Reputation: 43748
data-id
is not put into the <tr>
elements by Kendo Grid. It uses data-uid
instead, which is a generated GUID id that is made by the DataSource
.
You could get the uid for your item by doing:
var id = 123; // get the ID from the text box
var gridWidget = $("#" + gridName).data("kendoGrid");
var dataItem = gridWidget.dataSource.get(id);
var uid = dataItem.uid;
var row = .table.find('tr[data-uid="' + uid + '"]');
Of course you could probably simplify your button click handler to just:
// called from grid action
function OnSelectRow(e) {
var grid = $("#UserList").data("kendoGrid");
var dataItem = this.dataItem($(e.currentTarget).closest("tr"));
showDetails(dataItem);
}
// called by button outside grid
function OnSelectRowManually() {
var id = 123; // get the ID from the text box
var dataItem = $("#UserList").data("kendoGrid").dataSource.get(id);
showDetails(dataItem);
}
// reusable code to show details
function showDetails(dataItem) {
alert("User Detail: "
+ dataItem.UserID
+ " "
+ dataItem.FirstName
+ " "
+ dataItem.LastName");
}
Upvotes: 1