Revision history [back]

click to hide/show revision 1
initial version

answered 2013-06-25 13:03:29 +0800

sjoshi gravatar image sjoshi flag of India

http://zkframeworkhint.bl...

See below Sample ...Its a Basic demo... ZUL FIle

<?page title="Auto Generated index.zul"?>
<window apply="org.zkoss.bind.BindComposer"
    viewModel="@id('vm') @init('demo.listbox.FilterViewModel')">


    <listbox model="@load(vm.modelList)">

        <auxhead sclass="category-center">
            <auxheader colspan="1">
                <textbox instant="true" width="100px"
                    value="@bind(vm.people.name)" onChange="@command('changeFilter')" />
            </auxheader>


        </auxhead>
        <listhead>
            <listheader label="Name"></listheader>
            <listheader label="Age"></listheader>
            <listheader label="Id"></listheader>
        </listhead>
        <template name="model" var="person">
            <listitem>

                <listcell>
                    <label value="@load(person.name)" />
                </listcell>
                <listcell>
                    <label value="@load(person.age)" />
                </listcell>
                <listcell>
                    <label value="@load(person.id)" />
                </listcell>
            </listitem>
        </template>




    </listbox>
</window>

Java Code or ViewModel

package demo.listbox;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.zkoss.bind.annotation.AfterCompose;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.zul.ListModel;
import org.zkoss.zul.ListModelList;

public class FilterViewModel {

    private List myPersonList;
    private Person people = new Person();
    private ListModel<Person> modelList;

    @AfterCompose
    public void afterCompose() {
        myPersonList = new ArrayList<Person>();

        myPersonList.add(new Person("Hariom", 1, "om1"));
        myPersonList.add(new Person("Narayan", 2, "Hariom1"));
        myPersonList.add(new Person("Shiv", 3, "shiv"));
        myPersonList.add(new Person("Ram", 4, "Maa"));
        myPersonList.add(new Person("Krishna", 5, "Durga"));
        modelList = new ListModelList<Person>(myPersonList);
    }

    @Command
    @NotifyChange("modelList")
    public void changeFilter() {
        List myPersonList1 = getFilterPersons(people);
        modelList = new ListModelList<Person>(myPersonList1);
    }

    public List<Person> getFilterPersons(Person personObj) {
        List<Person> personsList = new ArrayList<Person>();
        String name = null;
        Integer age = null;
        String id = null;
        if (personObj!=null && personObj.getName() != null)
            name = personObj.getName().toLowerCase();
        if (personObj!=null && personObj.getAge() != null)
            age = personObj.getAge();
        if (personObj!=null && personObj.getId() != null)
            id = personObj.getId().toLowerCase();

        for (Iterator<Person> i = myPersonList.iterator(); i.hasNext();) {
            Person tmp = i.next();

            if (tmp.getName().toLowerCase().contains(name) && !name.trim().isEmpty() ) {
                personsList.add(tmp);
            }

        }
        if(personsList.size()>0)
        return personsList;
        else
            return myPersonList;
    }

    public class Person {
        String name;
        Integer age;
        String id;

        public String getName() {
            return name;
        }

        public Integer getAge() {
            return age;
        }

        public String getId() {
            return id;
        }

        public void setName(String name) {
            this.name = name;
        }

        public void setAge(Integer age) {
            this.age = age;
        }

        public void setId(String id) {
            this.id = id;
        }

        public Person() {

        }

        public Person(String name, Integer age, String id) {
            super();
            this.name = name;
            this.age = age;
            this.id = id;
        }

    }

    public List getMyPersonList() {
        return myPersonList;
    }

    public Person getPeople() {
        return people;
    }

    public void setMyPersonList(List myPersonList) {
        this.myPersonList = myPersonList;
    }

    public void setPeople(Person people) {
        this.people = people;
    }

    public ListModel<Person> getModelList() {
        return modelList;
    }

    public void setModelList(ListModel<Person> modelList) {
        this.modelList = modelList;
    }

}

You can check here zk-listbox-data-filter-demoOR See below Sample ...Its a Basic demo... ZUL FIle

<?page title="Auto Generated index.zul"?>
<window apply="org.zkoss.bind.BindComposer"
    viewModel="@id('vm') @init('demo.listbox.FilterViewModel')">


    <listbox model="@load(vm.modelList)">

        <auxhead sclass="category-center">
            <auxheader colspan="1">
                <textbox instant="true" width="100px"
                    value="@bind(vm.people.name)" onChange="@command('changeFilter')" />
            </auxheader>


        </auxhead>
        <listhead>
            <listheader label="Name"></listheader>
            <listheader label="Age"></listheader>
            <listheader label="Id"></listheader>
        </listhead>
        <template name="model" var="person">
            <listitem>

                <listcell>
                    <label value="@load(person.name)" />
                </listcell>
                <listcell>
                    <label value="@load(person.age)" />
                </listcell>
                <listcell>
                    <label value="@load(person.id)" />
                </listcell>
            </listitem>
        </template>




    </listbox>
</window>

Java Code or ViewModel

package demo.listbox;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.zkoss.bind.annotation.AfterCompose;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.zul.ListModel;
import org.zkoss.zul.ListModelList;

public class FilterViewModel {

    private List myPersonList;
    private Person people = new Person();
    private ListModel<Person> modelList;

    @AfterCompose
    public void afterCompose() {
        myPersonList = new ArrayList<Person>();

        myPersonList.add(new Person("Hariom", 1, "om1"));
        myPersonList.add(new Person("Narayan", 2, "Hariom1"));
        myPersonList.add(new Person("Shiv", 3, "shiv"));
        myPersonList.add(new Person("Ram", 4, "Maa"));
        myPersonList.add(new Person("Krishna", 5, "Durga"));
        modelList = new ListModelList<Person>(myPersonList);
    }

    @Command
    @NotifyChange("modelList")
    public void changeFilter() {
        List myPersonList1 = getFilterPersons(people);
        modelList = new ListModelList<Person>(myPersonList1);
    }

    public List<Person> getFilterPersons(Person personObj) {
        List<Person> personsList = new ArrayList<Person>();
        String name = null;
        Integer age = null;
        String id = null;
        if (personObj!=null && personObj.getName() != null)
            name = personObj.getName().toLowerCase();
        if (personObj!=null && personObj.getAge() != null)
            age = personObj.getAge();
        if (personObj!=null && personObj.getId() != null)
            id = personObj.getId().toLowerCase();

        for (Iterator<Person> i = myPersonList.iterator(); i.hasNext();) {
            Person tmp = i.next();

            if (tmp.getName().toLowerCase().contains(name) && !name.trim().isEmpty() ) {
                personsList.add(tmp);
            }

        }
        if(personsList.size()>0)
        return personsList;
        else
            return myPersonList;
    }

    public class Person {
        String name;
        Integer age;
        String id;

        public String getName() {
            return name;
        }

        public Integer getAge() {
            return age;
        }

        public String getId() {
            return id;
        }

        public void setName(String name) {
            this.name = name;
        }

        public void setAge(Integer age) {
            this.age = age;
        }

        public void setId(String id) {
            this.id = id;
        }

        public Person() {

        }

        public Person(String name, Integer age, String id) {
            super();
            this.name = name;
            this.age = age;
            this.id = id;
        }

    }

    public List getMyPersonList() {
        return myPersonList;
    }

    public Person getPeople() {
        return people;
    }

    public void setMyPersonList(List myPersonList) {
        this.myPersonList = myPersonList;
    }

    public void setPeople(Person people) {
        this.people = people;
    }

    public ListModel<Person> getModelList() {
        return modelList;
    }

    public void setModelList(ListModel<Person> modelList) {
        this.modelList = modelList;
    }

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