Reference Grid Element from rowTemplate methods

4 posts, 1 answers
  1. Max
    Max avatar
    4 posts
    Member since:
    May 2020

    Posted 03 Mar Link to this post

    I have dynamically created, unpredictable (but always valid) kendo object .

    Inside this object, there will sometimes be a rowTemplate method.

     

    This object is then passed through a functionA that makes a few changes to the object (assigning default values etc.) before being passed into (sometimes) multiple kendogrids.

     

    However, a problem arises when the rowTemplate method needs to reference the kendo grid element that it exists inside:

    The parameters for rowTemplate only include the dataItem, which has no reference to the grid element or any of it's children and as the kendo object is created outside of where it is passed in to the kendo grid elements I have no scope to the grid element nor any ID it may have.

     

    I've tried looking at the arguments to check to see if there are any extra parameters that I'm missing and there are none.

    I have checked the value of this and it seems to evaluate to window.

    Event is also not something I think that I can make use of.

    https://dojo.telerik.com/ATesALoM/2

     

    Two workarounds that I have thought of:

    I could potentially attach a field when editing the object in functionA that is equal to the value of the to-be-created grid's ID and the grids can make use of this column in order to reference the kendo element.

    The other workaround would be to (from functionA) programmatically loop through the object in search of any function and to bind those functions to the grid element, then simply have those functions assume that "this" will always be equal to the kendo grid element.

    Both of these workarounds have drawbacks and I would rather only use them as a last resort.

     

    Is there is something I am missing that would greatly simplify this?

     

    If you require any clarification or examples needed please let me know and I'll provide them.

  2. Answer
    Georgi
    Admin
    Georgi avatar
    703 posts

    Posted 08 Mar Link to this post

    Hello Max,

    You could override the _rowsHtml method as shown below and the row template handler will be bound to the grid's instance:

          kendo.ui.Grid.fn._rowsHtml = function(data, templates) {
            var that = this,
                html = "",
                idx,
                rowTemplate = templates.rowTemplate.bind(that),
                altRowTemplate = templates.altRowTemplate.bind(that),
                length;
    
            for (idx = 0, length = data.length; idx < length; idx++) {
              if (that._skipRerenderItemsCount > 0) {
                that._skipRerenderItemsCount--;
              } else {
                if (idx % 2) {
                  html += altRowTemplate(data[idx]);
                } else {
                  html += rowTemplate(data[idx]);
                }
              }
              that._data.push(data[idx]);
            }
    
            return html;
          }

    For your convenience I have updated the provided sample:

    I hope this helps.

    Regards,
    Georgi
    Progress Telerik

    Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

  3. Max
    Max avatar
    4 posts
    Member since:
    May 2020

    Posted 09 Mar in reply to Georgi Link to this post

    Thanks Georgi, this is a pretty good solution!

     

    Overriding kendo's code with some homebrew does seem a little hacky still. Is there any chance the vanilla kendo code will be updated to include this change?

  4. Georgi
    Admin
    Georgi avatar
    703 posts

    Posted 12 Mar Link to this post

    Hi Max,

    I can suggest you to submit a feature request to our feedback portal. Based on the traction it gains, our PMs will consider adding it to the planning.

    Regards,
    Georgi
    Progress Telerik

    Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Back to Top