Kendo-Knockout: Calling a method that changes viewmodel property from a template with data-binding inside a grid, breaks bindings

前端 未结 1 1199
感动是毒
感动是毒 2021-01-07 09:01

I am using RPNiemeyer`s kendo-knockout library. I have a kendo grid with a kendo template in it. In the template there is a button which uses knockout click binding which ca

相关标签:
1条回答
  • 2021-01-07 09:58

    Using Knockout templates inside of the grid is not fully supported at this point. Right now your row is bound, because the elements are there when Knockout first passes over the document to apply bindings.

    After the data is updated, the rows are re-rendered and the bindings are lost.

    One fix is to use an event handler for the "dataBound" event that rebinds the table. This could be done globally, something like:

    ko.bindingHandlers.kendoGrid.options.dataBound = function(data) {
        var body = this.element.find("tbody")[0];
    
        if (body) {
           ko.applyBindings(ko.dataFor(body), body);   
        }
    };
    

    Here is a sample: http://jsfiddle.net/rniemeyer/5Zkyg/

    I also added a custom binding that prevents Knockout from binding the table on the first pass, so that it is not bound twice (once overall applyBindings and once from the dataBound handler.

    Ultimately, this is something that I want to support better in Knockout-Kendo and it is the next thing that I plan to work on with the library.

    Here is a sample of how it might work from a branch that I had started a while back: http://jsfiddle.net/rniemeyer/xBL2B/

    0 讨论(0)
提交回复
热议问题