我有一个 MVC Kendo UI 网格,我正在尝试附加 dataBound 事件。以下是我尝试使用的两种方法。如果可以的话,我更喜欢第一种方法
@(Html.Kendo().Grid<BatchDetails>()
.Name("Grid")
.Columns(col =>
{
col.Bound(p => p.BatchKey).Title("State");
col.Bound(p => p.OriginalCost).Title("Total Cost");
})
.AutoBind(true)
.DataSource(dataSource => dataSource
.Ajax()
.PageSize(20)
.Read(read => read
.Action("GetData", "Home", new { batchID = Model.BatchID })))
)
1> 我想在文档就绪函数中绑定(bind)网格的 DataBound 事件。 但是当我这样做时,我得到空引用
$function({
var grid = $("#Grid").data("kendoGrid"); // grid variable is null here
grid.bind("dataBound", function(){
//dosomething here
});
})
2> 如果我使用以下方法绑定(bind)事件,那么我必须将 onDataGridBound 函数声明为全局函数。网格无法访问作用域函数
@(Html.Kendo().Grid<BatchDetails>()
.Name("Grid")
.Events(x => x.DataBound("onDataGridBound"))
)
有没有办法将 onDataGridBound 声明为作用域函数,并且网格仍然可以访问它?
最佳答案
根据 Telerik documentation您必须确保您的就绪函数位于小部件声明之后:
You can get a reference to the client-side object initialized by the server-side wrapper via the data jQuery method. Use the Name of the widget in an ID jQuery selector and obtain the reference in a document.ready handler, which is placed or called after the widget declaration. This will ensure that the widget is already initialized and the client-side object exists. After you get the object reference, you can use the widget's client-side API.
@(Html.Kendo().NumericTextBox()
.Name("age")
)
<script>
$(function(){
var numeric = $("#age").data("kendoNumericTextBox");
numeric.value(10);
});
</script>
关于jquery - 如何在文档就绪功能中附加kendo Grid的数据绑定(bind)事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30599818/