Working with GXT(Ext GWT) Grid : Add Grouping Funtionality

Grouping is very helpful when you view a large set of data. It creates a more readable view for the user. GXT provides us with a very easy way of adding grouping functionality with a grid. Here i describe how to add grouping functionality with GXT grid.

Create a new project named GxtGridGrouping and add GXT library in the project. As like my previous blogs about GXT Grid i have used the same Employee model and TestData class for generating data. You will find the code of these classes here. Go to the onModuleLoad method of GxtGridGrouping class and remove all the auto generated code of this method.

First create an instance of GroupingStore, a specialized store implementation which extends the ListStore that provides for grouping models by one of the available fields.

GroupingStore<Employee> employeeList = new GroupingStore<Employee>();  

The groupBy method takes a property of your model by which you want to group your grid. In the example i have grouped the grid by 'department' property. You can change it to 'designation' property and see how it works.

Now define a list of ColumnConfig and a ColumnModel by using this list as before.
List<Columnconfig> configs = new ArrayList<Columnconfig>();

ColumnConfig column = new ColumnConfig();  
column.setHeader("Employee Name");  

column = new ColumnConfig("department", "Department", 150);  

column = new ColumnConfig("designation", "Designation", 150);   

column = new ColumnConfig("salary", "Slary", 100);  
final NumberFormat number = NumberFormat.getFormat("0.00");  
GridCellRenderer<Employee> checkSalary = new GridCellRenderer<Employee>() {  
public String render(Employee model, String property, ColumnData config, int rowIndex,  
int colIndex, ListStore<Employee> employeeList, Grid<Employee> grid) {  
double val = (Double) model.get(property);  
String style = val < 70000 ? "red" : "green";  
return "<span style='color:" + style + "'>" + number.format(val) + "</span>"; 

column = new ColumnConfig("joiningdate", "Joining Date", 100);  

final ColumnModel cm = new ColumnModel(configs); 

Create an instance of GroupView which groups the data based on the GroupStore. Then set it as the view of the Grid.

GroupingView view = new GroupingView();  
view.setGroupRenderer(new GridGroupRenderer() {  
public String render(GroupColumnData data) {  
String f = cm.getColumnById(data.field).getHeader();  
String l = data.models.size() == 1 ? "Item" : "Items";  
return f + ": " + + " (" + data.models.size() + " " + l + ")";  

Grid<Employee> grid = new Grid<Employee>(employeeList, cm);  

Here setForceFit is set true to auto expand the columns to fit the grid width and setGroupRenderer is used to create a heading for the groups.

Finally add the grid to the content panel.

ContentPanel cp = new ContentPanel();  
cp.setHeading("Employee List");  
cp.setLayout(new FitLayout());  
cp.setSize(700, 420); 

Run the application and see how readable the grid is now. To view the live demo of this tutorial Click here.


Anonymous June 3, 2010 at 11:56 AM  

Nice post.

You take care of explaining the example codes found in, so we can get a better understanding of it :)

Now I got a question: Is it possible to group the grid by *multiple* columns?

Example: If I wanted to group the results with the same FIRST and LAST name, would it be possible?

Unknown June 3, 2010 at 11:33 PM  

According to your requirement I have tried to group with multiple columns. But failed :(. I have searched through the gxt forum but got no solution. Probable gxt doesn't support multiple columns grouping..

Anonymous June 16, 2015 at 7:32 AM  

Just in case anyone lands here and has the same question that Anonymous had before:

It is posible to do the trick and group by multiple columns. I've done so by creating a fake attribute that is the concatenation of those columns. Then, make the grid group by that attribute and set the groupingView groupRenderer with what you want (e.g, "First and Last name")

Total Pageviews


Twitter Updates
    follow me on Twitter