SmartGWT TreeGrid数据源由嵌套数组JSON填充

时间:2011-07-08 23:43:43

标签: json gwt datasource smartgwt treegrid

我尝试过与示例所说的相同但使用JSON而不是XML

Smart GWT Tree

当我使用XML时,它可以很好地工作,但是当我使用JSON时它只会加载第一个孩子。

这是XML:

<response>
<errors />
<data>
    <project>
        <id>3</id>
        <name>Checho Project</name>
        <sprints>
            <project>
                <id>4</id>
                <name>Backlog</name>
            </project>
        </sprints>
    </project>
</data>
</response>

这是JSON:

{"response":
   {"errors":"","data":
     {"project":[{"id":1,"name":"Bee Project","sprints":
                                                 {"project":[{"id":3,"name":"sprint3"},{"id":4,"name":"sprint2"},{"id":2,"name":"sprint1"}]}},
                    {"id":5,"name":"Checho Project","sprints":
                                                  {"project":{"id":6,"name":"Backlog"}}}]
     }
   }
}

private ProjectDS(String id) {

    setID(id);
    setRecordXPath("/response/data/*");
    // setRecordXPath("/Employees/*");
    // DataSourceTextField nameField = new DataSourceTextField("Name", "Name", 128);
    //
    // DataSourceIntegerField employeeIdField = new DataSourceIntegerField("EmployeeId", "Employee ID");
    // employeeIdField.setPrimaryKey(true);
    // employeeIdField.setRequired(true);

    DataSourceTextField idField = new DataSourceTextField("id", "Id", 128, true);
    idField.setHidden(true);
    idField.setPrimaryKey(true);

    DataSourceTextField itemNameField = new DataSourceTextField("name", "Item", 128, true);

    DataSourceField reportsToField = new DataSourceField();
    reportsToField.setName("sprints");
    reportsToField.setChildrenProperty(true);

    setFields(idField, itemNameField, reportsToField);

    setDataFormat(DSDataFormat.JSON);
    setDataURL("bee_gui/sc/ds.xml");
    setClientOnly(true);

}

有没人处理过这个问题?

谢谢!

2 个答案:

答案 0 :(得分:0)

我使用“Javascript Overly Types”进行我的JSON解析

如果您想了解更多信息,请查看此处:http://googlewebtoolkit.blogspot.com/2008/08/getting-to-really-know-gwt-part-2.html

他们很容易做好工作!

PS:讽刺,我无法帮助你解决你的榜样......

答案 1 :(得分:0)

以这种方式使用JSON对象,然后您将获得解决方案

{"List":
    {"employee":
        [{"EmployeeId":4,"ReportsTo":1,"Name":"Seg 1", "Job":"Chief Operating Officer","Salary":260000},{"EmployeeId":192,"ReportsTo":4,"Name":"Leg 1","Job":"Mgr Cap Rptg Dist","Salary":460000},{"EmployeeId":5,"ReportsTo":1,"Name":"Seg 2", "Job":"Chief Operating Officer","Salary":260000},{"EmployeeId":193,"ReportsTo":5,"Name":"Leg 2","Job":"Mgr Cap Rptg Dist","Salary":460000}]
    }
}