Requirement Constraints

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

Monday, 4 February 2013

Move List box item up and Down using MVC Design Pattern

Posted on 10:05 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

MVCListBoxMoveUpDown

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. In ZK, the controller is responsible for controlling ZK components and listening to events triggered by user interaction.
    We can create a such controller class by simply extending org.zkoss.zk.SelectorComposer.
  4. We also used Annotation in the composer using ZK Select Composer.
  5. MVC Developer reference
  6. 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 MVCListBoxMoveUpDown.

Step 3:

Now let us add the domain class Questions.java as follows. Note: We will create a package called "zkmvc" and then we will  create our class "Questions" in that.
package zkmvc;
 
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 zkmvc;
 
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 zkmvc;
 
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="Questions List" border="normal"
    apply="zkmvc.QuestionsListController">
    <hlayout  >
        <listbox id="questionListbox" width="1000px"
            emptyMessage="No Question found in the result">
            <listhead>
                <listheader label="Question Order" width="20%" />
                <listheader label="Question"  width="80%"/>
            </listhead>
            <template name="model">
                <listitem>
                    <listcell label="${each.questionOrder}"></listcell>
                    <listcell label="${each.questionName}"></listcell>
                </listitem>
            </template>
        </listbox>
        <vbox  >
            <image style="cursor:pointer" id="upBtn"
                src="${imgPath}/uparrow_g.png" />
            <image style="cursor:pointer" id="downBtn"
                src="${imgPath}/downarrow_g.png" />
        </vbox>
    </hlayout>
</window>

Step 7:

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

package zkmvc;
 
import java.util.List;
import java.util.Set;
 
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.AbstractListModel;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Messagebox;
 
/**
 * To simplify the implementation of the controller part of UI, ZK provides
 * several skeleton implementations. For example, SelectorComposer, as one of
 * the most popular skeletons, wires components, variables and event listeners
 * automatically based on Java annotations you specify.
 */
public class QuestionsListController extends SelectorComposer<Component> {
 
    private static final long serialVersionUID = 1L;
 
    /**
     * In SelectorComposer, when you specify a @Wire annotation on a field or
     * setter method, the SelectorComposer will automatically find the component
     * and assign it to the field or pass it into the setter method.
     **/
    @Wire
    private Listbox questionListbox;
    private ListModelList<Questions> questionList;
 
    private QuestionService questionService = new QuestionsServiceImpl();
 
    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp);
        List<Questions> result = questionService.findAll();
        questionList = new ListModelList<Questions>(result);
        questionListbox.setModel(questionList);
    }
 
    @Listen("onClick = #downBtn")
    public void down() {
 
        int selectedOrder;
        int nextOrder;
 
        Set<Questions> selected = questionList.getSelection();
        //If no item is selected, then do nothing
        if (selected.isEmpty())
            return;
 
        //If selected item is in the last i.e index is eq to size of the list, then do nothing
        int index = questionList.indexOf(selected.iterator().next());
        if (index == questionList.size() - 1 || index < 0)
            return;
 
        //Store the currently selected item
        Questions selectedItem = questionList.get(index);
        
        //Take the next item
        Questions nextItem = questionList.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
        questionList.set(questionList.indexOf(nextItem), nextItem);
 
        //Remove the selected item from the list 
        questionList.remove(selectedItem);
        
        // Add the selected item to the list in the next idex position
        questionList.add(++index, selectedItem);
        
        // Very Important, select the same item once we move down
        questionList.addToSelection(selectedItem);
 
    }
 
    @Listen("onClick = #upBtn")
    public void up() {
 
        int selectedOrder;
        int nextOrder;
 
        Set<Questions> selected = questionList.getSelection();
        if (selected.isEmpty())
            return;
        int index = questionList.indexOf(selected.iterator().next());
        if (index == 0 || index < 0)
            return;
 
        Questions selectedItem = questionList.get(index);
        Questions nextItem = questionList.get(index - 1);
        selectedOrder = selectedItem.getQuestionOrder();
        nextOrder = nextItem.getQuestionOrder();
        selectedItem.setQuestionOrder(nextOrder);
        nextItem.setQuestionOrder(selectedOrder);
        questionList.set(questionList.indexOf(nextItem), nextItem);
 
        questionList.remove(selectedItem);
        questionList.add(--index, selectedItem);
        questionList.addToSelection(selectedItem);
 
    }
 
}

Step 8:

Now you can run the QuestionsList.zul.

image


You can download the source here

Email ThisBlogThis!Share to XShare to Facebook
Posted in ZK MVC 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