Kendo UI Grid post rendered or post databound event?
Asked Answered
S

3

24

Is there a way to trigger an event after grid has been reloaded via ajax?

i see the RequestEnd event. but that seems to happen when the request returned, but before the grid has been refreshed.

i also see DataBound event. but that happens even earlier than RequestEnd,
also when i implement DataBound event, my header disappears..

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. 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.

update: adding databound definition

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

and here's grid ajax definition

   .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.

update

corrected the DataBound event hook.

in dataBound function, i'm trying to get a reference to newly rendered templates..

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

Submariner answered 13/2, 2014 at 16:37 Comment(9)
the DataBound event is triggered after the DOM is updated; if you're not getting the result you expected, there is a problem with your code, so you should add thatMarseillaise
@LarsHöppner added that code.. let me know if more is needed.Submariner
those span.editable elements are part of which template?Marseillaise
they are in ClientTemplates for few columns.. made the latest code a bit more clear.. the fields do show up, just with some delay after DataBound.. so it's evident that this event happens before rendering of new rowsSubmariner
also added the client template definition.. what i am looking for is a event that fires after the rows have been addedSubmariner
that's what dataBound is for - like I said, it definitely gets triggered after the rows are updated; I'm not sure what the problem is in your case; I can only suggest adding your full grid definition here; or maybe you can upload a sample project somewhereMarseillaise
well from my example it's pretty clear that it gets triggered before the rows are rendered. you can see the 2 consecutive lines and the one with 500ms delay finds the new rows.Submariner
all I know is that if you read the source code, you'll see that triggering dataBound is the last thing that happens in the grid's refresh method (and that method gets called after the data source sends its change event); there's also no asynchrony in the code that would explain what you're seeing; so I'm pretty sure there must be another epxlanation, but I can't see it from the information you've givenMarseillaise
hmm.. this is a pickle indeed. well thanks for the help!Submariner
I
12

See this sample code taken from the documentation (API docs on events are here) on how to bind an event handler using MVC wrappers:

@(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:

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.

Indecent answered 13/2, 2014 at 19:51 Comment(4)
right on.. i missed that new-instance part, and the events i was looking at were on DataSource, and not on Grid. the problem with the documentation you posted, is it's a long a-z how to article. i really wanted to find just a Events api documentation.. which seemed like a impossible taskSubmariner
actually DataBound still happens too soon. it happens at the same time as getting the collection back, but before the contents are rendered. i know this because im calling some javascript that selects rendered elements.. i'll post a sampleSubmariner
as a fun bonus, when i add that dataBound: function(e) definition with just the console.debug line, my whole grid header drops to the bottom of the grid!Submariner
well, that should obviously not happen..can't help without a demo of the problem unfortunatelyMarseillaise
L
1

you can use this way:

 transport: {
           read: {
           url: searchUrl,
           type: "POST",
           dataType: "json",
           data: additionalData,
           complete: function () {
              //code here :)
           }
        },                   
     },
Lice answered 18/3, 2019 at 3:52 Comment(0)
S
0

I have had situations where, (in a pinch), a MutationObserver may be deployed to "sense" when the grid has inserted rows into the DOM. In most cases, the grid's own dataBound event will suffice. However, when there's:

  • render-blocking JS, maybe on initial page load, and/or
  • slow connection/high latency, and/or
  • poorly constructed jumble of Kendo JS from server-side wrapper(s), mixed in with script blocks

Anyway, prior to rows being rendered the tbody tag buried within the grid will look like this:

               <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. In effect, this creates a "rowsRendered" grid event. 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.

Sacred answered 9/3, 2021 at 23:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.