jquery - 如何在文档就绪功能中附加kendo Grid的数据绑定(bind)事件?

标签 jquery kendo-ui kendo-grid kendo-asp.net-mvc

我有一个 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/

相关文章:

javascript - 为什么 Kendo dataSource.transport.update 方法不向服务器发送数据?

javascript - 生成自动完成功能 Angular 和 Jquery

javascript - Kendo UI 网格多列使用字段名称隐藏/显示

mvvm - Kendo UI Grid创建的数据未发送到 Controller

jquery - 如何使用母版页在CSS中添加背景图片

javascript - 如何动态更改 jquery 弹出窗口的文本?

javascript - 设置 Bxslider 以加载不同的图像?

javascript - 将 View Web 表单 View 引擎格式中的字符串数组转换为 javaScript 对象

kendo-ui - 清除选择更改事件的自动完成值

javascript - 单元测试使用 Kendo Grid/Datasource 的 Angular Controller