简体   繁体   English

Kendo UI Grid 发布渲染或发布数据绑定事件?

[英]Kendo UI Grid post rendered or post databound event?

Is there a way to trigger an event after grid has been reloaded via ajax?有没有办法在通过ajax重新加载网格后触发事件?

i see the RequestEnd event.我看到RequestEnd事件。 but that seems to happen when the request returned, but before the grid has been refreshed.但这似乎发生在请求返回时,但在网格刷新之前。

i also see DataBound event.我也看到了DataBound事件。 but that happens even earlier than RequestEnd,但这甚至比RequestEnd更早发生,
also when i implement DataBound event, my header disappears..同样,当我实现 DataBound 事件时,我的标题消失了..

i had to resort to this hack我不得不求助于这个黑客

function requestEnd(o) {
    console.debug('request ended.', o);
    setTimeout(refreshEditable, 500); // enough time to render the grid
}
function refreshEditable() {
    // perform my actions on controls within grid content
}

as a side note.. I am having a very hard time finding a reliable kendo grid mvc API reference.作为旁注..我很难找到可靠的剑道网格 mvc API 参考。 when i google for it, i get this: http://docs.telerik.com/kendo-ui/getting-started/using-kendo-with/aspnet-mvc/migration/widgets/grid which is a collection of little how-to and some "Events" but those don't correspond to what I am seeing in razor intelisense.当我用谷歌搜索它时,我得到了这个: http : //docs.telerik.com/kendo-ui/getting-started/using-kendo-with/aspnet-mvc/migration/widgets/grid这是一个小集合-to 和一些“事件”,但这些与我在剃刀智能感知中看到的不符。

update : adding databound definition更新:添加数据绑定定义

    $('#grid').kendoGrid({
        dataBound: function(e) {
            console.debug('data bound..');
        }
    });

and here's grid ajax definition这是网格 ajax 定义

   .Ajax().Read(read => read
        .Action("FilesRead", "SomeController")
        .Data("readData"))

 function readData() {
    return {
        IncludeChildren: $("#IncludeChildren").is(':checked'),
        SearchString: $('input[id=SearchString]').val()
    };
 }

i can see that DataBound is triggered while making the ajax call, not after it comes back.我可以看到在进行 ajax 调用时触发了 DataBound,而不是在它返回之后。

update更新

corrected the DataBound event hook.更正了 DataBound 事件挂钩。

in dataBound function, i'm trying to get a reference to newly rendered templates..在 dataBound 函数中,我试图获取对新渲染模板的引用..

function dataBound(o) {
  console.debug($('span.editable').length);                    // returns 0 
  setTimeout("console.debug($('span.editable').length)", 500); // returns 4
}

the spans are added using a client template跨度是使用客户端模板添加的

.ClientTemplate(@"<span class=""editable"" ... >#=DOCUMENT_DATE_FORMATTED#</span>");

see what i mean?明白了吗? data bound happens before grid is rendered数据绑定发生在呈现网格之前

See this sample code taken from the documentation (API docs on events are here ) on how to bind an event handler using MVC wrappers:有关如何使用 MVC 包装器绑定事件处理程序,请参阅从文档中获取的示例代码(事件的 API 文档在此处):

@(Html.Kendo().Grid(Model)
      .Name("grid")
      .Events(e => e
          .DataBound("grid_dataBound")
          .Change("grid_change")
      )
)
<script>
function grid_dataBound() {
    //Handle the dataBound event
}

function grid_change() {
    //Handle the change event
}
</script>

If you want to bind a handler in JavaScript, you need to access the grid like this:如果你想在 JavaScript 中绑定一个处理程序,你需要像这样访问网格:

var grid = $("#grid").data("kendoGrid");
grid.bind("dataBound", function(e) {});

When you do this here:当您在此处执行此操作时:

$('#grid').kendoGrid({
    dataBound: function(e) {
        console.debug('data bound..');
    }
});

you actually create a new grid instance.您实际上创建了一个新的网格实例。

you can use this way:你可以这样使用:

 transport: {
           read: {
           url: searchUrl,
           type: "POST",
           dataType: "json",
           data: additionalData,
           complete: function () {
              //code here :)
           }
        },                   
     },

I have had situations where, (in a pinch), a MutationObserver may be deployed to "sense" when the grid has inserted rows into the DOM.我遇到过这样的情况,(在紧要关头),当网格已将行插入到 DOM 中时,可能会部署 MutationObserver 以“感知”。 In most cases, the grid's own dataBound event will suffice.在大多数情况下,网格自己的 dataBound 事件就足够了。 However, when there's:但是,当有:

  • render-blocking JS, maybe on initial page load, and/or渲染阻塞 JS,可能在初始页面加载时,和/或
  • slow connection/high latency, and/or慢速连接/高延迟,和/或
  • poorly constructed jumble of Kendo JS from server-side wrapper(s), mixed in with script blocks来自服务器端包装器的 Kendo JS 结构混乱,与脚本块混合

Anyway, prior to rows being rendered the tbody tag buried within the grid will look like this:无论如何,在呈现行之前,埋在网格中的 tbody 标签将如下所示:

               <tbody>
                   <tr class="k-no-data">
                       <td colspan="9"></td>
                   </tr>
               </tbody>

and after the rows are rendered it will like:并且在呈现行之后它会像:

 <tbody role="rowgroup">
    <tr data-uid="004c8970-ba7e-4e3c-ae54-2695c6cbdbe8" role="row" class="k-state-selected"
       aria-selected="true">
       <td role="gridcell">07/18/2004</td>
       <td role="gridcell">24</td>
       <td role="gridcell">1890</td>
       <td role="gridcell">0</td>
       <td role="gridcell">176</td>
       <td role="gridcell">0</td>
       <td role="gridcell">2439</td>
       <td role="gridcell">2500</td>
       <td role="gridcell"></td>
    </tr>
      .....more rows, then
 </tbody>

so, something like:所以,像:

    let observer = new MutationObserver(mCallback);
    function mCallback(mutations) {
        for (let mutation of mutations) {
            if (mutation.addedNodes.length > 0) {
                doAutoDemoChart();
            }
        }
    }
    let observerOptions = { childList: true }
    let gridContent = document.querySelector("#dailyProdGrid div.k-grid-content tbody")
    observer.observe(gridContent, observerOptions);

will detect the change in DOM.将检测 DOM 的变化。 In effect, this creates a "rowsRendered" grid event.实际上,这会创建一个“rowsRendered”网格事件。 I had a situation where there was a lot riding on rows being present for a chart demo;我曾遇到过在图表演示中存在大量行的情况; (see here ). (见这里)。 Programmatically, rows needed to be selected (based on a prescribed date range), then a window opened and filled with a logarithmic chart.以编程方式,需要选择行(基于规定的日期范围),然后打开一个窗口并填充对数图表。

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

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