0

How to retain the value of each control in a grid

asked 2009-08-03 14:54:18 +0800

hanghuuhuy gravatar image hanghuuhuy
39 1

Hi,

I use a grid to render a list of criteria with combo-box, textbox, calendar and 2 button "add", "delete" a criteria. The issue, when I delete or add a criteria all the grid will reset and can not retain the selected value.
Here are my codes:

<zk xmlns="http://www.zkoss.org/2005/zul"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.zkoss.org/2005/zul http://www.zkoss.org/2005/zul/zul.xsd"
xmlns:a="http://www.zkoss.org/2005/zk/annotation">

<?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit" arg0="win"?>
<window title="mywindow example" border="normal" width="100%" id="win" use="com.demo.WindowController">
<div>
<listbox id="type" mold="select" model="@{win.typeList}" onSelect="win.typeChange(self.selectedItem.value)"> <!-- mold: select means a dropdown list -->
<listitem self="@{each=type}" label="@{type.name}" value="@{type}"/>
</listbox>

<separator/>

<hbox style="margin-left:10px">
<grid id="meta_data_list" width="100%" model="@{win.criterias}">
<rows id="criteria">
<row self="@{each=criteria}" value="@{criteria}">
<listbox id="meta_data" mold="select" model="@{criteria.metaDataList}" selectedItem="@{criteria.selectedMeta}" onSelect="win.metaDataChange()">
<listitem self="@{each=meta}" label="@{meta.name}" value="@{meta}"/>
</listbox>
<listbox id="operations" mold="select" model="@{criteria.operations}" >
<listitem self="@{each=operation}" label="@{operation}" value="@{operation}"/>
</listbox>
<textbox value="@{criteria.value}" visible="@{criteria.rerenderedText}"/>
<button label="Add" onClick="win.addCriteria(self.parent.value)"/>
<button label="Delete" onClick="win.deleteCriteria(self.parent.value)"/>
</row>
</rows>
</grid>
</hbox>

</div>

</window>
</zk>

Controller

public class WindowController extends Window {
/**
*
*/
private static final long serialVersionUID = 1L;

private List<Type> typeList;

private List<Criteria> criterias;

private AnnotateDataBinder binder;

// initial binding
public void onCreate() {
// get the databinder for later extra load and save
binder = (AnnotateDataBinder) this.getVariable("binder", false);

// save all controls and binding
binder.saveAll();

createDB();

// refresh the screens
binder.loadAll();
}

public List<Criteria> getCriterias() {
return criterias;
}

public void setCriterias(List<Criteria> criterias) {
this.criterias = criterias;
}

public List<Type> getTypeList() {
return typeList;
}

public void setTypeList(List<Type> typeList) {
this.typeList = typeList;
}

/**
* @param typeList
*/
private void createDB() {
typeList = new ArrayList<Type>();

Type type1 = new Type("0", "Project", "Project");
Type type2 = new Type("1", "Tasks", "Tasks");

MetaData data1 = new MetaData("0", "name", "name", "text");
MetaData data2 = new MetaData("1", "description", "description", "date");
MetaData data3 = new MetaData("2", "Date create", "Date create", "combo");

List<MetaData> metaDataList1 = new ArrayList<MetaData>();
metaDataList1.add(data1);
metaDataList1.add(data2);
type1.setMetaDataList(metaDataList1);

List<MetaData> metaDataList2 = new ArrayList<MetaData>();
metaDataList2.add(data1);
metaDataList2.add(data3);
type2.setMetaDataList(metaDataList2);

typeList.add(type1);
typeList.add(type2);

List<MetaData> metaDataList = typeList.get(0).getMetaDataList();

criterias = new ArrayList<Criteria>();
Criteria criteria = new Criteria();

criteria.setMetaDataList(metaDataList);
criteria.setOperations(Constants.getOperations());
criteria.setValue("");

criterias.add(criteria);

// refresh the screens
binder.loadAll();
}

public void typeChange(Type type) {

Grid gridCtl = (Grid) getFellow("meta_data_list");

// update for criteria grid only
binder.saveComponent(gridCtl);

// clear all criterias
criterias = new ArrayList<Criteria>();

List<MetaData> metaDataList = type.getMetaDataList();

Criteria criteria = new Criteria();
criteria.setMetaDataList(metaDataList);
criteria.setOperations(Constants.getOperations());
criteria.setValue("");

criterias.add(criteria);

// reload for criteria grid only
binder.loadComponent(gridCtl);

}

public void addCriteria(Criteria criteria) {



Grid gridCtl = (Grid) getFellow("meta_data_list");

//Rows rows = (Rows) getFellow("criteria");
// update for criteria gridCtl only
//binder.saveComponent(rows);

int k = 0;
for (int i = 0; i < criterias.size(); i++) {
Criteria personCmp = (Criteria) criterias.get(i);

if (personCmp.equals(criteria)) {
k = i;
break;
}
}

criterias.add(k+1, criteria);

//copy UI components values to data bean properties in one method call.
//binder.saveAll();

//load an UI component value from data bean property in one method call.
//binder.loadComponent(rows);

//gridCtl.setModel(model);
// reload for criteria gridCtl only
binder.loadComponent(gridCtl);
}

public void deleteCriteria(Criteria criteria) {

Grid gridCtl = (Grid) getFellow("meta_data_list");

// update for criteria gridCtl only
binder.saveComponent(gridCtl);

criterias.remove(criteria);

//copy UI components values to data bean properties in one method call.
//binder.saveAll();

//load an UI component value from data bean property in one method call.
binder.loadComponent(gridCtl);
}

public void metaDataChange() {
//Listbox listbox = (Listbox) getFellow("meta_data");
//binder.saveAll();
// update for criteria gridCtl only
//binder.saveComponent(listitem);

// if (metaData.getWidgetType().equals("text")) {
//
// }

//load an UI component value from data bean property in one method call.
//binder.loadComponent(listitem);
Rows rows = (Rows) getFellow("criteria");
binder.saveComponent(rows);

}
}

delete flag offensive retag edit

1 Reply

Sort by ยป oldest newest

answered 2011-10-11 15:04:57 +0800

gyowanny gravatar image gyowanny
283 1 2 6

Maybe you could store the values in the Executions or Session objects and then get them back after reseting the grid.

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2009-08-03 14:54:18 +0800

Seen: 686 times

Last updated: Oct 11 '11

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More