2

how to dynamic binding with java code? [closed]

asked 2012-12-04 07:48:15 +0800

sjoshi gravatar image sjoshi flag of India
3493 1 8
http://zkframeworkhint.bl...

I am using

@bind

in My ZUl page for Listcell value but now i am using Java code to rendered the Listbox by getItemRendered Method now anyone know if user edit any textbox inside listcell how can we bind this thing if we are using java code.

delete flag offensive retag edit

The question has been closed for the following reason "too localized" by sjoshi
close date 2013-02-08 07:24:18

39 Replies

Sort by ยป oldest newest

answered 2012-12-04 14:33:36 +0800

cyiannoulis gravatar image cyiannoulis
1201 10

I have implemented something similar in the past using the org.zkoss.zkplus.databind.AnnotateDataBinderclass. First i instantiate manually the binder inside the doAfterCompose() method. Then, i am using a custom RowRenderer to render the Grid. The CustomRenderer is an inner-class so it has access directly to the composer's 'binder' property

public class CustomRenderer implements RowRenderer {
		
		@Override
		public void render(Row row, Object data, int index) throws Exception {
			
			Employee bean = (Employee) data;

			final String beanid = row.getUuid();
			binder.bindBean(beanid, employee);
			
			final Intbox txtCode = new Intbox();
			row.appendChild( txtCode );			
			binder.addBinding(txtCode, "value", beanid + ".code", new String[] {}, "none", "both", null);
			
			final Label lblLastname = new Label();
			row.appendChild( lblLastname );
		        binder.addBinding(lblLastname, "value", beanid + ".lastName");

			final Intbox txtAdvance = new Intbox();
			row.appendChild( txtAdvance );			
                        binder.addBinding(txtAdvance, "value", beanid + ".advanceValue", new String[] {}, "none", "both", null);
              
            ................
            ................
            ................

Then, in the onCreate() composer's method a "binder.loadAll()" should do the trick. I haven't tried to do the same using a SelectorComposer and the new binder but normally it should work if you try to use the addPropertyLoadBindings() & addPropertySaveBindings() methods.

/costas

link publish delete flag offensive edit

answered 2012-12-04 16:21:44 +0800

sjoshi gravatar image sjoshi flag of India
3493 1 8
http://zkframeworkhint.bl...

Thanks For your reply and time. Can you please let me know where you initialize

binder

Object is this object define in class level?

link publish delete flag offensive edit

answered 2012-12-04 16:55:57 +0800

cyiannoulis gravatar image cyiannoulis
1201 10

Yeap. The binder is an instance of org.zkoss.zkplus.databind.AnnotateDataBinder initialized inside the doAfterCompose() method

          . . . . . . . . . 
          private AnnotateDataBinder binder;
          . . . . . . . . . 
          . . . . . . . . . 
	@Override
	public void doAfterCompose(Component comp) throws Exception {
		
		try {
			super.doAfterCompose(comp);
		} 
		catch (Exception e) {
			e.printStackTrace();
		}

		binder = new AnnotateDataBinder(comp);
                . . . . . . . . . 
                . . . . . . . . . 

/costas

link publish delete flag offensive edit

answered 2012-12-04 17:27:30 +0800

sjoshi gravatar image sjoshi flag of India
3493 1 8
http://zkframeworkhint.bl...

Thanks for your reply let me try it in Code will ask you if i have any issue.Do you have any idea of how can we fix the position of ListCell in Listbox

link publish delete flag offensive edit

answered 2012-12-04 17:57:47 +0800

cyiannoulis gravatar image cyiannoulis
1201 10

I don't know if i get your question but supposing that the total number of columns is known then if you are using a custom renderer it should be quite easy. Something like the following example may help you:

	public void render(Row row, Employee emp, int index) {
		/*
		 * Decide the position (column number) of the decimal box depending on the employee's rank
		 */
		if (emp.rank == 1) {	
			Div div = new Div();
			row.appendChild( div );
			Decimalbox txtSalaryAmount = new Decimalbox();
			row.appendChild(txtSalaryAmount);
		}
		else
		if (emp.rank == 4) {	
			/* add 3 dummy labels first */
			row.appendChild(new Label(""));
			row.appendChild(new Label(""));
			row.appendChild(new Label(""));
			
			Div div = new Div();
			row.appendChild( div );
			Decimalbox txtSalaryAmount = new Decimalbox();
			row.appendChild(txtSalaryAmount);
		}
    }

/costas

link publish delete flag offensive edit

answered 2012-12-04 21:12:36 +0800

terrytornado gravatar image terrytornado flag of Germany
9393 3 7 16
http://www.oxitec.de/

Here is sample code:

best
Stephan

link publish delete flag offensive edit

answered 2012-12-05 02:38:53 +0800

sjoshi gravatar image sjoshi flag of India
3493 1 8
http://zkframeworkhint.bl...

Thanks costas for your reply let me explain my problem in detail.
Hey Stephen ,
After long time i saw your answer in my post thanks for that, My main problem is that i am creating Listbox from java code by getItemRendered() and user can do reorder on this listbox means user can choose header name getitemrendered will rendered only those header which is chosen by user and other will not display to user also header position can change so data will rendered according to them,
But if some other developer wants to implement reordering in his/her Listbox he/she have to create listbox from the JavaCode not from the ZUL file because in Zul file you can not change the listcell position according to header position
So every developer telling that its wasting there time to implement the reordering in their code .also CRUD operation have issue because now they can not use

@bind

If you understand my problem . Can you suggest some solution? So that i can make developer life easy?
Thanks

link publish delete flag offensive edit

answered 2012-12-05 10:26:45 +0800

terrytornado gravatar image terrytornado flag of Germany
9393 3 7 16
http://www.oxitec.de/

updated 2012-12-05 10:30:24 +0800

Working with Annotations divide the guys in two separate camps. On loves it and the other hates it. There are many articles about this theme in the web.

For dynamially working you can do it easier without using Annotations.:

all times you have changed the data call the binding new:

		
binder.bindBean("person", person);
binder.bindBean("persons", persons);
binder.loadAll();


init:

		
binder.bindBean("person", person);
binder.bindBean("persons", persons);
binder.addBinding(listBoxPerson, "model", "persons");
binder.addBinding(listBoxPerson, "selectedItem", "person");

binder.loadAll();

The more codeWork is for reordering what listHeaders/listItems the user selected.

Thread

hope this helps
Stephan

link publish delete flag offensive edit

answered 2012-12-05 11:06:25 +0800

sjoshi gravatar image sjoshi flag of India
3493 1 8
http://zkframeworkhint.bl...

updated 2012-12-05 11:36:40 +0800

all times you have changed the data call the binding new:
Do you mean i have to fire a event onChange when user changing any cell value?

And in my case user can change multiple cell or row and when he /she will click on save data will saved the database.


Thanks Setphen I followed this Thread to create reordering Component . Do you have any demo example link where someone uses the Binder in java class and if someone changing the value developer easily get the value .Righ now i have t do this to get change value in Java Code.

Listbox box = (Listbox) comp.getFellow("taskListWindow").getFellow("taskList");
		 
		List<Listitem> item = box.getItems();
		ListModel<Object> model = box.getModel();
		for (Listitem listitem : item) {

			List<Component> com = listitem.getChildren();
			for (Component compCell : com) {
				Listcell list = (Listcell) compCell;
				if (list.getFirstChild() instanceof Intbox) {
					Intbox processPriorityBox = (Intbox) list.getFirstChild();
					 ---///Some other code here
				}

			}

link publish delete flag offensive edit

answered 2012-12-07 10:58:03 +0800

dennis gravatar image dennis
3679 1 6
http://www.javaworld.com....

you can also do this by add annotation to component and implement template, then let default binder handle it , just like zul. here is my simple example.

<window apply="org.zkoss.bind.BindComposer" width="600px"
	viewModel="@id('vm') @init('org.zkoss.zktest.bind.adv.DynamicCol')">
	<hlayout width="100%">
		<vlayout width="500px">
			<radiogroup>
				<radio label="case 1"
					onCheck="@command('doCase',case = 1)" />
				<radio label="case 2"
					onCheck="@command('doCase',case = 2)" />
				<radio label="case 3"
					onCheck="@command('doCase',case = 3)" />
			</radiogroup>

			<listbox model="@bind(vm.items) @template(vm.template)"
				selectedItem="@bind(vm.selected)" rows="10">
				<listhead children="@bind(vm.columns)">
					<template name="children">
						<listheader label="@bind(each.header)" />
					</template>
				</listhead>
			</listbox>
		</vlayout>
		<vlayout>
			<label value="@bind(vm.selected.value1)" />
			<label value="@bind(vm.selected.value2)" />
			<label value="@bind(vm.selected.value3)" />
		</vlayout>
	</hlayout>
</window>

package org.zkoss.zktest.bind.adv;

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

import org.zkoss.bind.annotation.BindingParam;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.zk.ui.util.Template;

public class DynamicCol {
	List<Item> items;
	List<ColMeta> columns;
	Item selected;

	Template template;
	
	public DynamicCol(){
		items = new ArrayList<Item>();
		for(int i=0;i<100;i++){
			items.add(new Item("item"+i));
		}
		columns = new ArrayList<ColMeta>();
		columns.add(ColMeta.field1);
		columns.add(ColMeta.field2);
		columns.add(ColMeta.field3);
		template = ColMeta.newTemplate(columns);
	}
	
	public Template getTemplate(){
		return template;
	}

	public List<Item> getItems() {
		return items;
	}

	public Item getSelected() {
		return selected;
	}

	public void setSelected(Item selected) {
		this.selected = selected;
	}

	public void setItems(List<Item> items) {
		this.items = items;
	}

	public List<ColMeta> getColumns() {
		return columns;
	}
	
	
	@Command @NotifyChange({"template","columns"})
	public void doCase(@BindingParam("case") int theCase){
		columns.clear();
		switch(theCase){
		case 2:
			columns.add(ColMeta.field3);
			columns.add(ColMeta.field1);
			columns.add(ColMeta.field2);
			break;
		case 3:
			columns.add(ColMeta.field2);
			columns.add(ColMeta.field1);
			break;
		default:
			columns.add(ColMeta.field3);
			columns.add(ColMeta.field2);
			columns.add(ColMeta.field1);
			break;
		}
		template = ColMeta.newTemplate(columns);
	}

	public static class Item{
		String value1;
		String value2;
		Integer value3;
		
		public Item(String prefix){
			value1 = prefix+"_1";
			value2 = prefix+"_2";
			value3 = 99;
		}

		public String getValue1() {
			return value1;
		}

		public void setValue1(String value1) {
			this.value1 = value1;
		}

		public String getValue2() {
			return value2;
		}

		public void setValue2(String value2) {
			this.value2 = value2;
		}

		public Integer getValue3() {
			return value3;
		}

		public void setValue3(Integer value3) {
			this.value3 = value3;
		}
	}
	
	
}

package org.zkoss.zktest.bind.adv;

import java.util.List;

import org.zkoss.zk.ui.util.Template;

public class ColMeta {
	
	
	public static final ColMeta field1 = new ColMeta("Col 1","value1");
	public static final ColMeta field2 = new ColMeta("Col 2","value2");
	public static final ColMeta field3 = new ColMeta("Col 3","value3");
	
	public static Template newTemplate(List<ColMeta> columns){
		return new ColMetaTemplate(columns);
	}
	
	String header;
	String fileName;

	public ColMeta(String header, String fieldName) {
		this.header = header;
		this.fileName = fieldName;
	}

	public String getHeader() {
		return header;
	}

	public String getFiledName() {
		return fileName;
	}
	
}

package org.zkoss.zktest.bind.adv;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.zkoss.xel.VariableResolver;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.util.Composer;
import org.zkoss.zk.ui.util.Template;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;

public class ColMetaTemplate implements Template {

	List<ColMeta> columns;
	Map parameters = new HashMap();
	
	public ColMetaTemplate(List<ColMeta> columns){
		this.columns = columns;
	}
	
	@Override
	public Component[] create(Component parent, Component insertBefore, VariableResolver resolver, Composer composer) {
		Listitem listitem = new Listitem();
		for(ColMeta col:columns){
			Listcell cell = new Listcell();
			//set annotation
			Map anno = new HashMap();
			anno.put("value", "each."+col.getFiledName());
			cell.addAnnotation("label", "bind", anno);
			listitem.appendChild(cell);
		}
		parent.insertBefore(listitem, insertBefore);
		return new Component[]{listitem};
	}

	@Override
	public Map<String, Object> getParameters() {
		return parameters;
	}

}

link publish delete flag offensive edit

Question tools

Follow

RSS

Stats

Asked: 2012-12-04 07:48:15 +0800

Seen: 761 times

Last updated: Jan 19 '13

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