0

Dynamically change background color of listitems in a listbox

asked 2014-01-30 21:03:45 +0800

rwoolf gravatar image rwoolf
35 5

I would like to dynamically change the background color of listitems/listcells in a listbox to highlight priority items. The code I have tried almost works, the problem is that the zebra striping overwrites the background color I have conditionally set on the items where the darker zebra background is set.

<style>
   .highPriority {background:lightpink};
</style>

<template name="model" var="item">
   <listitem>
      <attribute name="sclass" if='${item.priority=="1"}'>highPriority</attribute>
      <listcell>
         ...

If I add it as a zclass, or put the attribute on the list cell, then I no longer have the problem of the overwriting, but I also lose the functionality of mouseover highlighting, or changing to selected color.

Is there a way for me to conditionally change the background color of a listitem/listcell and retain the mouseover and selection coloring functionality?

delete flag offensive retag edit

Comments

Where your other questions right answered or do you have still problems there?

chillworld ( 2014-01-30 21:13:45 +0800 )edit

2 Answers

Sort by ยป oldest newest most voted
0

answered 2014-01-30 21:12:49 +0800

chillworld gravatar image chillworld flag of Belgium
5367 4 9
https://github.com/chillw...

updated 2014-02-01 13:00:04 +0800

why don't you add

.highPrioritySelected-odd{...}
.highPrioritySelected{...}
.highPriority-odd{....}
.highPriority{....}

Now for the problem of your selected item. That's not just css alone. First of all I should remove the "manual" set to the css class and add a renderer. I give you an example(already formatted for your problem) here :

package be.chillworld;

import org.zkoss.zul.Label;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.ListitemRenderer;

/**
*
* @author chillworld
*/
public class ListboxRenderer implements ListitemRenderer<Item>{

    public void render(Listitem item, Item data, int index) throws Exception {
        Listcell cell = new Listcell();       
        item.appendChild(cell);
        Label label = new Label(data.toString()); // set here what you want to see.
        cell.appendChild(label);
        if (item.isSelected()){
            doSelectedItem(label, data, index);
        } else {
            doNotSelecteItem(label, data, index);
        }

    }

    private void doSelectedItem(Label label, Item data, int index) {
        if (data.getPriority()==1) {
            if (index%2==0) {
                label.setClass("highPrioritySelected");
            } else {
                label.setClass("highPrioritySelected-odd");
            }
        } else if (index%2==0){
            label.setClass("z-listitem z-listitem-selected");
        } else {
           label.setClass("z-listitem z-listbox-odd z-listitem-selected");
        }
    }

    private void doNotSelecteItem(Label label, Item data, int index) {
        if (data.getPriority()==1) {
            if (index%2==0) {
                label.setClass("highPriority");
             } else {
                label.setClass("highPriority-odd");
             }
        } else if (index%2==0){
            label.setClass("z-listitem");
        } else {
            label.setClass("z-listitem z-listbox-odd");
        }
    }

}

edit : refactorig and add highpriority-odd

Greetz chill.

link publish delete flag offensive edit

Comments

The :hover works, but :active and :focus don't work for the selected item. If I can figure out what works for that then this is a workable solution.

rwoolf ( 2014-01-30 21:40:24 +0800 )edit

does this work?

chillworld ( 2014-01-31 18:17:32 +0800 )edit

I've been busy today with other things and have not tried this out yet. I'll check out your solution next Monday. Thanks

rwoolf ( 2014-01-31 22:05:00 +0800 )edit
0

answered 2014-01-31 08:09:15 +0800

cyiannoulis gravatar image cyiannoulis
1201 10

updated 2014-02-04 09:26:02 +0800

The following snippet works fine for me:

<style>
   .highPriority {background:lightpink !important};
</style>

<listbox id="lboxTasks" model="@bind(vm.tasks)">
 <listhead>
   <listheader label="Description" />
   <listheader label="Due Date" />
 </listhead>

 <template name="model" var="task">
   <listitem sclass="@load(task.priority eq 1 ? 'highPriority' : '')">
     <listcell>
       <textbox inplace="true" value="@bind(task.description)"  />
     </listcell>
     <listcell>
       <datebox inplace="true" value="@bind(task.dueDate)" format="d MMM yyyy" />
     </listcell>
   </listitem>
 </template>
</listbox>

Hope that helps /costas

link publish delete flag offensive edit

Comments

When you hover over a high priority item the listitem is painted blue and again pink when your mouse is out. When you select a pink listitem the color turns into the default blue. I have tested with zk 7 and 6.5.

cyiannoulis ( 2014-01-31 09:09:44 +0800 )edit

This does not work. This behaves exactly like my original problem. If the high priority item lands on a "white" stripe then it works, but if the high priority item lands on a "dark" stripe then it does not. Make every item high priority and you will see only every other item is painted pink.

rwoolf ( 2014-01-31 17:35:14 +0800 )edit

Sorry, my mistake.. just add the "!important" clause in your style and you should be ok

<style> .highPriority {background:lightpink !important}; </style>

cyiannoulis ( 2014-02-04 09:25:19 +0800 )edit

When I add the "!important" clause then it behaves just like when I set it using zclass. The colored cells no longer highlight when hovering over them and they do not change color when selecting. I wish I had a simple answer, but I will likely have to go the writing of a renderer to resolve this.

rwoolf ( 2014-02-10 20:52:48 +0800 )edit
Your answer
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
1 follower

RSS

Stats

Asked: 2014-01-30 21:03:45 +0800

Seen: 143 times

Last updated: Feb 04 '14

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