RichFaces built-in sorting – via external contros

Continuing the sorting series, let’s see how to use external controls to sort the 7 New Wonders of the World. In the first post we used the column headers, now we are going to use buttons outside the table for sorting. Let’s first look at changes to the page.


   
      
	
	    Name
	    
	
	
	      Location
	      
	
	
	   Image
	   
	
	
   
      
	
	
       
       
       
       
   


For the two columns (name, location) we want to sort, we add two attributes. selfSorted=â€?falseâ€? means that sorting will be done by external controls (we can’t click on the column headers anymore). sortOrder is bound to an bean property that sets the sort order to either ascending or descending.

Next to the table, two radio buttons let select column to sort by and the buttons to perform sorting.

screenshot022.png

Let’s see how the updated managed bean looks. First we need to add three properties:

...
private String nameDirection = Ordering.UNSORTED.name();
private String locationDirection = Ordering.UNSORTED.name();
private String column;
...
// getter for nameDirection and locationDirection
// getter and setter for column

column will be set to the column by which we want to sort. Based on the column selected, we will change nameDirection or locationDirection to ascending or descending value. One last thing is the sorting methods as shown next:

public void sortAsc (ActionEvent event){
   if ( column.equals("location"))
      this.locationDirection = Ordering.ASCENDING.name();
   else
      this.nameDirection = Ordering.ASCENDING.name();
}
public void sortDesc (ActionEvent event){
   if ( column.equals("location"))
      this.locationDirection = Ordering.DESCENDING.name();
   else
      this.nameDirection = Ordering.DESCENDING.name();
}

screenshot016.png

4 thoughts on “RichFaces built-in sorting – via external contros

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s