1

I'm new in Richfaces framework which I'm going to use in the nearest project in my job.

I've created some simple example in which I have rich:dataTable and I'm trying to make column sorted. I've read in the tutorial ( http://docs.jboss.org/richfaces/latest_3_3_X/en/devguide/html/rich_column.html ) that everything what I need is to type "sortBy" parameter to the "rich:column" tag . Unfortunately columns can't be sort. Can anybody help me? Below source code of my simple app :

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

<f:view>
    <h:head></h:head>
    <h:body>
    <rich:panel header="Richfaces dataTable">
    <rich:dataTable value="#{TaskListBean.dataList}" var="dataItem"
    rows="4" columnClasses="50,100,100,100"
    onRowMouseOver="this.style.backgroundColor='#B5F3FB'"
    onRowMouseOut="this.style.backgroundColor='#{a4jSkin.rowBackgroundColor}'"
    width="350">

    <f:facet name="caption">
    <h:outputText value="This is the caption" />
    </f:facet>

    <f:facet name="header">
    <h:outputText value="Trouble Ticket Systems" />
    </f:facet>

    <rich:column colspan="4">
    <h:outputText value="Trouble Tickets opened" />
    </rich:column>

    <rich:column sortBy="#{dataItem.taskInstanceId}">
    <f:facet name="header">Ticket Id</f:facet>
    <h:outputText value="#{dataItem.taskInstanceId}" />
    </rich:column>

    <rich:column sortBy="#{dataItem.taskNode}">
    <f:facet name="header">Status</f:facet>
    <h:outputText value="#{dataItem.taskNode}" />
    </rich:column>

    <rich:column sortBy="#{dataItem.actorId}">
    <f:facet name="header">Actor</f:facet>
    <h:outputText value="#{dataItem.actorId}" />
    </rich:column>

    <rich:column>
    <f:facet name="header">Description</f:facet>
    <h:outputText value="#{dataItem.description}" />
    </rich:column>

    <f:facet name="footer">
    <h:outputText value="This is the footer" />
    </f:facet>

    </rich:dataTable>
    </rich:panel>
    </h:body>
</f:view>

Menaged Bean of my app:

package richfacesr;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;

@ManagedBean(name = "manager")
public class TaskListBean implements Serializable {

private ArrayList dataList;

public void loadDataList() {

    dataList = new ArrayList();

    TaskListData data1 = new TaskListData();
    data1.setTaskInstanceId(1000l);
    data1.setActorId("Willy smith");
    data1.setTaskNode("In Charge");
    data1.setDescription("CR 123456");

    TaskListData data2 = new TaskListData();
    data2.setTaskInstanceId(1001l);
    data2.setActorId("Frank Sinatra");
    data2.setTaskNode("Rejected");
    data2.setDescription("CR 654321");

    dataList.add(data1);
    dataList.add(data2);

    }

public List<TaskListData> getDataList() {
    loadDataList();
    return dataList;
    }

 }

and the object:

package richfacesr;
import java.util.ArrayList;
import java.util.List;    

public class TaskListData {
  private String taskNode;
  private long taskInstanceId;
  private String actorId;
  private String description;

  public String getActorId() {
   return actorId;
  }
  public void setActorId(String actorId) {
   this.actorId = actorId;
  }
  public String getTaskNode() {
   return taskNode;
  }
  public void setTaskNode(String currentNode) {
   this.taskNode = currentNode;
  }
  public String getDescription() {
   return description;
  }
  public void setDescription(String description) {
   this.description = description;
  }
  public long getTaskInstanceId() {
   return taskInstanceId;
  }
  public void setTaskInstanceId(long taskInstanceId) {
   this.taskInstanceId = taskInstanceId;
  }
}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
code_pretty
  • 145
  • 3
  • 15
  • Are you using JSF 2.x and Richfaces 4.X or JSF 1.X and Richfaces 3.X? I know that you posted a link to the docs of Richfaces 3, but that maybe a mistake ... – cheffe Sep 29 '13 at 20:02
  • Thanks for answer mate. I know it's other version of JSF in the tutorial but I thought they are compatible with the former one ...? I'm using JSF 2.2 and Richfaces 5.0.0 Alpha1 version. I'm working on Websphere and is super difficult to configure the project in its environment so I've created that one from Maven archetype. If you have any better archetype with the final version od Richfaces (suppose it going to be solution for my issue (?))it will be very appreciate :) – code_pretty Sep 29 '13 at 22:17
  • Are you going to use that project in production? If so, I'd suggest to rollback to Richfaces 4. Since Richfaces 5 is still _alpha_ which is not even near production stable or will this project be running for more than half a year? Then you will quite surely face the release of RF 5. – cheffe Sep 30 '13 at 05:46

1 Answers1

2

Taken from the Richfaces 4 documentation, built in sorting for dataTables does not work.

The built-in sort controls are only available with the extendedDataTable component. Support for built-in sort controls in the dataTable component will be added in a subsequent release.

It did work in Richfaces 3, but it seems that they have changed a bit, so that this feature is currently not working in Richfaces 4, out of the box.

Although you say that you use Richfaces 5, which is Alpha1, I doubt that this feature is already in there. So you will need to write some code for yourself.

Looking a the Richfaces 4 showcase about table sorting you need to

  • put your managed bean into ViewScope - otherwise the unsorted list will be created again and again
  • move your loadDataList out of getDataList() into the constuctor or annotate it with @PostConstruct - otherwise the unsorted list will be created again and again
  • make your TaskListData Serializable
  • add a sort method to your TaskListBean - do the sorting there - maybe as described in Sort ArrayList of custom Objects by property
  • check the name of your ManagedBean TaskListBean - in the code you call it manager, but in your xhtml you call it TaskListBean

Here is my tuned version of your xhtml

<f:view>
    <h:head />
    <h:body>
        <h:form>
            <rich:dataTable id="table" var="dataItem"
                value="#{manager.dataList}" width="350">

                <rich:column id="ticketId" sortBy="#{dataItem.taskInstanceId}">
                    <f:facet name="header">
                        <a4j:commandLink value="Ticket Id" render="table"
                            action="#{manager.sortBy('Ticket Id')}" />
                    </f:facet>
                    <h:outputText value="#{dataItem.taskInstanceId}" />
                </rich:column>

                <rich:column sortBy="#{dataItem.taskNode}">
                    <f:facet name="header">
                        <a4j:commandLink value="Status" render="table"
                            action="#{manager.sortBy('Status')}" />
                    </f:facet>
                    <h:outputText value="#{dataItem.taskNode}" />
                </rich:column>

                <rich:column sortBy="#{dataItem.actorId}">
                    <f:facet name="header">
                        <a4j:commandLink value="Actor" render="table"
                            action="#{manager.sortBy('Actor')}" />
                    </f:facet>
                    <h:outputText value="#{dataItem.actorId}" />
                </rich:column>

                <rich:column>
                    <f:facet name="header">
                        <a4j:commandLink value="Description" render="table"
                            action="#{manager.sortBy('Description')}" />
                    </f:facet>
                    <h:outputText value="#{dataItem.description}" />
                </rich:column>

            </rich:dataTable>
        </h:form>
    </h:body>
</f:view>

My tuned version of your TaskListBean

import java.io.Serializable;
import java.util.*;

import javax.annotation.PostConstruct;
import javax.faces.bean.*;

@ViewScoped
@ManagedBean(name="manager")
public class TaskListBean implements Serializable {

    private ArrayList<TaskListData> dataList;

    @PostConstruct
    public void loadDataList() {
        dataList = new ArrayList<TaskListData>();

        TaskListData data1 = new TaskListData();
        data1.setTaskInstanceId(1000l);
        data1.setActorId("Willy smith");
        data1.setTaskNode("In Charge");
        data1.setDescription("CR 123456");

        TaskListData data2 = new TaskListData();
        data2.setTaskInstanceId(1001l);
        data2.setActorId("Frank Sinatra");
        data2.setTaskNode("Rejected");
        data2.setDescription("CR 654321");

        dataList.add(data1);
        dataList.add(data2);

    }

    public void sortBy(String aColumn) {
        System.out.println("sort by: " + aColumn);
        // TODO sort the list by that attribute
    }

    public List<TaskListData> getDataList() {
        return dataList;
    }

}
Community
  • 1
  • 1
cheffe
  • 9,345
  • 2
  • 46
  • 57