Use Database for Form and Grid

Hi,

I want to use a JsonDataSource to bind Data to the Form[1] and to a Grid[2] at the same time.
The json-string:

{"command":{
"firstname":"Steve",
"lastname":"Miller",
"table":[
{"id":"1","data":"15-01-2009","sport":"Soccer","color":"ff0000"},
{"id":"2","data":"15-01-2009","sport":"Cricket","color":"00ff00"},
{"id":"3","data":"15-01-2009","sport":"Rugby","color":"0000ff"}
]
}

Now I want to bind the firstname and the lastname property to a form
and the table property to a grid.

The database is defined like following Code Block.

And here the Problem:
if “dataSource.setRecordXPath(”/command/table");" exists the table values are displayed and the form values are not displayed. If not the form-values are displayed and the table values are not displayed.

Is there a way to use the DataSource for the Form and the Grid at the same time?

Regards
Kai

The DataSource:

DataSource dataSource = new DataSource();
dataSource.setDataFormat(DSDataFormat.JSON);
dataSource.setDataURL("/test/hello.json");
/*
* If next line exists the table values are displayed and the formvalues not
* If next line is removed the form-values are displayed and the table values not.
*/ 
dataSource.setRecordXPath("/command/table");  

/*
* FormFelds
*/
DataSourceTextField firstnameField = new DataSourceTextField( "firstname", "Firstname");
firstnameField.setValueXPath("/command/firstname");

DataSourceTextField lastnameField = new DataSourceTextField( "lastname", "Lastname");
lastnameField.setValueXPath("/command/lastname");

/*
* GridFelds
*/
DataSourceTextField sportField = new DataSourceTextField( "sport", "Sport");

DataSourceTextField dataField = new DataSourceTextField( "data", "Data");

DataSourceTextField colorField = new DataSourceTextField( "color", "Color");

dataSource.setFields(new DataSourceField[] { firstnameField, lastnameField, sportField, dataField, colorField });

The form is defined like

final DynamicForm preferencesForm = new DynamicForm();
preferencesForm.setDataSource(dataSource);

TextItem lastname = new TextItem();
lastname.setTitle("Lastname");  
lastname.setName("lastname");

TextItem firstname = new TextItem();
firstname.setTitle("Firstname");  
firstname.setName("firstname");

preferencesForm.setFields(firstname, lastname);
preferencesForm.setAutoFetchData(true);

The grid is defined like

final ListGrid countryGrid = new ListGrid();
countryGrid.setShowAllRecords(true);
countryGrid.setDataSource(dataSource);
countryGrid.setAutoFetchData(true);

ListGridField sportField = new ListGridField("sport", "Sport");  
ListGridField dataField = new ListGridField("data", "Data");  
ListGridField colorField = new ListGridField("color", "Color");  

countryGrid.setFields(new ListGridField[] {sportField, dataField, colorField});

If you select the top-level element (“command”) the subelements are there in the dataset you get back from the DataSource, and could be used with a related form. See this thread for more context.

Yes, the thread seams to describe my case. But I did not understand the circumstance.
How can I select the top-level element (“command”) ?
Is it possible to split the database some how ?

I got a bit further:

It seems I can set the top-level element(“command”)

dataSource.setRecordXPath("/command");

In the thread you posted you wrote:

This could solve my problem, But how can I “take the Array of items from the “order” record” in my case?

Look at the records you get back. The nested records are there as an attribute.

So can I call the fetchData(Criteria criteria, DSCallback callback) method of the database and implement a DSCallback were I add the data to the Form and the Grid ?
Is there a alternative?

Like what?

for example splitting a database into two new databases.