-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Hi, I'm new on 6.5 and I'd like testing MVVM in a simple example. So I created a little form for viewing a listbox. In this listbox I can only view data or delete only record one-by-one. When I delete a record how can I refresh (automatically?!?) the listbox?
Thanks
My simple form
<window title="Gestione Utenti" border="normal" apply="org.zkoss.bind.BindComposer"
viewModel="@id('vm') @init('rvl.stcc.ui.aaa')" >
<caption>
<button label="Cancella" onClick="@command('delUtente')" />
</caption>
<listbox sizedByContent="true" span="true" autopaging="true" model="@load(vm.lstUtenti)" selectedItem="@bind(vm.selected)">
<listhead sizable="true">
<listheader label="index"/>
<listheader label="Utente" sort="auto(utente)"/>
</listhead>
<template name="model" var="Item" status="s">
<listitem>
<listcell label="@bind(s.index)"/>
<listcell label="@bind(Item.utente)" />
</listitem>
</template>
</listbox>
My simple class
public class aaa{
private List<Utenti> lstUtenti;
private Utenti selected;
@init
public void init()
{
System.out.println("init");
}
public List<Utenti> getlstUtenti()
{
UtentiDAO pdao = (UtentiDAO)SpringUtil.getBean("UtentiDAO");
lstUtenti=pdao.findAll();
System.out.println("getUtenti");
return lstUtenti;
}
@NotifyChange("selected")
public void setSelected(Utenti selected)
{
this.selected = selected;
}
public Utenti getSelected()
{
return selected;
}
@Command
@NotifyChange({"utenti","selected"})
public void delUtente() throws Exception
{
Messagebox.show("Confermi la cancellazione di "+selected.getUtente()+" ?","Confirm",Messagebox.YES | Messagebox.NO,Messagebox.QUESTION,
new EventListener<Event>()
{
@Override
public void onEvent(Event event) throws Exception
{
if (event.getName().equals("onYes"))
{
System.out.println("Cancellato");
UtentiDAO pdao = (UtentiDAO)SpringUtil.getBean("UtentiDAO");
pdao.delete(selected.getUtente());
selected=null;
getlstUtenti().remove(selected);
}
}
});
}
Yes you can You did some mistakes see this line of code
selected=null;
getlstUtenti().remove(selected);
When you already initialize selected=null;
how can yoy remove this object from your main list? and rather than using NotifyChange you can use...
utenti.remove(selected);
selected = new HashSet<E>(); //E will be your object
BindUtils.postNotifyChange(null,null,this,utenti);
BindUtils.postNotifyChange(null,null,this,selected);
I think this code snippet will help you to overcome the issue.
The problem is easaliy solved.. your NotifyChange is wrong. You changed another field in your model, which is not listed there.. Try this:
@NotifyChange({"utenti","selected", "lstUtenti"})
I know, your list is not a "real" member, but it has been changed anyway. So you have to notify the controller of the change and he will redraw the list.
This code works
private List<Utenti> lstUtenti;
private Utenti selected;
@Init
public void init()
{
UtentiDAO pdao = (UtentiDAO)SpringUtil.getBean("UtentiDAO");
lstUtenti=pdao.findAll();
System.out.println("init");
}
public List<Utenti> getlstUtenti()
{
return lstUtenti;
}
...
@Command
@NotifyChange({"lstUtenti","selected"})
public void delUtente() throws Exception
{
Messagebox.show("Confermi la cancellazione di "+selected.getUtente()+" ?","Confirm",Messagebox.YES | Messagebox.NO,Messagebox.QUESTION,
new EventListener<Event>()
{
@Override
public void onEvent(Event event) throws Exception
{
if (event.getName().equals("onYes"))
{
System.out.println("Cancellato");
UtentiDAO pdao = (UtentiDAO)SpringUtil.getBean("UtentiDAO");
pdao.delete(selected.getUtente());
lstUtenti.remove(lstUtenti.indexOf(selected));
BindUtils.postNotifyChange(null,null,aaa.this,"lstUtenti");
selected=null;
}
}
});
}
I hope it can help someone Luca
Asked: 2013-01-30 17:34:00 +0800
Seen: 86 times
Last updated: Jan 31 '13
Databinding and auto-complete on combobox
Composite component and bind in ZK 6
How to detach / reattach MVVM windows?
Is there a way to resolve view model properties as input to client side javascripts?
MVVM Validator: class not found ? [closed]
How to Call Child ViewModel Method from Parent Window? [closed]
[Solved]Aplikasi Hibernate Spring [closed]
zk mvvm > what's the best component to make LOV (list of value) of a master data [closed]