2013-09-30 64 views

回答

0

對於相同的情況下,我曾經創造了一個自定義窗口小部件一樣(我叫它按鈕選擇器):

define([ 
    "dojo/_base/declare", 
    "dijit/_WidgetBase", 
    "dijit/form/ToggleButton", 
    "put-selector/put" 
], function (declare, _WidgetBase, ToggleButton, put) { 


    /////////////////////////////////////////////////// 
    // 
    // Define a generic Button style selector 
    // 
    //////////////////////////////////////////////////// 
    return declare([_WidgetBase], { 

     valueAttr: 'value', 
     labelAttr: 'label', 
     titleAttr: 'title', 
     data: [], 
     selectedValue: null, 

     postCreate: function() { 

      this.inherited(arguments); 

      this.containerDiv = put(this.domNode, "div.wpt-button-selector"); 
      this.buttons = []; 

      var getClickHandler = function (_this, _item){ 
       return function(evt){ 
        _this.setSelectedValue(_item[_this.valueAttr]); 
       };  
      } ; 

      for (var index = 0; index < this.data.length; index++) { 
       var item = this.data[index]; 

       this.buttons.push(new ToggleButton({ 
        label:item[this.labelAttr] || "", 
        title:item[this.titleAttr]||"", 
        iconClass: item.iconClass, 
        checked: item[this.valueAttr] === this.selectedValue, 
        onClick:getClickHandler(this,item) 
       }, put(this.containerDiv, "span"))); 
      } 
     }, 

     getSelectedValue: function() { 
      return this.selectedValue; 
     }, 

     setSelectedValue: function (newValue) { 
      if (this.selectedValue != newValue) { 
       this.selectedValue = newValue; 
       this.selectionChanged(newValue); 
      } 
      this.refresh(); 
     }, 

     getSelectedIndex: function() { 

      for (var i = 0; i < this.data.length; i++) { 
       if (this.data[i][this.valueAttr] === this.selectedValue) { 
        return i; 
       } 
      } 
      return -1; 
     }, 

     refresh: function() { 

      for (var i = 0; i < this.data.length; i++) { 
       this.buttons[i].set("checked", 
        this.data[i][this.valueAttr] === this.selectedValue); 
      } 
     }, 

     //please overwrite o provide really handler 
     selectionChanged: function (selectedValue) { 

     } 
    }); // End of ButtonSelector Definition 

}); 

然後你可以使用它像:

new ButtonSelector({ 
     data: [{ 
      value: value1, 
      iconClass: img1, 
      title: Title1 
     }, { 
      value: value2, 
      iconClass: img2, 
      title: Title2 
     }, { 
      value: value3, 
      iconClass: img3, 
      title: Title3 
     }], 
     selectionChanged: function (value) { 
       // you logic to handle change 
     } 
    }, put(toolBar.containerNode, "div div")) 
    .setSelectedValue(yourInitialvalue); 
+0

嗨,我已經下載把選擇器,創建新的js文件與頂部的代碼,甚至調用底層代碼之前,我曾經錯誤我有你的JS文件: 錯誤:multipleDefine 比 類型錯誤:_37.set不widgetbase.js功能 您可以在這裏更新此任何機會: http://jsfiddle.net/SDN2e/ – Alophind

+0

這是我自己的項目工作定製部件。我習慣於使用編程風格而不是模板風格。你可以按照我的想法來創建你自己的版本。對於模板版本,我建議你從[另一個問題的答案]想法(http://stackoverflow.com/questions/19125105/say-i-have-many-divs-and-each-div-trigger-an -event-時,點擊/ 19125338#19125338)。 –

+0

供參考:我使用這個小窗口在[bi2.io](http://bi2.io)很多。 –