Search code examples
smartgwt

ListGrid in smart gwt showing blank rows


I am trying to add rows to ListGrid in smart gwt. Rows are being added,but the values are not being displayed

public class SearchResultsView extends HLayout {
public SearchResultsView(EmployeeServiceAsync empServiceAsync) {
    final ListGrid employeeGrid = new ListGrid();
    employeeGrid.setWidth(500);
    employeeGrid.setHeight(224);
    employeeGrid.setShowAllRecords(true);

    ListGridField empIDField = new ListGridField("empID", "Employee Id");
    ListGridField empNameField = new ListGridField("empName",
            "Employee Name");

    employeeGrid.setFields(empIDField, empNameField);
                                // Employee[EmployeeData.getRecords().size()]);

    empServiceAsync.getRecords(new AsyncCallback<List<Employee>>() {

        @Override
        public void onSuccess(List<Employee> result) {
            System.out.println(result);
            /*
            Employee[] employee = result.toArray(new Employee[result.size()]);

            employeeGrid.setData(employee);*/
            EmployeeListGrid employeeListGrid[]=new EmployeeListGrid[result.size()];
            int counter=0;
            for (Employee employee : result) {
                employeeListGrid[counter]=new EmployeeListGrid(employee.getEmpID(), employee.getEmpName());
                System.out.println(employeeListGrid[counter].getEmpID()+">>"+employeeListGrid[counter].getEmpName());
                counter++;
            }
            employeeGrid.setData(employeeListGrid);

        }

        @Override
        public void onFailure(Throwable caught) {
            // TODO Auto-generated method stub

        }
    });

    this.addChild(employeeGrid);

}

}

Explaination>> To populate the grid I am making an rpc call to the server, the server returns a List. I iterate the list and create an array of objects of type EmployeeListGrid(as EmployeeListGrid extends ListGridRecord ). Then feed the employeegrid wuth the array created.

Have I missed something here, why is the grid showing empty rows?

Please note I have debugged and made sure that "employeeListGrid" is getting populated correctly, but the contents are not rendered.


Solution

  • Things to lookout for: Field names are case-sensitive so check for mismatched case between the data and field names, such as "parentId" vs "parentID" http://forums.smartclient.com/showthread.php?t=8159#aGrid

    class SearchResultsView extends HLayout {
        public SearchResultsView() {
            final ListGrid employeeGrid = new ListGrid();
            employeeGrid.setWidth(500);
            employeeGrid.setHeight(224);
            employeeGrid.setShowAllRecords(true);
    
            ListGridField empIDField = new ListGridField("empID", "Employee Id");
            ListGridField empNameField = new ListGridField("empName", "Employee Name");
    
            employeeGrid.setFields(empIDField, empNameField);
    
            // using a timer to simulate async callback, grid will populate after 2.5 seconds
            // place code inside run() method in onSuccess()
            Timer timer = new Timer() {
                @Override
                public void run() {
                    RecordList recordList = employeeGrid.getRecordList();
    
                    List<Employee> employeeList = getEmployeeList();
                    for (Employee employee : employeeList) {
                        recordList.add(new EmployeeRecord(employee.empID, employee.empName));
                    }
                }
            };
    
            timer.schedule(2500);
    
            this.addChild(employeeGrid);
        }
    
        private List<Employee> getEmployeeList() {
            List<Employee> employeeList = new ArrayList<Employee>();
            employeeList.add(new Employee("1", "AAA"));
            employeeList.add(new Employee("2", "BBB"));
            employeeList.add(new Employee("3", "CCC"));
            employeeList.add(new Employee("4", "DDD"));
    
            return employeeList;
        }
    }
    
    class Employee {
        String empID;
        String empName;
    
        Employee(String empID, String empName) {
            this.empID = empID;
            this.empName = empName;
        }
    }
    
    class EmployeeRecord extends ListGridRecord {
    
        EmployeeRecord(String empID, String empName) {
            setAttribute("empID", empID);
            setAttribute("empName", empName);
        }
    }
    

    Its possible to use ListGrid.setData() as given below.

    List<Employee> employeeList = getEmployeeList();
    
    if (employeeList != null && employeeList.size() > 0) {
        EmployeeRecord[] employeeRecords = new EmployeeRecord[employeeList.size()];
        for (int i = 0; i < employeeList.size(); i++) {
            employeeRecords[i] = new EmployeeRecord(employeeList.get(i).empID, employeeList.get(i).empName);
        }
    
        employeeGrid.setData(employeeRecords);
    }
    

    EmployeeRecord (EmployeeListGrid) class might not be required, unless there's enough reasons to do so. I've used it to reduce differences between your code and mine.

    Proper access modifiers must be used, even though I haven't to minimize the code.