2014-10-11 60 views
1

繼這個example我試圖編寫一個插件,或者更好地將擴展名寫入kendoGrid。 我的目標是爲kendoGrid配備額外的構造參數,比如:一個名爲format的回調函數,它將提供數據更改的自定義條件格式。用額外的構造函數參數擴展kendoGrid

令我感到困惑的是,該示例實際上構造了一個全新的窗口小部件,而我希望kendoGrid構造函數保持不變,只是採用一個新參數。我想避免將kendoGrid的所有調用更改爲kendoMyPlugin。這是可行的嗎?

回答

2

鏈接中的示例擴展了Widget類。這是派生所有Kendo UI小部件的基類。在你的情況下,你想擴展基諾網格並從那裏添加功能。您不能更改init方法的簽名,因爲這是由Kendo內部調用的,但您可以輕鬆地添加自定義選項來執行您所需的操作。基本模式是:

(function ($, undefined) { 

    if (!kendo.ui.MyGrid) {  

     var base = kendo.ui.Grid; 

     var MyGrid = base.extend({ 

      init: function (element, options) { 

       // Call the base class's init. 
       base.fn.init.call(this, element, options); 

       // Add initialization... 
       this.myField1 = options.myOption1; 

       // If you want to watch changes in the dataSource 
       // attached to the grid, you could use this. Of 
       // course you don't even need to subclass to do this 
       // but you get the point. 
       this.dataSource.bind("change", function(e) { 
        if (that.options.myCallback) { 
         that.options.myCallback.call(that, e); 
        } 
       }; 
      }, 

      // Add default options... 
      options: { 
       myOption1: "Hello", 
       myOption2: "World", 
       myCallback: undefined, 
      } 

      // Add events... 
      events: [ 
       "myEvent1", 
       "myEvent2" 
      ], 

      // Add fields... 
      myField1: "Goodbye", 

      // Add methods... 
      myMehod: function (a, b, c) { 
       // Do something... 
      } 
     } 

     // Register the new widget. 
     kendo.ui.plugin(MyGrid); 
    }  
})(jQuery); 

MyGrid現在具有Grid的所有功能以及您添加的功能。然後,就像您使用網格一樣創建一個新實例。

$("#someElement").kendoMyGrid({ 
    // Add options for standard kendo Grid and your new options... 
    myCallback: function (e) { 
     alert(e.action); 
    } 
}); 
+0

謝謝moomoo!在這一刻我無法測試代碼,但我很感謝你的回答。看着它,我擔心的是dataSource的change事件是被攔截還是停止傳播,因此用戶仍然可以在構造函數中爲它分配自己的處理程序? – user776686 2014-10-15 08:06:44

+0

是的,這就像一個魅力!我甚至不需要將構造函數改寫爲'kendoMyGrid' - 默認的'kendoGrid'仍然可以工作。 – user776686 2014-10-16 18:09:32

+0

酷!實際上,我已經將近4K的代碼添加到了Grid(我稱之爲GridEx),以執行各種操作。沒有任何我可以分享的。 – moomoo 2014-10-17 01:45:09