Requirement Constraints

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Sunday, 3 February 2013

Move List box item up and Down using MVVM Design Pattern

Posted on 11:35 by Unknown
Let us see how we can implement Moving the list box item up and down. In almost all the application, we need to change the order of the record that are displayed in the list. Say for an example, if you are defining questions for the subject/exam, then question order is very important. So this example shows how you can change the order of the question using up and down button.

Technologies Used

1. ZK 6.5 CE Version

Reference

  • http://www.zkoss.org/zkdemo/listbox/dual_listbox
  • http://forum.zkoss.org/question/42954/listbox-refresh/

Project Name and Structure

MVVMListBoxMoveUpAndDown

image
Concepts used in this example
  1. We have used List box to display two column listing.
  2. We also used the "emptyMessage" attribute is used to show a message when listbox contains no items.
  3. MVVM Design pattern
  4. We are using Listbox Template concept to display the data.

Step 1:

If you are new ZK, then you can setup the Development environment by downloading this document.

Step 2:

Let us create the Project in eclipse. Click File -> New -> ZK Project . Let us name the project as MVVMListBoxMoveUpAndDown.

Step 3:

Now let us add the domain class Questions.java as follows. Note: We will create a package called "zkMVVM" and then we will create our class "Questions" in that.
package zkMVVM;

 

public class Questions {

 

    private Integer questionOrder;

    private String questionName;

 

    public Questions() {

    }

 

    public Questions(Integer questionOrder, String questionName) {

        this.questionOrder = questionOrder;

        this.questionName = questionName;

    }

 

    public Integer getQuestionOrder() {

        return questionOrder;

    }

 

    public void setQuestionOrder(Integer questionOrder) {

        this.questionOrder = questionOrder;

    }

 

    public String getQuestionName() {

        return questionName;

    }

 

    public void setQuestionName(String questionName) {

        this.questionName = questionName;

    }

 

}

Step 4:

We then define a service class to perform the business logic (QuestionService.java) shown below:.

package zkMVVM;

import java.util.List;

 

public interface QuestionService {

 

    /**

     * Retrieve all questions in the catalog.

     * 

     * @return all questions

     */

    public List<Questions> findAll();

 

}

Step 5:

Next we create our implementation class for the above interface. For simplicity, it uses a static list object as the data model.

package zkMVVM;

import java.util.LinkedList;

import java.util.List;

 

import org.zkoss.zul.Messagebox;

 

public class QuestionsServiceImpl implements QuestionService {

 

    // data model

    private List<Questions> questionsList = new LinkedList<Questions>();

    private static int id = 1;

 

    // initialize book data

    public QuestionsServiceImpl() {

        questionsList.add(new Questions(1, "What was your childhood nickname?"));

        questionsList.add(new Questions(2, "In what city did you meet your spouse/significant other?"));

        questionsList.add(new Questions(3, "What is the middle name of your oldest child?"));

        questionsList.add(new Questions(4, "What is the name of the place your wedding reception was held?"));

        questionsList.add(new Questions(5, "What year did you graduate from High School?"));

        questionsList.add(new Questions(6, "What was the make and model of your first car?"));

        questionsList.add(new Questions(7, "What is the name of the company of your first job?"));

        

    }

 

    public List<Questions> findAll() {

         

        return questionsList;

    }

 

}

Step 6:

Now we will add our listing zul file as follows

<window title="Listitem MVVM Move up and Down" border="normal"

    apply="org.zkoss.bind.BindComposer"

    viewModel="@id('myvm') @init('zkMVVM.QuestionsListVM')">

    <hlayout>

        <listbox id="questionListbox" model="@load(myvm.allQuestions)"

            selectedItem="@bind(myvm.curSelectedQuestions)" width="1000px"

            emptyMessage="No Question found in the result">

 

            <listhead sizable="true">

                <listheader label="Question Order" width="20%"

                    sort="auto(questionOrder)" />

                <listheader label="Question" width="80%"

                    sort="auto(questionName)" />

            </listhead>

            <template name="model" var="p1">

                <listitem>

                    <listcell label="@load(p1.questionOrder)" />

                    <listcell label="@load(p1.questionName)" />

                </listitem>

            </template>

        </listbox>

        <vbox>

            <image style="cursor:pointer" id="upBtn"

                onClick="@command('moveUp')" src="${imgPath}/uparrow_g.png" />

            <image style="cursor:pointer" id="downBtn"

                onClick="@command('moveDown')" src="${imgPath}/downarrow_g.png" />

        </vbox>

    </hlayout>

</window>

Step 7:

Next we will add our VM for the above zul file which will take of Move up and Move down.

package zkMVVM;

 

import java.util.List;

 

import org.zkoss.bind.annotation.AfterCompose;

import org.zkoss.bind.annotation.Command;

import org.zkoss.bind.annotation.NotifyChange;

 

public class QuestionsListVM {

 

    private List<Questions> result;

    private QuestionService questionService = new QuestionsServiceImpl();

    private Questions curSelectedQuestions;

    private Integer curSelecteQuestionsIndex;

 

    public Questions getCurSelectedQuestions() {

        return curSelectedQuestions;

    }

 

    public void setCurSelectedQuestions(Questions curSelectedQuestions) {

        this.curSelectedQuestions = curSelectedQuestions;

    }

 

    public Integer getCurSelecteQuestionsIndex() {

        return curSelecteQuestionsIndex;

    }

 

    public void setCurSelecteQuestionsIndex(Integer curSelecteQuestionsIndex) {

        this.curSelecteQuestionsIndex = curSelecteQuestionsIndex;

    }

 

    @AfterCompose

    public void initSetup() {

        result = questionService.findAll();

    }

 

    public List<Questions> getallQuestions() {

        return result;

    }

 

    @Command

    @NotifyChange("allQuestions")

    public void moveUp() {

        

        int selectedOrder;

        int nextOrder;

 

        // If no item is selected, then do nothing

        if (curSelectedQuestions == null)

            return;

 

        // If selected item is in the first i.e index is eq to zero,

        // then do nothing

        int index = result.indexOf(curSelectedQuestions);

        if (index == 0 || index < 0)

            return;

 

        // Store the currently selected item

        Questions selectedItem = result.get(index);

 

        // Take the next item

        Questions nextItem = result.get(index - 1);

 

        // Get the order for the currrently selected Item

        selectedOrder = selectedItem.getQuestionOrder();

 

        // Get the order for the next item of the selected item

        nextOrder = nextItem.getQuestionOrder();

 

        // Swap the order - In the real world, you can make a DB Call to do this

        selectedItem.setQuestionOrder(nextOrder);

        nextItem.setQuestionOrder(selectedOrder);

 

        // Since we changed the order of the next item, we need to refresh the

        // ZUL

        result.set(result.indexOf(nextItem), nextItem);

 

        // Remove the selected item from the list

        result.remove(selectedItem);

 

        // Add the selected item to the list in the next idex position

        result.add(--index, selectedItem);

 

        // Very Important, select the same item once we move down

        setCurSelectedQuestions(selectedItem);

    }

 

    @Command

    @NotifyChange("allQuestions")

    public void moveDown() {

 

        int selectedOrder;

        int nextOrder;

 

        // If no item is selected, then do nothing

        if (curSelectedQuestions == null)

            return;

 

        // If selected item is in the last i.e index is eq to size of the list,

        // then do nothing

        int index = result.indexOf(curSelectedQuestions);

        if (index == result.size() - 1 || index < 0)

            return;

 

        // Store the currently selected item

        Questions selectedItem = result.get(index);

 

        // Take the next item

        Questions nextItem = result.get(index + 1);

 

        // Get the order for the currrently selected Item

        selectedOrder = selectedItem.getQuestionOrder();

 

        // Get the order for the next item of the selected item

        nextOrder = nextItem.getQuestionOrder();

 

        // Swap the order - In the real world, you can make a DB Call to do this

        selectedItem.setQuestionOrder(nextOrder);

        nextItem.setQuestionOrder(selectedOrder);

 

        // Since we changed the order of the next item, we need to refresh the

        // ZUL

        result.set(result.indexOf(nextItem), nextItem);

 

        // Remove the selected item from the list

        result.remove(selectedItem);

 

        // Add the selected item to the list in the next idex position

        result.add(++index, selectedItem);

 

        // Very Important, select the same item once we move down

        setCurSelectedQuestions(selectedItem);

 

    }

 

}

Step 8:

Now you can run the QuestionsList.zul.

image_thumb2


You can download the source here
Online Demo here.

Email ThisBlogThis!Share to XShare to Facebook
Posted in ZK MVVM ListBox | No comments
Newer Post Older Post Home

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

  • ZK Example for inline Editing with Add New and Delete
    I am quite impressed on this demo from ZK . But adding new record and delete existing record is missing as part of typical CRUD. So i thoug...
  • EDI 5010 Documentation 837 Professional - Loop 2010BB Payer Name
    2010BB Payer Name          In this loop, all the information will be taken from Insurance master screen. Take a look of our sample screen...
  • EDI 5010 Documentation–837 - BHT - Beginning of Hierarchical Transaction
    BHT – Beginning of Hierarchical Transaction Loop Seg ID Segment Name Format Length Ref# Req Value   BHT Beginning of Hier...
  • Hibernate Validator Example 2
    In this example, we will see some more validation constraints such as @email, @past, @length, etc. And also we will also define custom error...
  • ZK Passing Parameter between two files using MVVM–Part 1
    Overview This is the first series of articles about Passing parameter between two zul files using MVVM Design pattern .This article will fo...
  • MVVM Command annotation and Notify change example
    Here is an example, how to pass parameter on a zul through MVVM Command binding annotation. ZK URL http://books.zkoss.org/wiki/ZK%20Develo...
  • History of Present Illness
    HPI - One of the main component of Clinical History. What is an HPI ? The history of present illness (HPI) is a chronological description...
  • Patient Demographics
    Patient browse (search) is the key element for any EMR / PMS Software. In my past 15 years experience, i involved more than 5 times in desig...
  • ViewModel Class Java Annotation @Init, @NotifyChange, @Command
    In following sections we'll list all syntaxes that can be used in implementing a ViewModel and applying ZK bind annotation. The ZK binde...
  • Good Website Design Links
    Form Design Label Placement in Forms International Address Fields in Web Forms 40 Eye-Catching Registration Pages blog-comment-form-...

Categories

  • Billing Process
  • C Workbook
  • C++ Workbook
  • Eclipse Tips
  • EDI 5010
  • EMR Appointment Features
  • EMR Labs Stuff
  • EMR PMS Links
  • EMR Use cases
  • EMR Vital Sign
  • Good Website Design
  • Hibernate Criteria Queries
  • Hibernate Introduction
  • Hibernate Introduction Setup
  • Hibernate Mapping
  • Hibernate POC
  • Hibernate Validator
  • Hibernate–Java Environment setup
  • HPI
  • Java
  • Maven
  • MU Certification
  • NPI
  • PQRS
  • Practice Management System
  • Spring Security
  • Tech Links
  • Today Tech Stuff
  • zk
  • ZK Hibernate
  • ZK 5 Databinding
  • ZK Application
  • ZK Calling Another ZUL
  • ZK CheckBox
  • ZK CreateComponents
  • ZK CSS
  • ZK extended Components
  • ZK Foreach
  • ZK Forum Posts
  • ZK Framework
  • ZK Hibernate Setup
  • ZK ID Space
  • ZK Include
  • ZK Installation
  • ZK iReport
  • ZK Layout
  • ZK Listitem Pagination
  • ZK Message Box
  • ZK MVC
  • ZK MVC Combox Box
  • ZK MVC CRUD Examples
  • ZK MVC Listbox
  • ZK MVVM
  • ZK MVVM Combo
  • ZK MVVM CRUD
  • ZK MVVM ListBox
  • ZK Spring
  • ZK TextBox

Blog Archive

  • ▼  2013 (105)
    • ►  December (3)
    • ►  September (7)
    • ►  August (13)
    • ►  July (1)
    • ►  June (11)
    • ►  May (3)
    • ►  April (14)
    • ►  March (19)
    • ▼  February (21)
      • ZK MVVM CRUD With Spring 3 + JPA + Hibernate 4 Ent...
      • ZK MVVM CRUD With Spring 3 + JPA + Hibernate 4 Ent...
      • ZK MVVM CRUD With Spring 3 + JPA + Hibernate 4 Ent...
      • ZK MVVM CRUD With Spring 3 + JPA + Hibernate 4 Ent...
      • ZK MVVM CRUD With Spring 3 + JPA + Hibernate 4 Ent...
      • ZK Passing Parameter between two files using MVVM–...
      • ZK Passing Parameter between two files using MVVM–...
      • ZK Passing Parameter between two files using MVC a...
      • How to submit the claims after primary insurance ?
      • ZK Passing Parameter between two files using MVC -...
      • ZK Passing Parameter between two files using MVC -...
      • MVVM CRUD with Spring 3.1 and Hibernate 4 and ZK -...
      • ZK Passing Parameter between two files using MVC -...
      • Move List box item up and Down using MVC Design Pa...
      • MVVM CRUD with Spring 3.1 and Hibernate 4 and ZK -...
      • Move List box item up and Down using MVVM Design P...
      • MVVM CRUD with Spring 3.1 and Hibernate 4 and ZK -...
      • ZK Passing Parameter between two files using MVC -...
      • MVVM CRUD with Spring 3.1 and Hibernate 4 and ZK -...
      • ZK Passing Parameter between two files using MVC -...
      • MVVM CRUD with Spring 3.1 and Hibernate 4 and ZK -...
    • ►  January (13)
  • ►  2012 (177)
    • ►  December (1)
    • ►  November (13)
    • ►  October (19)
    • ►  September (24)
    • ►  August (26)
    • ►  July (6)
    • ►  June (37)
    • ►  May (30)
    • ►  April (16)
    • ►  March (1)
    • ►  January (4)
  • ►  2011 (5)
    • ►  December (1)
    • ►  November (1)
    • ►  July (1)
    • ►  June (1)
    • ►  April (1)
  • ►  2010 (1)
    • ►  September (1)
Powered by Blogger.

About Me

Unknown
View my complete profile