notify change of grid/list/tree inside a grid/list/tree

前端 未结 1 1632
后悔当初
后悔当初 2021-01-24 01:38

I saw multiple questions of people who wants to update a part of a grid/list/tree with mvvm but they didn\'t wanted to refresh the whole list.

For all the people who has

1条回答
  •  闹比i
    闹比i (楼主)
    2021-01-24 02:03

    This is a simpel example. The most important of the whole code is this :

    BindUtils.postNotifyChange(null, null, person, "childs");
    

    First simple pojo class :

    package be.chillworld;
    
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     *
     * @author chillworld
     */
    public class Person {
        private int id;
        private String naam;
        private List childs = new ArrayList();
    
        public Person(int id) {
            this.id = id;
            naam = "test " + id;
        }
    
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public void setNaam(String naam) {
            this.naam = naam;
        }
    
        public String getNaam() {
            System.out.println("asked getter (naam) of "+ id);
            return naam;
        }
    
        public List getChilds() {
            System.out.println("asked getter (childs) of "+ id);
            return childs;
        }
    
        public void setChilds(List childs) {
            this.childs = childs;
        }
    
        public boolean addChild(Person person) {
            return childs.add(person);
        }
    
        @Override
        public String toString() {
            return "Person{" + "id=" + id + ", name=" + getNaam() + '}';
        }
    }
    

    then the IndexVM:

    package be.chillworld;
    
    import java.util.ArrayList;
    import java.util.List;
    import org.zkoss.bind.BindUtils;
    import org.zkoss.bind.annotation.BindingParam;
    import org.zkoss.bind.annotation.Command;
    
    /**
     *
     * @author chillworld
     */
    public class IndexVm {
    
        private List persons;
        int i;
    
        public IndexVm() {
            System.out.println("starting creating list");
            persons = new ArrayList();
            for (i = 0; i < 100; i++) {
                Person person = new Person(i);
                person.addChild(new Person(++i));
                persons.add(person);
            }
            System.out.println("ending creating list");
    
        }
    
        public List getPersons() {
            return persons;
        }
    
        public void setPersons(List persons) {
            this.persons = persons;
        }
    
        @Command
        public void showIndex(@BindingParam("person") Person person) {
            System.out.println("changed name");
            person.setNaam("Chillworld");
            BindUtils.postNotifyChange(null, null, person, "naam");
        }
    
        @Command
        public void addChild(@BindingParam("person") Person person) {
            System.out.println("add child");
            Person child = new Person(++i);
            child.setNaam("new child");
            person.addChild(child);
            BindUtils.postNotifyChange(null, null, person, "childs");
        }
    }
    

    and at last the index.zul :

    
    
    
                
                        
                 
                            
                        
                    
                
        
    
    
    

    0 讨论(0)
提交回复
热议问题