Using the ajaxKeys attribute for <rich:dataList>

The following simple example demonstrates how to update a price for a specific car from the list.

Here is the code:

 

<h:form>
     <h:selectOneMenu id="select" value="#{carBean.currentItem}">

          <f:selectItems value="#{carBean.selectsList}"/>

     </h:selectOneMenu>

     <h:inputText id="newprice" value="#{carBean.newPrice}" />


     <a4j:commandButton action="#{carBean.updateCarPrice}" reRender="price" value="Submit"/>

     <rich:dataList var="car" value="#{carBean.listOfCars}" id="list" 
          rows="6" type="disc" ajaxKeys="#{carBean.keyToUpdate}">
          

          <h:outputText value="#{car.make} #{car.model}"/><br/>

          <h:outputText value="Price: " styleClass="label" />
          <h:outputText id="price" value="#{car.price}" />

     </rich:dataList>
                    

</h:form>

 

There is no point in updating the whole component to change a price for a single

car. Using the ajaxKeys attribute we can choose which item to update.

 

The car model contains the next properties: a car title, model and price.

Car.java :

 

 public class Car {
      private String make;

      private String model;

      private BigDecimal price = new BigDecimal(0);

     
      public Car() {}
 
      public Car(String make, String model, BigDecimal price) {
          this.make = make;
          this.model = model;
          this.price = price;
      }
 
     /* --- getters and setters ----*/

}

 

dataList1.png

In the example you see a list of cars. To change a price for a car, choose in the select menu the car which you want to re-set the price for, type a new price in the input field and press the Sumbmit button to update the value.

 

In the example, the <a4j:commandButton/> has the reRender attribute bound to the <h:outputText/> that outputs the price using the id attribute. While the ajaxKeys of  <rich:dataList/>  points to the list item to be updated.

 

The bean used in the example looks like this:


public class CarBean {
 
      private  List<Car> listOfCars;
      private BigDecimal newPrice;
      public List<SelectItem> selectsList = new ArrayList<SelectItem>();
      private Integer currentItem;
      private Set<Integer> keyToUpdate;

 
      public CarBean(){
           keyToUpdate = new HashSet<Integer>();

           listOfCars = new ArrayList<Car>();
           listOfCars.add( new Car("Chevrolet", "Corvette", new BigDecimal(18500)));

           listOfCars.add( new Car("Chevrolet", "Malibu", new BigDecimal(20500)));

           listOfCars.add( new Car("Chevrolet", "Tahoe", new BigDecimal(17890)));
           listOfCars.add( new Car("Ford", "Taurus", new BigDecimal(18580)));
           listOfCars.add( new Car("Ford", "Explorer", new BigDecimal(21540)));
           listOfCars.add( new Car("Nissan", "Maxima", new BigDecimal(15900)));

           SelectItem item;
           for(Car car : listOfCars){

                 item = new SelectItem(listOfCars.indexOf(car), car.getMake() + "
               " + car.getModel());
                 selectsList.add(item);
           }
      }


public void updateCarPrice(){
            listOfCars.get(currentItem).setPrice(newPrice);
            keyToUpdate = Collections.singleton(currentItem);

      }
 
      /* --- getters and setters ----*/
 
}   

 

 

 

In the example we use ajaxKeys to update  only one item. Actually, it’s possible to update multiple items: ajaxKeys contains an Object (java.util.Set) that holds keys of all items needed to be updated after an Ajax request.