简体   繁体   English

如何从DataSource事件处理程序获取kendoGrid ID?

[英]How to get kendoGrid id from DataSource event handler?

I have a kendo grid defined inside a razor view (MVC4) , and I also have dataSource event handler - onRequestStart like this: 我在剃刀视图(MVC4)内定义了一个(MVC4)网格,并且我还有dataSource事件处理程序-onRequestStart像这样:

function onRequestStart(e){
     var gridId = e.sender...   //the rest of the code
}

What I am trying to do is to get gridId inside function without passing it as parameter to this function. 我想做的是在函数内部获取gridId而不将其作为参数传递给该函数。 I also tried: 我也尝试过:

e.sender.element.closest('[data-role="grid"]');

and: 和:

e.sender.options.table.context.id //('e.sender.options.table' is null)

but it doesn't work! 但这不起作用! Kendo version 2016.1.226 Kendo版本2016.1.226

Based on the comments, it is probably simpleest to configure the grid databinding and dataBound events of each grid you want to show something for. 根据评论,配置要显示其内容的每个网格的网格数据绑定和dataBound事件可能是最简单的。

So, in the .cshtml 因此,在.cshtml中

@(Html.Kendo().Grid<grid-1-model-class>()
  .Name("grid1")
  .Events(events => {
     events.DataBinding("commonDataBinding");
     events.DataBound("commonDataBound");
  })
  .DataSource (...)
  .Columns(...)
  ...
)

@(Html.Kendo().Grid<grid-2-model-class>()
  .Name("grid2")
  .Events(events => {
     events.DataBinding("commonDataBinding");
     events.DataBound("commonDataBound");
  })
  .DataSource (...)
  .Columns(...)
  ...
)

<script>
function commonDataBinding(e) {
  // going for data
}
function commonDataBound(e) {
  // data is gotten
}
</script>

e.sender will be the kendo grid component. e.sender将成为剑道网格组件。

You can create a custom grid widget like so and add the attached grid if you don't ever use it for more than one grid at a time. 您可以像这样创建一个自定义的网格小部件,并在一次不只使用一个网格的情况下添加附加的网格。

here is the dojo for it. 这是它的道场。 https://dojo.telerik.com/OSovidOy https://dojo.telerik.com/OSovidOy

<div id="my-grid"></div>
  <script>

    var CustomGrid = kendo.ui.Grid.extend({
        options: {
            name: 'CustomGrid'
        },
        _dataSource: function() {
          kendo.ui.Grid.fn._dataSource.call(this);
          this.dataSource.attachedGrid = this;
        }
    });

    kendo.ui.plugin(CustomGrid);

    $('#my-grid').kendoCustomGrid({
        dataSource: {
          requestStart: function(e) {
                console.log(e.sender.attachedGrid);
                var gridId = e.sender.attachedGrid.element.attr('id');
          },
          data: [
            { id: 1, fullname: 'David Lebee' }, 
            { id: 2, fullname: 'Chuck Norris' } 
          ]
        }
    });
  </script>

More hackish less clean if you ask me, but since you are using CSharp builders I doubt you want to inherit their fluent builder (having to go through this trouble once). 如果您问我,更多的杂物将变得更加干净,但是由于您使用的是CSharp构建器,因此我怀疑您想继承其流畅的构建器(必须经历一次这种麻烦)。

You can override the grid by your own widget class like so. 您可以像这样用自己的窗口小部件类覆盖网格。

here is the dojo again: 这又是道场:

https://dojo.telerik.com/ISUxAWoj https://dojo.telerik.com/ISUxAWoj

<div id="my-grid"></div>
  <script>

    var originalDataSourceMethod = kendo.ui.Grid.fn._dataSource;

    var overrideGrid = kendo.ui.Grid.extend({
        _dataSource: function() {
          originalDataSourceMethod.call(this);
          this.dataSource.attachedGrid = this;
        }
    });

    kendo.ui.plugin(overrideGrid);

    $('#my-grid').kendoGrid({
        dataSource: {
          requestStart: function(e) {
                console.log(e.sender.attachedGrid);
                var gridId = e.sender.attachedGrid.element.attr('id');
          },
          data: [
            { id: 1, fullname: 'David Lebee' }, 
            { id: 2, fullname: 'Chuck Norris' } 
          ]
        }
    });
  </script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM