gpt4 book ai didi

java - zk - 是否可以为模型中的每条记录添加绑定(bind)?

转载 作者:行者123 更新时间:2023-11-30 11:24:20 26 4
gpt4 key购买 nike

<zk>
<grid width="auto" sizedByContent="true" span="1" sclass="tblWithoutHover">
<attribute name="onCreate">
authorPublisherEtcInclude.insertBefore(self, authorBox);
</attribute>
<columns>
<column/>
<column/>
</columns>
<rows>
<row valign="center">
<cell colspan="2"><label use="${authorPublisherComponents.originalFieldsLabel}"/></cell>
</row>
<row valign="center">
<label use="${authorPublisherComponents.titleAuthorOriginalLabel}"/>
<textbox use="${authorPublisherComponents.titleAuthorOriginalTextbox}"/>
</row>
<row valign="center">
<label use="${authorPublisherComponents.mainAuthorOriginalLabel}"/>
<textbox use="${authorPublisherComponents.mainAuthorOriginalTextbox}"/>
</row>
<row valign="center">
<label use="${authorPublisherComponents.mainAuthorResponsibilityLabel}"/>
<selectbox use="${authorPublisherComponents.mainAuthorResponsibilitySelectbox}"/>
</row>
<row valign="center">
<label use="${authorPublisherComponents.authorityDatesOriginalLabel}"/>
<textbox use="${authorPublisherComponents.authorityDatesOriginalTextbox}"/>
</row>
<row valign="center">
<cell>
<label use="${authorPublisherComponents.addMainAuthorsOriginalLabel}"/>
<toolbarbutton use="${authorPublisherComponents.addAuthorButton}"/>
</cell>
<cell id="addAuthorsCell">
<grid id="addAuthorsContainer" model="@bind(ivm.inventory.addAuthorsBeans)">
<columns>
<column/>
<column/>
<column/>
</columns>
<rows>
<row>
<textbox value="@load(xgbfxb.authorName)" onChange="@command('test', component = self, index=s.index)"/>
<button label="Del" onClick="@command('delAuthor', container=addAuthorsContainer, index=modelIndex )">
<custom-attributes modelIndex="${s.index}"/>
</button>
</row>
</rows>
</grid>
<textbox use="${authorPublisherComponents.addMainAuthorsOriginalTextbox}"/>
</cell>
</row>

这是我的 zul 页面的一部分。 addAuthorsBeans 是带有字段的类列表。当我更改组合框中的数据时,应用程序会为列表中的所有类调用 set 方法,但我希望它只调用相应的项目。是否可以?或者我应该使用 onChange 事件和 ViewModel 方法施展黑魔法吗?

编辑(21/12/2013)。它是这样工作的:我有三个项目 1、2、3。然后我为 2 个项目激活 setAuthor。然后应用程序为 2 个项目调用 setAuthor 方法,然后为 3 个项目,然后为 1 个项目,然后在容器中查找此方法。

我有“黑魔法”临时解决方案来创建静态变量并在第一次调用 setAuthor 方法时更改它并在容器中取消阻止它。

但这不是最终的解决方案,因为它会消耗更多的资源,而且实际上并不是它应该如何工作。

解决方案:如果您在另一个网格中有模型的网格,它会表现得很奇怪。所以只需使用列表框即可。

最佳答案

combobox 只会触发代表项目的对象的 item.setAuthorName(...)。如果调用了其他 setter ,则必须在链接到此的 ViewModel 类中具有 @NotifyChange@DependsOn

编辑:尝试改变:

<grid model="@bind(ivm.inventory.addAuthorsBeans)">

<grid model="@load(ivm.inventory.addAuthorsBeans)">

更新:我为这个主题创建的一个工作示例:http://forum.zkoss.org/question/90188/notifychange-to-grid-that-is-inside-a-row-of-another-grid/?answer=90284#post-id-90284 ):

第一个简单的 pojo 类:

package be.chillworld;

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

/**
*
* @author chillworld
*/
public class Person {
private int id;
private String naam;
private List<Person> childs = new ArrayList<Person>();

public Person(int id) {
this.id = id;
naam = "test " + id;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public void setNaam(String naam) {
this.naam = naam;
}

public String getNaam() {
System.out.println("asked getter (naam) of "+ id);
return naam;
}

public List<Person> getChilds() {
System.out.println("asked getter (childs) of "+ id);
return childs;
}

public void setChilds(List<Person> childs) {
this.childs = childs;
}

public boolean addChild(Person person) {
return childs.add(person);
}

@Override
public String toString() {
return "Person{" + "id=" + id + ", name=" + getNaam() + '}';
}
}

然后是 IndexVM:

package be.chillworld;

import java.util.ArrayList;
import java.util.List;
import org.zkoss.bind.BindUtils;
import org.zkoss.bind.annotation.BindingParam;
import org.zkoss.bind.annotation.Command;

/**
*
* @author chillworld
*/
public class IndexVm {

private List<Person> persons;
int i;

public IndexVm() {
System.out.println("starting creating list");
persons = new ArrayList<Person>();
for (i = 0; i < 100; i++) {
Person person = new Person(i);
person.addChild(new Person(++i));
persons.add(person);
}
System.out.println("ending creating list");

}

public List<Person> getPersons() {
return persons;
}

public void setPersons(List<Person> persons) {
this.persons = persons;
}

@Command
public void showIndex(@BindingParam("person") Person person) {
System.out.println("changed name");
person.setNaam("Chillworld");
BindUtils.postNotifyChange(null, null, person, "naam");
}

@Command
public void addChild(@BindingParam("person") Person person) {
System.out.println("add child");
Person child = new Person(++i);
child.setNaam("new child");
person.addChild(child);
BindUtils.postNotifyChange(null, null, person, "childs");
}
}

最后是 index.zul :

<?xml version="1.0" encoding="UTF-8"?>
<zk xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.zkoss.org/2005/zul"
xsi:schemaLocation="http://www.zkoss.org/2005/zul
http://www.zkoss.org/2005/zul/zul.xsd">
<window border="normal" closable="false"
apply="org.zkoss.bind.BindComposer"
viewModel="@id('vm') @init('be.chillworld.IndexVm')">
<grid width="1000px" model="@load(vm.persons)">
<columns>
<column label="naam" />
<column label="add child" />
<column label="childs" />
</columns>
<template name="model" >
<row>
<textbox value="@bind(each.naam)" />
<button onClick="@command('addChild',person = each)" label="add child"/>
<grid width="400px" model="@load(each.childs)">
<columns>
<column label="naam" />
<column label="button" />
</columns>
<template name="model" var="item">
<row>
<textbox value="@bind(item.naam)" />
<button onClick="@command('showIndex',person = item)" label="change value"/>
</row>
</template>
</grid>
</row>
</template>
</grid>
</window>
</zk>

这给出了输出(启动后):

changed name
asked getter of 11
changed name
asked getter of 7
changed name
asked getter of 19

问候寒意。

关于java - zk - 是否可以为模型中的每条记录添加绑定(bind)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20704519/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com