Theme:
Processing...
Description & Source Code

This demo uses a Model to control dynamic data with Grid.
Users can remove specific mail, and the grid will update directly.
Instead of nothing, ZK developer can define a text to show by setting the emptyMessage attribute when no item in the grid.

dynamic_data.zul
<zk>
	<window apply="org.zkoss.bind.BindComposer"
		viewModel="@id('vm') @init('demo.grid.dynamic_data.MailViewModel')">
		<hlayout id="ctlhlayout">
			<button id="revertBtn" width="115px" label="Revert Letters" onClick="@command('revertMail')" />
			<button id="rmAllBtn" width="115px" label="Delete All"
				disabled="@load(empty vm.mailData.mails)" onClick="@command('deleteAllMail')" />
		</hlayout>
		<separator />
		Inbox mails : <label value="@load(vm.mailData.size)" />
		<separator />
		<grid height="350px" model="@load(vm.mailData.mails)"
			emptyMessage="Nothing in Inbox.">
			<columns>
				<column label="Icon" width="50px" />
				<column label="Subject" />
				<column label="Received" width="150px" />
				<column label="Size" width="80px" />
				<column label="Ctrl" width="100px" />
			</columns>
			<template name="model">
				<row>
					<image
						src="/img/Centigrade-Widget-Icons/EnvelopeOpen-16x16.png" />
					<label value="@load(each.subject)" />
					<label value="@load(each.date)" />
					<label value="@load(each.size)" />
					<button label="Delete" onClick="@command('removeMail', mail=each)" />
				</row>
			</template>
		</grid>
		
	</window>
</zk>
MailViewModel.java
package demo.grid.dynamic_data;

import org.zkoss.bind.annotation.BindingParam;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.NotifyChange;

import demo.data.InboxData;
import demo.data.Mail;

public class MailViewModel {
	
	InboxData mailData = new InboxData();
	
	public InboxData getMailData() {
		return mailData;
	}

	@Command
	@NotifyChange("mailData")
	public void revertMail() {
		mailData.revertDeletedMails();
	}
	
	@Command
	@NotifyChange("mailData")
	public void deleteAllMail() {
		mailData.deleteAllMails();
	}
	
	@Command
	@NotifyChange("mailData")
	public void removeMail(@BindingParam("mail") Mail myMail) {
		mailData.deleteMail(myMail);
	}
}
InboxData.java
package demo.data;

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

public class InboxData {
	List<Mail> mails = new ArrayList<Mail>();

	public InboxData() {
		initData();
	}

	private void initData() {
		if (!mails.isEmpty())
			mails.clear();
		mails.add(new Mail(1, "zk Spreadsheet RC Released Check this out", "2010/10/17 20:37:12", ((int) ((int) ((Math.random() * 128) + 1))) ));
		mails.add(new Mail(2, "[zk 5 - Help] RE: SelectedItemConverter Question 3", "2010/10/17 18:31:12", ((int) ((Math.random() * 128) + 1)) ));
		mails.add(new Mail(3, "[zk 5 - Help] RE: SelectedItemConverter Question 2", "2010/10/17 17:30:12", ((int) ((Math.random() * 128) + 1)) ));
		mails.add(new Mail(4, "Times_Series Chart help", "2010/10/17 15:26:37", ((int) ((Math.random() * 128) + 1)) ));
		mails.add(new Mail(5, "RE: Times_Series Chart help", "2010/10/17 14:22:37", ((int) ((Math.random() * 128) + 1)) ));
		mails.add(new Mail(6, "RE: Times_Series Chart help(Updated)", "2010/10/17 13:26:37", ((int) ((Math.random() * 128) + 1)) ));
		mails.add(new Mail(7, "[zk 5 - General] Grid Rendering problem", "2010/10/17 10:41:33", ((int) ((Math.random() * 128) + 1)) ));
		mails.add(new Mail(8, "[zk 5 - Help] RE: SelectedItemConverter Question", "2010/10/17 10:14:27", ((int) ((Math.random() * 128) + 1)) ));
		mails.add(new Mail(9, "[Personal] RE: requirement of new project", "2010/10/16 13:34:37", ((int) ((Math.random() * 128) + 1)) ));		
		mails.add(new Mail(10, "[zk 3 - Feature] Client programming Question", "2010/10/15 04:31:12", ((int) ((Math.random() * 128) + 1)) ));		
		mails.add(new Mail(11, "[zk 5 - Feature] Hlayout/Vlayout Usage", "2010/10/15 04:31:12", ((int) ((Math.random() * 128) + 1)) ));
		mails.add(new Mail(12, "RE: Times_Series Chart help(Updated)", "2010/10/15 03:26:37", ((int) ((Math.random() * 128) + 1)) ));
		mails.add(new Mail(13, "[zk 3 - Feature] JQuery support", "2010/10/14 04:31:12", ((int) ((Math.random() * 128) + 1)) ));
		mails.add(new Mail(14, "[zk 5 - Help] RE: Times_Series Chart help", "2010/10/14 02:43:34", ((int) ((Math.random() * 128) + 1)) ));
		mails.add(new Mail(15, "[Personal] requirement of new project", "2010/10/14 02:44:35", ((int) ((Math.random() * 128) + 1)) ));
		mails.add(new Mail(16, "[zk 5 - Help] RE: SelectedItemConverter Question", "2010/10/13 02:14:27", ((int) ((Math.random() * 128) + 1)) ));		
	}

	public void revertDeletedMails() {
		initData();
	}

	public void deleteAllMails() {
		mails.clear();
	}

	public void addMails(Collection<Mail> c) {
		mails.addAll(c);
	}

	public List<Mail> getMails() {
		return mails;
	}

	public void deleteMail(Mail m) {
		mails.remove(m);
	}
	
	public int getSize(){
		return mails.size();
	}
}
Mail.java
package demo.data;


public class Mail {
	private int index, size;
	private String subject, date;
	
	public Mail(int index, String subject, String date, int size) {
		this.index = index;
		this.subject = subject;
		this.date = date;
		this.size = size;
	}
	
	public int getIndex() {
		return index;
	}
	public void setIndex(int index) {
		this.index = index;
	}
	public int getSize() {
		return size;
	}
	public void setSize(int size) {
		this.size = size;
	}
	public String getSubject() {
		return subject;
	}
	public void setSubject(String subject) {
		this.subject = subject;
	}
	public String getDate() {
		return date;
	}
	public void setDate(String date) {
		this.date = date;
	}
	
	
}