智能gwt中的ListGrid显示空白行

时间:2013-03-24 15:43:22

标签: smartgwt

我正在尝试向智能gwt中的ListGrid添加行。正在添加行,但未显示值

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);

}

}

释&GT;&GT; 为了填充网格,我正在对服务器进行rpc调用,服务器返回一个List。 我迭代列表并创建一个EmployeeListGrid类型的对象数组(因为EmployeeListGrid扩展了ListGridRecord)。然后输入数组创建的employeegrid wuth。

  

我在这里遗漏了什么,为什么网格显示空行?

请注意我已经调试并确保“employeeListGrid”正在正确填充,但内容未呈现。

1 个答案:

答案 0 :(得分:0)

需要注意的事项:     字段名称区分大小写,因此请检查数据和字段名称之间的不匹配大小写,例如“parentId”与“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);
    }
}

可以使用下面给出的ListGrid.setData()。

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)类,除非有足够的理由这样做。 我用它来减少代码和我的代码之间的差异。

必须使用正确的访问修饰符,即使我不必最小化代码。