Revision history [back]

click to hide/show revision 1
initial version

asked 2013-01-30 17:34:00 +0800

lramellavotta gravatar image lramellavotta flag of Italy

How can I synchronize data in a ListBox in MVVM ?

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.

How can I synchronize data in a ListBox in MVVM ?

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.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);
                    }

                }
            });

}

How can I synchronize data in a ListBox in MVVM ?

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);
                    }

                }
            });

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