Processing...
Description & Source Code
  • Description
  • View
  • Controller
    DynamicRendererController.java
    SimpleRenderer.java
  • Model
    ContributorData.java
    Contributor.java
    LanguageContribution.java

Rows in grid can be populated with data dynamically by associating the View (grid) with a Model and a Renderer.
A model is typically a collection of data beans;

We devise a renderer that assigns values of bean properties to the UI component, in this case, rows in a grid.
The data displayed in grid is updated dynamically when the data model is changed.

dynamic_renderer.zul
<div apply="demo.grid.dynamic_renderer.DynamicRendererController">
	<style src="/widgets/grid/dynamic_renderer/style.css" />
	<grid model="${$composer.contributors}" 
		rowRenderer="demo.grid.dynamic_renderer.SimpleRenderer"
		sclass="narrow" height="400px">
		<auxhead>
			<auxheader label="Contributor of ZK International Message(Part)" colspan="5" />
		</auxhead>
		<columns>
			<column width="5%">Id</column>
			<column width="30%">Language</column>
			<column width="40%">Contributor</column>
			<column width="10%">Charset</column>
			<column width="15%">Like</column>
		</columns>
	</grid>
</div>
DynamicRendererController.java
package demo.grid.dynamic_renderer;

import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zul.ListModel;
import org.zkoss.zul.ListModelList;

import demo.data.ContributorData;
import demo.data.pojo.LanguageContribution;

public class DynamicRendererController extends SelectorComposer<Component> {
	private static final long serialVersionUID = 1L;
	
	private final ListModel<LanguageContribution> langContributors = 
			new ListModelList<LanguageContribution>(new ContributorData().getLanguageContributors());

	public ListModel<LanguageContribution> getContributors() {
		return langContributors;
	}
}
SimpleRenderer.java
package demo.grid.dynamic_renderer;

import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Button;
import org.zkoss.zul.Div;
import org.zkoss.zul.Label;
import org.zkoss.zul.Row;
import org.zkoss.zul.RowRenderer;

import demo.data.pojo.LanguageContribution;

public class SimpleRenderer implements RowRenderer<LanguageContribution> {

	public void render(Row row, LanguageContribution data, int index) {
		// the data append to each row with simple label
		row.appendChild(new Label(Integer.toString(index)));
		row.appendChild(new Label(data.getLanguage()));
		row.appendChild(new Label(data.getName()));
		row.appendChild(new Label(data.getCharset()));
		// we create a thumb up/down comment to each row
		final Div d = new Div();
		final Button thumbBtn = new Button(null, "/images/thumb-up.png");
		thumbBtn.setParent(d);
		thumbBtn.addEventListener(Events.ON_CLICK, new EventListener<Event>() {
			public void onEvent(Event event) throws Exception {
				d.appendChild(new Label("Thumbs up"));
				thumbBtn.setDisabled(true);
			}
		});
		row.appendChild(d); // any component could created as a child of grid
	}
}
ContributorData.java
package demo.data;

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

import demo.data.pojo.Contributor;
import demo.data.pojo.LanguageContribution;

public class ContributorData {

	private List<String> titles = new ArrayList<String>();
	private List<Contributor> contributors = new ArrayList<Contributor>();
	private List<LanguageContribution> languageContributions = new ArrayList<LanguageContribution>();

	public ContributorData() {
		titles.add("Code");
		titles.add("Bug");
		titles.add("Docs");
		titles.add("Arts");

		contributors.add(new Contributor("Kaleb", "Leonel", titles.get(0).toString(), 321));
		contributors.add(new Contributor("Balu", "Haben", titles.get(0).toString(), 321));
		contributors.add(new Contributor("Trey", "Wyatt", titles.get(0).toString(), 323));
		contributors.add(new Contributor("Balu", "Chen", titles.get(0).toString(), 324));
		contributors.add(new Contributor("Terry", "Tornado", titles.get(0).toString(), 711));
		contributors.add(new Contributor("Jesse", "Miles", titles.get(1).toString(), 712));
		contributors.add(new Contributor("Sadira", "Jobs", titles.get(1).toString(), 713));
		contributors.add(new Contributor("Jaquan", "Frederick", titles.get(2).toString(), 451));
		contributors.add(new Contributor("Avery", "Katrina", titles.get(2).toString(), 453));
		contributors.add(new Contributor("Heidi", "Nikolas", titles.get(2).toString(), 455));
		contributors.add(new Contributor("Katelyn", "Clara", titles.get(2).toString(), 457));
		contributors.add(new Contributor("Branden", "Shane", titles.get(2).toString(), 459));
		contributors.add(new Contributor("Dacey", "Obert", titles.get(2).toString(), 450));
		contributors.add(new Contributor("Julianna", "Allison", titles.get(3).toString(), 643));
		contributors.add(new Contributor("Rachel", "Elisabeth", titles.get(3).toString(), 644));
		contributors.add(new Contributor("Clarissa", "Francesca", titles.get(3).toString(), 645));
		contributors.add(new Contributor("Gabby", "Taffy", titles.get(3).toString(), 646));
		
		languageContributions.add(new LanguageContribution("Arabic (ar)", "Ayman Elgharabawy", "iso-8859-6"));
		languageContributions.add(new LanguageContribution("Hungarian (hu)", "Andreas Klein", "iso-8859-2"));
		languageContributions.add(new LanguageContribution("Italian (it)", "Matteo Barbieri", "iso-8859-1"));
		languageContributions.add(new LanguageContribution("Indonesian (id)", "James Liam Supangkat", "iso-8859-1"));
		languageContributions.add(new LanguageContribution("Japanese (ja)", "Poli Lee", " iso-2022-jp"));
		languageContributions.add(new LanguageContribution("Korean (ko)", "Deok-su Lee", "euc-kr"));
		languageContributions.add(new LanguageContribution("Dutch (nl)", "Wido Jansen", "iso-8859-1"));
		languageContributions.add(new LanguageContribution("Portuguese (pt)", "Airton Carrara", "iso-8859-1"));
		languageContributions.add(new LanguageContribution("Brazilian Portugese (pt_BR)", "Airton Carrara", "iso-8859-1"));
		languageContributions.add(new LanguageContribution("Romanian (ro)", "Calin Vaida", "iso-8859-2"));
		languageContributions.add(new LanguageContribution("Russian (ru)", "Denis Yarkovoy", "iso-8859-1"));
		languageContributions.add(new LanguageContribution("French (fr)", "Jêrôme Vergereau", "iso-8859-1"));
		languageContributions.add(new LanguageContribution("Spanish (es)", "Daniel Octavio Ruiz Rodriguez", "iso-8859-1"));
		languageContributions.add(new LanguageContribution("German (de)", "H.-Dirk Schmitt", "iso-8859-1"));
		languageContributions.add(new LanguageContribution("Czech (cs)", "Pavel Mica", "iso-8859-2"));
		languageContributions.add(new LanguageContribution("Catalan (ca)", "Xavier Covas O'Ryan", "iso-8859-1"));
		languageContributions.add(new LanguageContribution("Bulgarian (bg) ", "Boril Yonchev", "iso-8859-5"));
		languageContributions.add(new LanguageContribution("Ukrainian (uk)", "Denis Yarkovoy", "iso-8859-5"));
		languageContributions.add(new LanguageContribution("Turkish (tr)", "Kursat Kurt", "iso-8859-9"));
		languageContributions.add(new LanguageContribution("Swedish (sv)", "Easit AB", "iso-8859-1"));
		languageContributions.add(new LanguageContribution("Slovenian (sl) ", "Ziga", "iso-8859-2"));
	}

	public List<String> getTitles() {
		return titles;
	}

	public List<Contributor> getContributors() {
		return contributors;
	}

	public List<LanguageContribution> getLanguageContributors() {
		return languageContributions;
	}

}
Contributor.java
package demo.data.pojo;

import org.zkoss.bind.annotation.DependsOn;

public class Contributor {
	private String title;
	private String firstName;
	private String lastName;
	private Integer extension;

	public Contributor(String firstName, String lastName, String title, Integer ext) {
		setFirstName(firstName);
		setLastName(lastName);
		setTitle(title);
		setExtension(ext);
	}

	public Contributor() {
		setFirstName("");
		setLastName("");
		setTitle("Code");
		setExtension(12345);
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String fn) {
		firstName = fn;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String ln) {
		lastName = ln;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String tl) {
		title = tl;
	}

	public Integer getExtension() {
		return extension;
	}

	public void setExtension(Integer ext) {
		extension = ext;
	}

	@DependsOn({ "firstName", "lastName" })
	public String getFullName() {
		return getFirstName() + " " + getLastName();
	}
}
LanguageContribution.java
package demo.data.pojo;

public class LanguageContribution {
	private String language, name, charset;

	public LanguageContribution(String language, String name, String charset) {
		this.language = language;
		this.name = name;
		this.charset = charset;
	}
	
	public String getLanguage() {
		return language;
	}

	public void setLanguage(String language) {
		this.language = language;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getCharset() {
		return charset;
	}

	public void setCharset(String charset) {
		this.charset = charset;
	}
}