0

Multiple selection in listbox with databinding

asked 2012-01-05 09:00:26 +0800

Neus gravatar image Neus
1415 14

Hi,
I have a listbox with attributes multiple="true" and checkmark="true".
When the listbox is created (with no items because items only appears when a button is clicked) the checkmark to select multiple items is displayed in the listheader.
But when I fill the listbox with data (binder.loadComponent(LBFicheros)) the checkbox disappears and a radiobutton is displayed in each row. I checked if the listbox for some reason changes to multiple="false" but is not so, listbox is still multiple.

Any idea why is this happening?

My listbox:

<listbox  id="LBFicheros" multiple="true" checkmark="true" width="100%" vflex="1" mold="paging" autopaging="true"  model="@{VentanaFicheros$composer.listaBeans}">
          <listhead sizable="true">
                    ...
          </listhead>
          <listitem id="fila" self="@{each=ServidorBean}" value="@{ServidorBean}">
                    <listcell label="@{ServidorBean.Tabla}" tooltiptext="@{ServidorBean.Tabla}" style="text-align:left;white-space:nowrap"/>
                     ....
         </listitem>
</listbox>

delete flag offensive retag edit

15 Replies

Sort by ยป oldest newest

answered 2012-01-05 09:50:45 +0800

Matze2 gravatar image Matze2
773 7

Which ZK version?

link publish delete flag offensive edit

answered 2012-01-05 11:42:24 +0800

Neus gravatar image Neus
1415 14

I just found the solution.
It was my fault.

Sorryyy!

link publish delete flag offensive edit

answered 2012-01-05 12:34:34 +0800

Steva77 gravatar image Steva77 flag of Italy
1014 3
http://www.research.softe...

Neus, please could you post the solution to the problem, or a description of your error, for future readers?

link publish delete flag offensive edit

answered 2012-01-27 09:58:10 +0800

Neus gravatar image Neus
1415 14

I was doing something wrong..I don't think that naybody will do that too :P (in some part of my application I was setting multiple to false)

But...now I update to ZK 6 and it is happening again!And it seems that this time is not my fault!

Don't know why is this happening...But setting listbox multiple="true" and checkmrk ="true" doesn't work. It shows a radiobutton and no multiple selection is available.

I found and example in ZK Fiddle in which is happening the same...

ZKFiddle-Link

index.zul
<?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit" ?>
<zk>
<zscript>
class customListbox extends org.zkoss.zul.Listbox {
public Listitem getSelectedItem() {
Listitem li = super.getSelectedItem();
if (li == null) {
// get first item if no one is selected
li = getItemAtIndex(0);
}
if (li == null) {
// create an invalid item if no item in listbox
li = new Listitem();
People p = new People();
p.setName("Invalid Name");
p.setAge(999);
li.setValue(p);
}
return li;
}
}
page.getComponentDefinition("listbox", true).setImplementationClass(customListbox.class);

// the class of people
class People {
String name;
int age;

public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}

// create data list
List lpeoples = new ArrayList();
List lpeoplesEmpty = new ArrayList();
for (int i = 15;i != 20;i ++) {
People p = new People();
p.setName("Name "+i);
p.setAge(i);
lpeoples.add(p);
}
</zscript>
<html><![CDATA[
<ol>
<li> check / uncheck the "clear data" checkbox to clear / restore the data</li>
<li> the value will show in textbox when header double_clicked</li>
<li> if an item selected, selected item is that one</li>
<li> if no item selected, selected item is the first item</li>
<li> if no item in listbox, will create an invalid one</li>
</ol>
]]>
</html>
<vbox>
<checkbox label="clear data">
<attribute name="onCheck">
if (self.checked)
listOfPeople.setModel(new BindingListModelList(lpeoplesEmpty, true));
else
listOfPeople.setModel(new BindingListModelList(lpeoples, true));
</attribute>
</checkbox>
<hbox>
name: <textbox id="sName" />
</hbox>
<hbox>
age: <textbox id="sAge" />
</hbox>
</vbox>
<listbox id="listOfPeople" checkmark="true" multiple="true" model="@{lpeoples}"
value="@{peopleData}" selectedItem="@{selectedPeople}">
<listhead sizable="true">
<attribute name="onDoubleClick">
People p = (People)listOfPeople.getSelectedItem().getValue();
sName.setValue(p.getName());
sAge.setValue(p.getAge().toString());
</attribute>
<listheader label="Name"/>
<listheader label="Age"/>
</listhead>
<listitem self="@{each=people}" value="@{people}">
<listcell label="@{people.name}"/>
<listcell label="@{people.age}"/>
</listitem>
</listbox>
</zk>

In this example checkmark and multiple are setted to true but if you run it using Zk 6.0 only radiobutton appears and you can't select more than one item!

link publish delete flag offensive edit

answered 2012-01-27 10:28:49 +0800

Matze2 gravatar image Matze2
773 7

updated 2012-01-27 10:30:15 +0800

In ZK6, the multiple flag of the llistbox may get overwritten by the multiple flag of the listmodel. These are the possibilities to circumvent it:
- provide a "real" ListModel as model and set multiple=true there
- with ZK6 binding you can bind selectedItems instead of selectedItem, then multiple=true is set automatically
- use a converter like that on the model to fix that behavior:

/**
 * Fix multiple selection state.
 *  
 * @author matze
 */
public class MultiSelectionConverter extends ListModelConverter {
	private static final long serialVersionUID = 1L;

	@Override
	public Object coerceToUi(Object val, Component comp) {
		Object bean = super.coerceToUi(val, comp);
		AbstractListModel<?> model = (AbstractListModel<?>) bean;
		Listbox listbox = (Listbox) comp;
		
		if (listbox.isMultiple() && !model.isMultiple()) {
			model.setMultiple(true);
		}
		return bean;
	}
}

link publish delete flag offensive edit

answered 2012-01-30 08:58:57 +0800

Neus gravatar image Neus
1415 14

Thank you! I fixed it using the converter!

link publish delete flag offensive edit

answered 2012-02-17 11:18:25 +0800

Neus gravatar image Neus
1415 14

updated 2012-02-17 11:22:26 +0800

I'm changing to ZK6 binding.
Matze2 said that with ZK6 Bind if selectedItems are bind multiple="true" is set automatically.
I tried it and multiple="true"wasn't set.
So I hope some of you can help me finding what I'm not doing well:
This is my listbox:

<listbox nonselectableTags="*" id="LBFicheros" multiple="true" checkmark="true" mold="paging" autopaging="true" model="@load(vm.items)" selectedItems="@bind(vm.selected)">

As you can see I'm binding selectedItems
Then, this is my java code
private Set <ServidorBean> selected;
public Set<ServidorBean> getSelected() {
       System.out.println("Entra en getSelected");
	return selected;
}
public void setSelected(Set<ServidorBean> selected){
	System.out.println("Entra en setSelected");
	this.selected = selected;
}

With this code multiple selection is not enabled and radiobuttons are shown instead of checkboxes.

If I set multiple="true" in the ListModelList it works OK but I understood (maybe I'm wrong) that it is not necessary if selectedItems are binded.

link publish delete flag offensive edit

answered 2012-02-17 12:06:32 +0800

Matze2 gravatar image Matze2
773 7

updated 2012-02-17 12:07:49 +0800

Things changed during the ZK6 development. Currently my multi-selection listboxes look like the following:

<listbox multiple="true"
	selectedItems="@bind(vm.selectedEntries)"
	model="@load(vm.entries)">

This works with ZK6 release, as far as I remember (cannot check that currently).
As mentioned in another post today, I cannot help with "checkmark" feature.

link publish delete flag offensive edit

answered 2012-02-17 12:29:27 +0800

Neus gravatar image Neus
1415 14

Ok, I reproduce your listbox:

<listbox id="LBFicheros" multiple="true" model="@load(vm.items)" selectedItems="@bind(vm.selected)">

I remove the paging and the checkmark="true".
And I can't even select multiple items. I press CTRL key and click various items but selection change instead of select all items.
So, my question is...If it works for you..Maybe there's something wrong (that I can't see) with my getter, setter and the instance of selected? Could anyone confirm me that they are ok??

I repeat that setting multiple to listmodellist works.

link publish delete flag offensive edit

answered 2012-02-17 13:43:22 +0800

Matze2 gravatar image Matze2
773 7

updated 2012-02-17 13:46:04 +0800

Yes, this looks ok. Do you use ZK6 release?
And how does your getItems() looks like?

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-01-05 09:00:26 +0800

Seen: 1,399 times

Last updated: Feb 21 '12

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