ZK,自定义组件。一个值

时间:2017-07-19 07:49:17

标签: java zk

ZK框架。我有自定义组件v_dualListbox.zul:



<hlayout hflex="1">
    <listbox id="candidateLb" hflex="1" vflex="true" multiple="true" rows="8">
        <template name="model">
            <listitem>
                <listcell label="${each.description}"/>
            </listitem>
        </template>
    </listbox>
    <vbox spacing="10px" width="24px">
        <image style="cursor:pointer" id="chooseAllBtn" src="/img/001_25.png"/>
        <image style="cursor:pointer" id="chooseBtn" src="/img/001_25.png"/>
        <image style="cursor:pointer" id="removeBtn" src="/img/001_27.png"/>
        <image style="cursor:pointer" id="removeAllBtn" src="/img/001_27.png"/>
    </vbox>
    <listbox id="chosenLb" hflex="1" vflex="true" multiple="true" rows="8">
        <template name="model">
            <listitem>
                <listcell label="${each.description}"/>
            </listitem>
        </template>
    </listbox> </hlayout>
&#13;
&#13;
&#13;

我在zul页面上使用它:

&#13;
&#13;
<?component name="dual-listbox" extends="div" class="ru.it_constanta.pguAdmin.components.DualListbox"?>
<dual-listbox id="scopeDualLBox" chosenDataList="@bind(vm.orgScopeList)" model="@bind(vm.scopeList)"/>
&#13;
&#13;
&#13;

DualListbox.java:

package ru.it_constanta.pguAdmin.components;

import ...
public class DualListbox<T> extends HtmlMacroComponent implements IdSpace {

private static final long serialVersionUID = 5183321186606483396L;

@Wire
private Listbox candidateLb;
@Wire
private Listbox chosenLb;

private ListModelList<T> candidateModel = new ListModelList<>();
private ListModelList<T> chosenDataModel = new ListModelList<>();

private boolean isLoad = false;

public DualListbox() {
    Executions.createComponents("v_dualListbox.zul", this, null);
    Selectors.wireComponents(this, this, false);
    Selectors.wireEventListeners(this, this);
    chosenLb.setModel(chosenDataModel = new ListModelList<T>());
    chosenDataModel.setMultiple(true);
}

@Listen("onClick = #chooseBtn")
public void chooseItem() {
    Events.postEvent(new ChooseEvent(this, chooseOne()));
}

@Listen("onClick = #removeBtn")
public void unchooseItem() {
    Events.postEvent(new ChooseEvent(this, unchooseOne()));
}

@Listen("onClick = #chooseAllBtn")
public void chooseAllItem() {
    Events.postEvent(new ChooseEvent(this, chooseAll()));
}

@Listen("onClick = #removeAllBtn")
public void unchooseAllItem() {
    Events.postEvent(new ChooseEvent(this, unchooseAll()));
}

/**
 * Set new candidate ListModelList.
 *
 * @param candidate is the data of candidate list model
 */
public void setModel(List<T> candidate) {
    candidateLb.setModel(this.candidateModel = new ListModelList<>(candidate));
    this.candidateModel.setMultiple(true);
    chosenDataModel.clear();
}

@ComponentAnnotation(
        "@ZKBIND(ACCESS=load, LOAD_EVENT=onLoad)")
public void setChosenDataList(List<T> chosen) {
    chosenDataModel.addAll(chosen);
    candidateModel.removeAll(chosen);
}

/**
 * @return current chosen data list
 */
@ComponentAnnotation(
        "@ZKBIND(ACCESS=save, SAVE_EVENT=onChoose)")
public List<T> getChosenDataList() {
    return new ArrayList<>(chosenDataModel);
}

private Set<T> chooseOne() {
    Set<T> set = candidateModel.getSelection();
    chosenDataModel.addAll(set);
    candidateModel.removeAll(set);
    return set;
}

private Set<T> unchooseOne() {
    Set<T> set = chosenDataModel.getSelection();
    candidateModel.addAll(set);
    chosenDataModel.removeAll(set);
    return set;
}

private Set<T> chooseAll() {
    chosenDataModel.addAll(candidateModel);
    candidateModel.clear();
    return chosenDataModel.getSelection();
}

private Set<T> unchooseAll() {
    candidateModel.addAll(chosenDataModel);
    chosenDataModel.clear();
    return candidateModel.getSelection();
}

// Customized Event
public class ChooseEvent extends Event {
    private static final long serialVersionUID = -7334906383953342976L;

    public ChooseEvent(Component target, Set<T> data) {
        super("onChoose", target, data);
    }
}
}

来自数据库的数据。在加载页面上我想放置已经对象的实体(orgScopeList)列表,我想在selectedLb列表框中看到它们。我为此使用了selectedDataList属性。另外我想将所选对象保存在同一个列表中(orgScopeList),i。当客户选择更多实体或取消选择它时,我想将它保存在orgScopeList中,这就是为什么我使用@bind注释(用于加载和保存)。因此,我需要在onLo和onChoose上监听两个事件以获取两个命令(加载和保存)。我用@ComponentAnnotation编写了两个方法,但setChosenDataList不起作用,加载页面没有任何反应,我不知道为什么。 我希望有人理解我:)请帮助!

1 个答案:

答案 0 :(得分:0)

感谢http://sumo.dlr.de/wiki/FAQ#How_do_I_get_high_flows.2Fvehicle_densities.3F的回答,他的两个建议都有效(仅在getter上使用@ComponentAnnotations并将onCreate事件用作LOAD_EVENT),这就是我现在所拥有的我的DualListbox.java:

public void setChosenDataList(List<T> chosen) {
    chosenDataModel.clear();
    chosenDataModel.addAll(chosen);
    candidateModel.removeAll(chosen);
}

@ComponentAnnotation(
        "@ZKBIND(ACCESS=both, SAVE_EVENT=onChoose, LOAD_EVENT=onCreate)")
public List<T> getChosenDataList() {
    return new ArrayList<>(chosenDataModel);
}

但重要的是要记住,LOAD_EVENT也可以在SAVE_EVENT之后工作,所以这就是写chosenDataModel.clear();的原因。