0

我正在尝试在智能 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);

}

}

说明>> 为了填充网格,我正在对服务器进行 rpc 调用,服务器返回一个列表。我迭代列表并创建一个 EmployeeListGrid 类型的对象数组(因为 EmployeeListGrid 扩展 ListGridRecord )。然后用创建的数组喂给employeegrid。

我在这里错过了什么吗,为什么网格显示空行?

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

4

1 回答 1

0

注意事项:字段名称区分大小写,因此请检查数据和字段名称之间的大小写是否不匹配,例如“parentId”与“parentID” http://forums.smartclient.com/showthread.php?t=8159#网格

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

必须使用适当的访问修饰符,即使我没有最小化代码。

于 2013-03-26T22:00:48.240 回答