2012-02-17 59 views
1

我有一個任務來設計具有多選功能的ComboBox(GXT)大小的控件。我試圖使用ComboBox的setView來設置CheckBoxListView,但似乎沒有工作。如果有任何使用GXT框架的方法,我可以實現這一目標嗎?任何人都可以引導我嗎?GXT - 具有多選功能的ComoboBox

PS:我發現了一個名爲XComboBox在煎茶論壇組件(Java類,源代碼),它提前做工不錯,但不能作爲其GNU GPL許可證下使用

謝謝!

回答

5

感謝@smiletolead爲您提供指導,我發現了一個將Dialog與CheckBoxListView和TriggerField類集成的解決方案。

完整的代碼清單..



    package com.ui.test.client; 

    import java.util.List; 

    import com.extjs.gxt.ui.client.data.ModelData; 
    import com.extjs.gxt.ui.client.event.ComponentEvent; 
    import com.extjs.gxt.ui.client.event.WindowEvent; 
    import com.extjs.gxt.ui.client.event.WindowListener; 
    import com.extjs.gxt.ui.client.store.ListStore; 
    import com.extjs.gxt.ui.client.widget.CheckBoxListView; 
    import com.extjs.gxt.ui.client.widget.Dialog; 
    import com.extjs.gxt.ui.client.widget.form.TriggerField; 
    import com.extjs.gxt.ui.client.widget.layout.FillLayout; 
    import com.google.gwt.user.client.Element; 

    public class MultiSelectComboBox extends TriggerField { 

     private Dialog checkBoxListHolder; 
     private CheckBoxListView listView; 
     private ListStore store; 

     private String delimiter = ","; 
     private boolean readOnly; 


     public MultiSelectComboBox() { 
      store = new ListStore(); 
      listView = new CheckBoxListView(); 
     } 




     @Override 
     protected void onTriggerClick(ComponentEvent ce) { 
      super.onTriggerClick(ce); 
      if(readOnly) { 
       return; 
      } 
      checkBoxListHolder.setSize(getWidth(), 200); 
      listView.setWidth(getWidth()); 
      checkBoxListHolder.setPosition(getAbsoluteLeft(), 
        getAbsoluteTop() + getHeight()); 
      if(checkBoxListHolder.isVisible()) { 
       checkBoxListHolder.hide(); 
      } 
      else { 
       checkBoxListHolder.show(); 
      } 
     } 




     @Override 
     protected void onRender(Element target, int index) { 
      super.onRender(target, index); 

      checkBoxListHolder = new Dialog(); 
      checkBoxListHolder.setClosable(false); 
      checkBoxListHolder.setHeaderVisible(false); 
      checkBoxListHolder.setFooter(false); 
      checkBoxListHolder.setFrame(false); 
      checkBoxListHolder.setResizable(false); 
      checkBoxListHolder.setAutoHide(false); 
      checkBoxListHolder.getButtonBar().setVisible(false); 
      checkBoxListHolder.setLayout(new FillLayout()); 
      checkBoxListHolder.add(listView); 
      listView.setStore(store); 

      checkBoxListHolder.addWindowListener(new WindowListener(){ 

       @Override 
       public void windowHide(WindowEvent we) { 
        setValue(parseCheckedValues(listView)); 
       } 

      }); 

     } 




     private String parseCheckedValues(CheckBoxListView checkBoxView) { 
      StringBuffer buf = new StringBuffer(); 
      if(checkBoxView != null) { 
       List selected = checkBoxView.getChecked(); 
       int index = 1, len = selected.size(); 
       for(D c : selected) { 
        buf.append(c.get(listView.getDisplayProperty())); 
        if(index getListView() { 
      return listView; 
     } 

     public void setListView(CheckBoxListView listView) { 
      this.listView = listView; 
     } 

     public ListStore getStore() { 
      return store; 
     } 

     public void setStore(ListStore store) { 
      this.store = store; 
     } 

     public String getDelimiter() { 
      return delimiter; 
     } 

     public void setDelimiter(String delimiter) { 
      this.delimiter = delimiter; 
     } 

     public boolean isReadOnly() { 
      return readOnly; 
     } 

     public void setReadOnly(boolean readOnly) { 
      this.readOnly = readOnly; 
     } 


    } 

的代碼已經在這裏解釋... http://bhat86.blogspot.com/2012/02/gxt-comobobox-with-multi-select-feature.html

謝謝!

+0

通過使用上面的代碼,我得到所有複選框與標籤0直到標籤9和所有複選框未選中。 現在我的需要是我想要複選框與「標籤2」應該檢查和休息應該保持未選中狀態。 我嘗試了各種選項,如刷新checkboxlistview和重新繪製商店。我不知道我要去哪裏錯了..請幫助 – Jayesh 2012-09-03 07:43:38

+0

創建上述小部件(比如MultiSelectComboBox mscb = new MultiSelectComboBox())後,您可以爲listView添加一個getter,並且可以說mscb.getListView()。setChecked(yourModel,true )。 – sanbhat 2012-09-06 05:48:36

0

請參考示例listviewadvanced list view。他們可能會幫助您開發帶多選選項的組合框

+0

XComboBox不是GXT組件;它的源代碼是作爲一個論壇成員的插件編寫的,他們希望以GPL許可證的形式發佈它。它不與GXT一起! – sanbhat 2012-02-22 07:17:27