I am trying to build an example for the usage of includeFrom. I want to use only client side code and "clientOnly" data sources. I'm trying to stick as close as possible to the example lines in the documentation of the "includeFrom" API.
Thanks in advance!!!
I'm trying to create a grid that shows this information:
The raw data is:
My Java code:
My testing environment:
Thanks in advance!!!
I'm trying to create a grid that shows this information:
Code:
== Expected Grid == Position | Manager Id ------------------------|---------- Chief Executive Officer | Anthony Benoit Software Developer | Chris Fisher == This is my current Grid == Position | Manager Id ------------------------|---------- Chief Executive Officer | Software Developer |
Code:
== employeeDataSource == id | Name ---|----------------- 17 | Anthony Benoit 23 | Chris Fisher == organizationDataSource == position | managerId ------------------------|---------- Chief Executive Officer | 17 Software Developer | 23
Code:
import com.google.gwt.core.client.EntryPoint; import com.smartgwt.client.data.DataSource; import com.smartgwt.client.data.DataSourceField; import com.smartgwt.client.types.FieldType; import com.smartgwt.client.widgets.grid.ListGrid; import com.smartgwt.client.widgets.grid.ListGridField; import com.smartgwt.client.widgets.grid.ListGridRecord; public class Tryout implements EntryPoint { @Override public void onModuleLoad() { /** A data source that maps employee ids to names */ DataSource employeeDataSource = defineEmployeeDataSource(); fillEmployeeDataSource(employeeDataSource); /** A data source that tells, which of the employees has which position in the organization */ DataSource organizationDataSource = defineOrganizationDataSource(); fillOrganizationDataSource(organizationDataSource); /** show data in grid */ ListGrid grid = createListGrid(); grid.setDataSource(organizationDataSource); grid.show(); } private DataSource defineEmployeeDataSource() { /* The primary key id field for employees */ final DataSourceField employeeIdDataSourceField = new DataSourceField("id", FieldType.INTEGER) {{ setPrimaryKey(true); }}; /* The name field for employees (consisting of firstname and lastname) */ final DataSourceField employeeNameDataSourceField = new DataSourceField("name", FieldType.TEXT); return new DataSource() {{ setID("employee"); setFields(employeeIdDataSourceField, employeeNameDataSourceField); setClientOnly(true); }}; } private void fillEmployeeDataSource(DataSource dataSource) { ListGridRecord record1 = new ListGridRecord() {{ setAttribute("id", 17); setAttribute("name", "Anthony Benoit"); }}; ListGridRecord record2 = new ListGridRecord() {{ setAttribute("id", 23); setAttribute("name", "Chris Fisher"); }}; dataSource.setCacheData(new ListGridRecord[] {record1, record2}); } private DataSource defineOrganizationDataSource() { // Define a primary key column with the name of the position within the organization final DataSourceField organizationPositionDataSourceField = new DataSourceField("position", FieldType.TEXT) {{ setPrimaryKey(true); }}; // Define a field for the joined name of the employee // I tried to keep as close as possible to this documentation: http://www.smartclient.com/smartgwtee/javadoc/com/smartgwt/client/docs/serverds/DataSourceField.html#includeFrom // <field name="managerName" includeFrom="employee.name" hidden="true"/> final DataSourceField organizationManagerNameDataSourceField = new DataSourceField("managerName", FieldType.TEXT) {{ // setIncludeFrom not available in API (we will set this attribute in the list grid field afterwards) setHidden(true); }}; // Define a field for the foreign key to the id of the employee // I tried to keep as close as possible to this documentation: http://www.smartclient.com/smartgwtee/javadoc/com/smartgwt/client/docs/serverds/DataSourceField.html#includeFrom // <field name="managerId" foreignKey="employee.id" displayField="managerName"/> final DataSourceField organizationManagerIdDataSourceField = new DataSourceField("managerId", FieldType.INTEGER) {{ setForeignKey("employee.id"); setDisplayField("managerName"); }}; // create new data source using the defined fields return new DataSource() {{ setID("organization"); setFields(organizationPositionDataSourceField, organizationManagerNameDataSourceField, organizationManagerIdDataSourceField); setClientOnly(true); }}; } private void fillOrganizationDataSource(DataSource dataSource) { final ListGridRecord record1 = new ListGridRecord(); record1.setAttribute("position", "Chief Executive Officer"); record1.setAttribute("managerId", 17); final ListGridRecord record2 = new ListGridRecord(); record2.setAttribute("position", "Software Developer"); record2.setAttribute("managerId", 23); dataSource.setCacheData(new ListGridRecord[] {record1, record2}); } private ListGrid createListGrid() { final ListGridField positionListGridField = new ListGridField("position"); // <field name="managerName" includeFrom="employee.name" hidden="true"/> final ListGridField managerNameListGridField = new ListGridField("managerName") {{ setIncludeFrom("employee.name"); setHidden(true); }}; // <field name="managerId" foreignKey="employee.id" displayField="managerName"/> final ListGridField managerIdListGridField = new ListGridField("managerId") {{ // setForeignKey not available in API (we have already set this attribute in the data source field) setDisplayField("managerName"); }}; return new ListGrid() {{ setFields(positionListGridField, managerNameListGridField, managerIdListGridField); setAutoFetchData(true); setWidth100(); setHeight100(); }}; } }
Code:
SmartGWT LPGL 5.0p SmartClient Version: v10.0p_2014-11-30/LGPL Development Only (built 2014-11-30) Firefox 31
Comment