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>(); employeeList.add(TestData.getEmployees()); employeeList.groupBy("department");
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.setId("name"); column.setHeader("Employee Name"); column.setWidth(200); configs.add(column); column = new ColumnConfig("department", "Department", 150); column.setAlignment(HorizontalAlignment.LEFT); configs.add(column); column = new ColumnConfig("designation", "Designation", 150); column.setAlignment(HorizontalAlignment.LEFT); configs.add(column); column = new ColumnConfig("salary", "Slary", 100); column.setAlignment(HorizontalAlignment.RIGHT); 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.setRenderer(checkSalary); configs.add(column); column = new ColumnConfig("joiningdate", "Joining Date", 100); column.setAlignment(HorizontalAlignment.RIGHT); column.setDateTimeFormat(DateTimeFormat.getShortDateFormat()); configs.add(column); 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.setForceFit(true); 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.group + " (" + data.models.size() + " " + l + ")"; } }); Grid<Employee> grid = new Grid<Employee>(employeeList, cm); grid.setView(view); grid.setBorders(true);
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.setBodyBorder(false); cp.setHeading("Employee List"); cp.setButtonAlign(HorizontalAlignment.CENTER); cp.setLayout(new FitLayout()); cp.setSize(700, 420); cp.add(grid); RootPanel.get().add(cp);
Run the application and see how readable the grid is now. To view the live demo of this tutorial Click here.