0

How can I synchronize data in a ListBox in MVVM ? [closed]

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

lramellavotta gravatar image lramellavotta flag of Italy
200 1 8

updated 2013-02-01 01:45:29 +0800

jimyeh gravatar image jimyeh
2047 1 4
ZK Team

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

                }
            });

}
delete flag offensive retag edit

The question has been closed for the following reason "the question is answered, right answer was accepted" by sjoshi
close date 2013-02-08 06:31:54

3 Answers

Sort by ยป oldest newest most voted
1

answered 2013-01-30 17:59:46 +0800

sjoshi gravatar image sjoshi flag of India
3508 1 8
http://zkframeworkhint.bl...

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.

link publish delete flag offensive edit

Comments

Thanks, you're right. I wrote some errors.

lramellavotta ( 2013-01-30 21:18:50 +0800 )edit

Please close the question.Thanks

sjoshi ( 2013-01-31 06:22:39 +0800 )edit
0

answered 2013-01-30 21:21:55 +0800

GEdelmann gravatar image GEdelmann
5 1

updated 2013-01-31 04:51:57 +0800

sjoshi gravatar image sjoshi flag of India
3508 1 8
http://zkframeworkhint.bl...

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.

link publish delete flag offensive edit

Comments

Thanks, we wrote at the same time .. :)

lramellavotta ( 2013-01-30 21:25:00 +0800 )edit

@GEdelmann NotiChange will work with only @Command and in this question @lramellavotta deleting a record after the OK event fire So NotifyChange will not work here.

sjoshi ( 2013-01-31 04:53:44 +0800 )edit
0

answered 2013-01-30 21:23:25 +0800

lramellavotta gravatar image lramellavotta flag of Italy
200 1 8

updated 2013-01-30 21:25:51 +0800

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

link publish delete flag offensive edit

Comments

Please accept the answer and close the question as it is resolved and from my view no need to use NotifyChange as you already use this in method level.

sjoshi ( 2013-01-31 04:35:34 +0800 )edit

Question tools

Follow
2 followers

RSS

Stats

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

Seen: 81 times

Last updated: Jan 31 '13

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