无法在zk中的listbox mold =“select”中设置所选值

时间:2013-02-05 10:33:22

标签: java gwt extjs zk

我正在使用zk 6.5并遵循MVVM模式,我正面临着使用问题 listbox mold =“select”我从数据库中获取数据并希望像--select--作为我列表框中的第一个选择选项,但我无法做到,这里是我的代码

这是我的.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));
}

1 个答案:

答案 0 :(得分: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));

这将显示所选项目