2013-02-05 110 views
1

我正在使用zk 6.5和以下MVVM模式,我面臨的問題使用 listbox mould =「select」我正在從數據庫中獲取數據, - 請選擇 - 作爲第一選擇的選項,在我的列表框,但我不能做到,這裏是我的代碼無法在列表框中設置選定的值模型=「選擇」在zk

這是我.zul頁

<listbox style="width:70px" id="lstGrpNames" selectedItem="@bind(vmmodel.selSearchGroup)" mold="select" tabindex="0"> 

        </listbox> 

//這是我的控制器代碼

@Wire("#lstGrp") 
    private Listbox lstGrp; 

@AfterCompose(superclass=true) 
    public void afterCompose(@ContextParam(ContextType.VIEW) Component view) { 
     Selectors.wireComponents(view, this, false); 

     if (lstGrp != null) { 

      lstGrp.setSelectedItem(lstGrp 
        .appendItem("--Select--", "")); //i want this to be the selected option while page is loaded 

      if (this.groups != null && this.groups.size() > 0) { 
       for (Group groupObj : this.groups) { 
        lstGrp.appendChild(new Listitem(groupObj.getName(), 
          groupObj)); 
       } 
      } 
      lstGrp.setSelectedItem(lstGrp.getItemAtIndex(0)); 
} 
+0

你確定你使用MVVM模式的正確方法?通常情況下,ViewModel不應該有任何對視圖組件的引用。也許MVC更適合您的需求。 – bidifx

回答

0

這是你可以做到這一點..的演示代碼

ZUL文件..

<listbox model="@bind(vm.dataList)" > 
     <listhead> 
      <listheader value="A"></listheader> 
      <listheader value="B"></listheader> 
      <listheader value="C"></listheader> 

     </listhead> 
     <template name="model" var="mymodel"> 
      <listitem> 
       <listcell> 

        <textbox value="@bind(mymodel.a)" /> 
       </listcell> 
       <listcell> 
        <label value="@bind(mymodel.b)" /> 

       </listcell> 
       <listcell> 
        <label value="@bind(mymodel.c)" /> 

       </listcell> 
      </listitem> 
     </template> 
    </listbox> 
</window> 

的演示代碼

Java代碼...

package com.team; 

import java.util.ArrayList; 
import java.util.List; 

import org.zkoss.bind.annotation.AfterCompose; 
import org.zkoss.bind.annotation.Command; 
import org.zkoss.bind.annotation.ContextParam; 
import org.zkoss.bind.annotation.ContextType; 
import org.zkoss.bind.annotation.NotifyChange; 
import org.zkoss.zhtml.Messagebox; 
import org.zkoss.zk.ui.Component; 
import org.zkoss.zul.ListModelList; 


public class MyListbox { 

    private ListModelList<Data> dataList; 

    @AfterCompose 
    public void afterCompose(@ContextParam(ContextType.VIEW) Component view) { 
     try { 
      dataList = new ListModelList<Data>(); 
      Data data; 
      data = new Data("a1", "b1", "c1"); 
      dataList.add(data); 
      data = new Data("a2", "b2", "c2"); 
      dataList.add(data); 
      data = new Data("a3", "b3", "c3"); 
      dataList.add(data); 
      dataList.addToSelection(dataList.get(0)); 

     } catch (Exception e) { 

     } 
    } 

    @Command 
    public void ctrlKeyClick(@org.zkoss.bind.annotation.BindingParam("item") String ctekKeyCode){ 
     int keyCode =Integer.parseInt(ctekKeyCode); 
     String s = ""; 
     switch(keyCode){ 
      case 65: s = "Ctrl+A";break; 
      case 119: s = "F8";break; 
      case 83:s="Ctrl+S";break; 
      case 68:s="Ctrl+D";break; 
     } 
     Messagebox.show(s+" is pressed", "CtrlKey", 
       Messagebox.OK, Messagebox.EXCLAMATION); 
     System.out.println("I am clicked"); 
    } 

    @Command 
    @NotifyChange("dataList") 
    public void addNewItem(){ 
     Data data = new Data("", "", ""); 
     dataList.add(data); 
    } 


    public ListModelList<Data> getDataList() { 
     return dataList; 
    } 

    public void setDataList(ListModelList<Data> dataList) { 
     this.dataList = dataList; 
    } 


    public class Data { 
     String a; 
     String b; 
     String c; 
     public String getA() { 
      return a; 
     } 
     public String getB() { 
      return b; 
     } 
     public String getC() { 
      return c; 
     } 
     public void setA(String a) { 
      this.a = a; 
     } 
     public void setB(String b) { 
      this.b = b; 
     } 
     public void setC(String c) { 
      this.c = c; 
     } 
     public Data(String a, String b, String c) { 
      super(); 
      this.a = a; 
      this.b = b; 
      this.c = c; 
     } 

    } 
} 

在這裏你可以檢查這行代碼..

dataList.addToSelection(dataList.get(0)); 

這將顯示您所選擇的項目

相關問題