0

Want to change the background color of some listitems based on some condition

asked 2012-06-09 09:32:31 +0800

Senthilchettyin gravatar image Senthilchettyin flag of India
2623 3 8
http://emrpms.blogspot.in...

Hi

I want to change the background color of the listitems based on condition. After looking the demo, i came to know this can be done if we our own render er.

I have created the sample, but it is working properly. Here is the code.

Zul File

<?xml version="1.0" encoding="UTF-8"?>
<?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit" ?>
<zk>
<style>
.red {
	background-color:red;
}
</style>
	<window id='myWin' title="MVC example" border="normal"
		apply="mydomainUI.ListItemCondCSS">
		Example ListItem Conditional CSS Setting for some particular Rows
		<listbox id="listUsers"
			model="@{myWin$ListItemCondCSS.users}">
			<listhead sizable="true">
				<listheader label="User Code" width="100px" />
				<listheader label="User Name" width="285px" />
				<listheader label="User Password" width="285px" />
				<listheader label="Active" width="285px" />
			</listhead>
			<listitem self="@{each=p1}" >
				<listcell label="@{p1.userCode}" />
				<listcell label="@{p1.userName}" />
				<listcell label="@{p1.userPassword}" />
				<listcell label="@{p1.active}" />
			</listitem>
		</listbox>
	</window>
</zk>

MVC Controller

package mydomainUI;

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


import mydomain.users;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.sys.ComponentsCtrl;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.ListitemRenderer;
import org.zkoss.zul.Messagebox;

@SuppressWarnings({ "rawtypes", "serial" })
public class ListItemCondCSS extends GenericForwardComposer {

	private List<users> usersAll = new ArrayList<users>();
	private Listbox listUsers;

	@SuppressWarnings("unchecked")
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);

		users u1 = new users();
		u1.setUserName("John");
		u1.setUserCode("User101");
		u1.setUserPassword("xxxxx");
		u1.setActive("Y");
		usersAll.add(u1);

		u1 = new users();
		u1.setUserName("Robert");
		u1.setUserCode("User102");
		u1.setUserPassword("xxxxx");
		u1.setActive("Y");
		usersAll.add(u1);
		

		u1 = new users();
		u1.setUserName("Sean");
		u1.setUserCode("User103");
		u1.setUserPassword("xxxxx");
		u1.setActive("N");
		usersAll.add(u1);

		u1 = new users();
		u1.setUserName("Marry");
		u1.setUserCode("User104");
		u1.setUserPassword("xxxxx");
		u1.setActive("N");
		usersAll.add(u1);

		listUsers.setItemRenderer(new ListitemRenderer() {
			@Override
			public void render(Listitem item, Object arg1, int arg2)
					throws Exception {
				users value = (users) arg1;
				item.appendChild(new Listcell(value.getUserCode()));
				item.appendChild(new Listcell(value.getUserName()));
				item.appendChild(new Listcell(value.getUserPassword()));
				item.appendChild(new Listcell(value.getActive()));
				item.setValue(value);
				if (value.getActive()=="N")
				{
					Messagebox.show("inside");
					item.setSclass("red");
				}
				
			}
		});
	}

	
	public List<users> getUsers() {
		return usersAll;
	}

}

delete flag offensive retag edit

2 Replies

Sort by ยป oldest newest

answered 2012-06-09 11:10:58 +0800

Senthilchettyin gravatar image Senthilchettyin flag of India
2623 3 8
http://emrpms.blogspot.in...

Hi

Again, i am back. I have achieved the above by setting up the style for individual cell, instead of trying to apply at the list item level.
Is there any way, we can set by listitem itself. You can see my example here

link publish delete flag offensive edit

answered 2012-07-13 06:51:56 +0800

paowang gravatar image paowang
140 6

I write an example with ZK 6.0.1, check it:

<zk>
	<style>
	.myColor { 
		color: red; 
	} 
	div.z-listcell-cnt { 
		color: inherit; 
	}
	</style>
	<listbox id="listUsers">
		<listhead sizable="true">
			<listheader label="User Code" width="100px" />
			<listheader label="User Name" width="285px" />
			<listheader label="User Password" width="285px" />
			<listheader label="Active" width="285px" />
		</listhead>
		<listitem>
			<listcell label="user0" />
			<listcell label="John" />
			<listcell label="*****" />
			<listcell label="N" />
		</listitem>
		<listitem zclass="myColor">
			<listcell label="user1" />
			<listcell label="Mary" />
			<listcell label="*****" />
			<listcell label="Y" />
		</listitem>
	</listbox>
</zk>

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: 2012-06-09 09:32:31 +0800

Seen: 488 times

Last updated: Jul 13 '12

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