Application Techniques



Manipulating Lists

How to enable users to move selected items from one list to another on a page.

About this technique

Details

Category

HTML Client Techniques> Basic techniques

Description

You'll learn about:

You can run this technique code from:

NOTE   First make sure that database is running on your localhost SilverStream Server

Related reading

See the chapter on page basics in the Programmer's Guide

The example provides buttons to move highlighted items between the deselected-item list on the left and the selected-item list on the right. Another button displays the items from the selected-item list in an alert dialog box.

Adjusting an empty list's size   Top of page

The following code shows how to add a temporary item to the Selected list if no items have been selected. This allows the list to be displayed in its regular size, as if there were items in it. This method runs immediately after the page is first loaded, as well as at the beginning of each page request.

  protected void pageRequestBegin(AgiHttpServletRequest req, AgiHttpServletResponse res) throws Exception 
  { 
     // If the 'right' list has no rows, we need to add a temporary row so that the 
     // width of the list looks ok. 
     if (right.getItemCount() == 0) 
        right.add("No employee selected", new Integer(-1)); 
  } 

Notes about the code

Moving items to the selected list   Top of page

The following code shows how to move one or more selected items in the left list to the Selected list. For each selected item, the code uses the (left) list's index to get the item, and (after it has added the item to the Selected list) delete the item from the left list.

  private void handle_btnSelect_pageActionPerformed(ActionEvent evt) throws Exception 
  { 
     // Add each selected employee to the 'right' list and remove them from 
     // the 'left' list. 
     int index = left.getSelectedIndex(); 
     while (index >= 0) 
     { 
        // We need to check to see if the 'No employee selected' entry is still 
        // in the 'right' list and, if it is, remove it. 
        int itemValue = ((Integer)right.getItemValue(0)).intValue(); 
        if (itemValue == -1) 
           right.remove(0); 
   
        String item = left.getItem(index); 
        itemValue = ((Integer)left.getItemValue(index)).intValue(); 
   
        right.add(item, new Integer(itemValue)); 
   
        left.remove(index); 
   
        index = left.getSelectedIndex(); 
     } 
  } 

Notes about the code

Moving items to the deselected list   Top of page

The following code gets the first selected item in the Selected list, then uses a while loop to add each to the left list then remove it from the Selected list. (The code is similar to that of moving items from the left list to the Selected list.) After removing all the selected items, it checks if there are any items left in the Selected list. If there are none, it adds a temporary item to it, identifying it by the value 0.

  private void handle_btnDeselect_pageActionPerformed(ActionEvent evt) throws Exception 
  { 
     // Remove the selected employees from the 'right' list and add 
     // them back to the 'left' list. 
     int index = right.getSelectedIndex(); 
     while (index >= 0) 
     { 
        String item = right.getItem(index); 
        int itemValue = ((Integer)right.getItemValue(index)).intValue(); 
         
        left.add(item, new Integer(itemValue)); 
        right.remove(index); 
         
        index = right.getSelectedIndex(); 
     } 
      
     // If the 'right' list now has no rows, we need to add a temporary row so that the 
     // width of the list looks ok. 
     if (right.getItemCount() == 0) 
        right.add("No employee selected", new Integer(-1)); 
  } 

Notes about the code

Displaying selected items as text in a dialog box   Top of page

The following code loops through all the items in the Selected list and appends each item as a text string to a string buffer. It displays the text in an Alert dialog by calling agScriptHelper.alert().

  private void handle_btnDisplay_pageActionPerformed(ActionEvent evt) throws Exception 
  { 
     StringBuffer sb = new StringBuffer("You selected: "); 
      
     int i = right.getItemCount(); 
     if (i == 1) 
     { 
        if (((Integer)right.getItemValue(0)).intValue() == -1) 
        { 
           agScriptHelper.alert("You have not selected anyone!"); 
           return; 
        } 
     } 
     for (int j = 0; j < i; j++) 
     { 
        if (j > 0) 
           sb.append(", "); 
        sb.append(right.getItem(j)); 
     } 
     agScriptHelper.alert(sb.toString()); 
  } 

Notes about the code






Copyright © 2000, SilverStream Software, Inc. All rights reserved.