RichFacesCookbook/ScrollableDataTableSelectionUsage

Here is a forum post discussing scrollableDataTable issues with selecting rows: http://www.jboss.com/index.html?module=bb&op=viewtopic&t=122543

 

Below there is a simple example of how you can use the "selection" attribute in order to get row selection in <rich:scrollableDataTable>.

 

Example xhtml file:

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:a4j="http://richfaces.org/a4j"
      xmlns:rich="http://richfaces.org/rich">

        <h:form>
            <rich:spacer height="30" ></rich:spacer>
            <rich:scrollableDataTable rowKeyVar="rkv" frozenColCount="1" height="400px" 
                width="700px" id="carList" rows="40" columnClasses="col"
                value="#{dataTableScrollerBean.allCars}" var="category" sortMode="single"
                selection="#{dataTableScrollerBean.selection}">
                <rich:column id="make">
                    <f:facet name="header"><h:outputText styleClass="headerText" value="Make" ></h:outputText></f:facet>
                    <h:outputText value="#{category.make}" ></h:outputText>
                </rich:column>
                <rich:column id="model">
                    <f:facet name="header"><h:outputText styleClass="headerText" value="Model" ></h:outputText></f:facet>
                    <h:outputText value="#{category.model}" ></h:outputText>
                </rich:column>
                <rich:column id="price">
                    <f:facet name="header"><h:outputText styleClass="headerText" value="Price" ></h:outputText></f:facet>
                    <h:outputText value="#{category.price}" ></h:outputText>
                </rich:column>
                <rich:column id="mileage">
                    <f:facet name="header"><h:outputText styleClass="headerText" value="Mileage" ></h:outputText></f:facet>
                    <h:outputText value="#{category.mileage}" ></h:outputText>
                </rich:column>
                <rich:column width="200px" id="vin">
                    <f:facet name="header"><h:outputText styleClass="headerText" value="VIN" ></h:outputText></f:facet>
                    <h:outputText value="#{category.vin}" ></h:outputText>
                </rich:column>
                <rich:column id="stock">
                    <f:facet name="header"><h:outputText styleClass="headerText" value="Stock" ></h:outputText></f:facet>
                    <h:outputText value="#{category.stock}" ></h:outputText>
                </rich:column>
            </rich:scrollableDataTable>
            <rich:spacer height="20px"></rich:spacer>
            <a4j:commandButton value="Show Current Selection" reRender="table" 
                action="#{dataTableScrollerBean.takeSelection}" 
                oncomplete="javascript:Richfaces.showModalPanel('panel');"></a4j:commandButton>
        </h:form>
        <rich:modalPanel id="panel" autosized="true">
                <f:facet name="header">
                    <h:outputText value="Selected Rows"></h:outputText>
                </f:facet>
                <f:facet name="controls">
                    <span style="cursor:pointer" onclick="javascript:Richfaces.hideModalPanel('panel')">X</span>
                </f:facet>
            <rich:dataTable value="#{dataTableScrollerBean.selectedCars}" var="sel" id="table">
                <rich:column>
                    <f:facet name="header"><h:outputText value="Make" ></h:outputText></f:facet>
                    <h:outputText value="#{sel.make}" ></h:outputText>
                </rich:column>
                <rich:column id="model">
                    <f:facet name="header"><h:outputText value="Model" ></h:outputText></f:facet>
                    <h:outputText value="#{sel.model}" ></h:outputText>
                </rich:column>
                <rich:column id="price">
                    <f:facet name="header"><h:outputText value="Price" ></h:outputText></f:facet>
                    <h:outputText value="#{sel.price}" ></h:outputText>
                </rich:column>
                <rich:column id="mileage">
                    <f:facet name="header"><h:outputText value="Mileage" ></h:outputText></f:facet>
                    <h:outputText value="#{sel.mileage}" ></h:outputText>
                </rich:column>
                <rich:column id="stock">
                    <f:facet name="header"><h:outputText value="Stock" ></h:outputText></f:facet>
                    <h:outputText value="#{sel.stock}" ></h:outputText>
                </rich:column>
            </rich:dataTable>
        </rich:modalPanel>
</ui:composition>

 

-


 

Example bean:

 

/**
 * 
 */
package org.richfaces.datatablescroller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import org.richfaces.demo.datafilterslider.DemoInventoryItem;
import org.richfaces.model.ScrollableTableDataModel.SimpleRowKey;
import org.richfaces.model.selection.SimpleSelection;

/**
 * @author Nick Belaevski - nbelaevski@exadel.com
 * created 02.03.2007
 * 
 */
public class DataTableScrollerBean {
     private SimpleSelection selection = new SimpleSelection();
     private ArrayList<DemoInventoryItem> selectedCars = new ArrayList<DemoInventoryItem>(); 
     private static int DECIMALS = 1;
     private static int ROUNDING_MODE = BigDecimal.ROUND_HALF_UP;

     private List <DemoInventoryItem> allCars = null;

     public List <DemoInventoryItem> getAllCars() {
          synchronized (this) {
               if (allCars == null) {
                    allCars = new ArrayList<DemoInventoryItem>();
                    for (int k = 0; k <= 5; k++) {
                         try{
                              switch (k) {
                              case 0:
                                   allCars.addAll(createCar("Chevrolet","Corvette", 5));
                                   allCars.addAll(createCar("Chevrolet","Malibu", 8));
                                   allCars.addAll(createCar("Chevrolet","S-10", 10));
                                   allCars.addAll(createCar("Chevrolet","Tahoe", 6));
                                   break;

                              case 1:
                                   allCars.addAll(createCar("Ford","Taurus", 12));
                                   allCars.addAll(createCar("Ford","Explorer", 11));
                                   break;
                              case 2:
                                   allCars.addAll(createCar("Nissan","Maxima", 9));
                                   break;
                              case 3:
                                   allCars.addAll(createCar("Toyota","4-Runner", 7));
                                   allCars.addAll(createCar("Toyota","Camry", 15));
                                   allCars.addAll(createCar("Toyota","Avalon", 13));
                                   break;
                              case 4:
                                   allCars.addAll(createCar("GMC","Sierra", 8));
                                   allCars.addAll(createCar("GMC","Yukon", 10));
                                   break;
                              case 5:
                                   allCars.addAll(createCar("Infiniti","G35", 6));
                                   break;
                              /*case 6:
                                   allCars.addAll(createCar("UAZ","469", 6));
                                   break;*/
                              default:
                                   break;
                              }
                         }catch(Exception e){
                              System.out.println("!!!!!!loadAllCars Error: " + e.getMessage());
                              e.printStackTrace();
                         }
                    }
               }
          }

          return allCars;
     }

     public int genRand() {
          return rand(1,10000);
     }

     public List <DemoInventoryItem> createCar(String make, String model, int count){

          ArrayList <DemoInventoryItem> iiList = null;

          try{
               int arrayCount = count;

               DemoInventoryItem[] demoInventoryItemArrays = new DemoInventoryItem[arrayCount];

               for (int j = 0; j < demoInventoryItemArrays.length; j++){
                    DemoInventoryItem ii = new DemoInventoryItem();

                    ii.setMake(make);
                    ii.setModel(model);
                    ii.setStock(randomstring(6,7));
                    ii.setVin(randomstring(14,15));
                    ii.setMileage(new BigDecimal(rand(5000,80000)).setScale(DECIMALS, ROUNDING_MODE));
                    ii.setMileageMarket(new BigDecimal(rand(25000,45000)).setScale(DECIMALS, ROUNDING_MODE));
                    ii.setPrice(new Integer(rand(15000,55000)));
                    ii.setPriceMarket(new BigDecimal(rand(15000,55000)).setScale(DECIMALS, ROUNDING_MODE));
                    ii.setDaysLive(rand(1,90));
                    ii.setChangeSearches(new BigDecimal(rand(0,5)).setScale(DECIMALS, ROUNDING_MODE));
                    ii.setChangePrice(new BigDecimal(rand(0,5)).setScale(DECIMALS, ROUNDING_MODE));
                    ii.setExposure(new BigDecimal(rand(0,5)).setScale(DECIMALS, ROUNDING_MODE));
                    ii.setActivity(new BigDecimal(rand(0,5)).setScale(DECIMALS, ROUNDING_MODE));
                    ii.setPrinted(new BigDecimal(rand(0,5)).setScale(DECIMALS, ROUNDING_MODE));
                    ii.setInquiries(new BigDecimal(rand(0,5)).setScale(DECIMALS, ROUNDING_MODE));
                    demoInventoryItemArrays[j] = ii;

               }

               iiList = new ArrayList<DemoInventoryItem>(Arrays.asList(demoInventoryItemArrays));

          }catch(Exception e){
               System.out.println("!!!!!!createCategory Error: " + e.getMessage());
               e.printStackTrace();
          }
          return iiList;
     }

     public static int rand(int lo, int hi)
     {
          Random rn2 = new Random();
          //System.out.println("**" + lo);
          //System.out.println("**" + hi);
          int n = hi - lo + 1;
          int i = rn2.nextInt(n);
          return lo + i;
     }

     public static String randomstring(int lo, int hi)
     {
          int n = rand(lo, hi);
          byte b[] = new byte[n];
          for (int i = 0; i < n; i++)
               b[i] = (byte)rand('A', 'Z');
          return new String(b);
     }

     public SimpleSelection getSelection() {
          return selection;
     }

     public void setSelection(SimpleSelection selection) {
          System.out.println("Setting Started");
          this.selection = selection;
          System.out.println("Setting Complete");
     }
     
     public String takeSelection() {
          getSelectedCars().clear();
          Iterator<SimpleRowKey> iterator = getSelection().getKeys();
          while (iterator.hasNext()){
               SimpleRowKey key = iterator.next();
               getSelectedCars().add(getAllCars().get(key.intValue()));
          }
          return null;
     }

     public ArrayList<DemoInventoryItem> getSelectedCars() {
          return selectedCars;
     }

     public void setSelectedCars(ArrayList<DemoInventoryItem> selectedCars) {
          this.selectedCars = selectedCars;
     }
}

 

You can also see the working example of <rich:scrollableDataTable> usage Here