2017-01-05 45 views
0

我儘量讓我的組合框多重選擇 這我的代碼:GWT組合框MULTISELECT

這multiSelectComboBox:

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<D extends ModelData> extends TriggerField<String> { 

    private Dialog checkBoxListHolder; 
    private CheckBoxListView<D> listView; 
    private ListStore<D> store; 
    private String delimiter = ","; 
    private boolean readOnly; 

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

    @Override 
    protected void onTriggerClick(ComponentEvent ce) { 
     super.onTriggerClick(ce); 
     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<D> checkBoxView) { 
     StringBuffer buf = new StringBuffer(); 
     if(checkBoxView != null) { 
      List<D> selected = checkBoxView.getChecked(); 
      int index = 1, len = selected.size(); 
      for(D c : selected) { 
       buf.append(c.get(listView.getDisplayProperty())); 
       if(index < len) { 
        buf.append(delimiter); 
       } 
       index++; 
      } 
     } 
     return buf.toString(); 
    } 

    public CheckBoxListView<D> getListView() { 
     return listView; 
    } 

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

    public ListStore<D> getStore() { 
     return store; 
    } 

    public void setStore(ListStore<D> 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; 
    } 

} 

然後使用這個類在我的項目:

// ... 
    ListStore<UserDTO> employeeList = getUsers(); 

    MultiSelectComboBox<ModelData> multiUsers = new MultiSelectComboBox<ModelData>(); 
    multiUsers.getListView().setDisplayProperty("label"); 
    //multiUsers.setStore(employeeList); 
    multiUsers.getStore().add(getModelData()); 
    multiUsers.setFieldLabel("Employee"); 
    //multiUsers.setEditable(true); 
    //multiUsers.setMaxHeight(200); 
    simpleForm.add(multiUsers, formData); 
    // ... 


private List<ModelData> getModelData() { 
    List<ModelData> list = new ArrayList<ModelData>(); 
    List<NctrUserDTO> employees= employeeList.getModels(); 

    for (int i=0; i<employeeList.getCount(); i++) { 
     ModelData data = new BaseModel(); 
     data.set("label", employeeList.getAt(i).getName()); 
     list.add(data); 
    } 

    return list; 
} 

但comboBox顯示爲空或沒有任何值,當我傳遞employeeList的值,但是當我嘗試使用靜態字符串值這工作正常,我不知道爲什麼這個問題oc請問,有什麼建議嗎?

+0

似乎比如你的問題更多與Sencha ExtJS/GXT有關。我添加了一些標籤(等待審覈),因此訂閱他們的人可以清楚地看到您的問題。 – walen

回答

1

聽起來好像你尋找類似的選項multiple select boxes,如果是的話你可以看看源代碼上Github的指針 - 或者,而不是重新發明輪子,只是使用GwtBootstrap3組件