I have a grid listing employees and there is a combbox in one column contains the department. A department as an id and a name. The name is diplayed but the id is what needs to be used as information sent to/from server pertaining to the employee. I have done this by creating a Map in the EmployeeDS keyed by departmentId.
DataSourceTextField department = new DataSourceTextField( Constants.DEPARTMENT_ID, "Department");
Map<String, String> departmentMap = new LinkedHashMap<String, String>();
departmentMap.put( "4", "AWD" );
departmentMap.put( "14", "Bottle Washer" );
departmentMap.put( "6", "Desktop" );
departmentMap.put( "3", "FANWeb" );
departmentMap.put( "13", "Help Desk" );
departmentMap.put( "7", "IRM" );
departmentMap.put( "11", "Purchasing" );
departmentMap.put( "5", "TA2000" );
departmentMap.put( "1", "Technology Office" );
department.setValueMap( departmentMap );
I did this a a quick workaround for this evaluation. I now have a DepartmentDS which has an departmentId and departmentName fields. I tried creating a SelectItem object in my grid and associating that to the grid field item but that allows me to show the departmentId but not the name.
What is the best way to handle this? I basically want to load a Map with data from my DepartmentDS. Looking for the best practice.
Thanks!
Take a look at optionDataSource, it’s designed for exactly this.
OK, I’m getting very close. BTW, I was using setOptionDataSource but was not using the sets DisplayField & ValueField. The following creates a ComboBox in my grid but the departmentId is displayed until I click the dropdown and then the departmentName list is displayed. When I click off, then it reverts back to the departmentId. I’m not getting why.
BTW, departmentId is a ListGridField in my employee grid.
SelectItem departmentSelectItem = new SelectItem();
departmentSelectItem.setAutoFetchData( true );
departmentSelectItem.setDisplayField( Constants.DEPARTMENT_NAME );
departmentSelectItem.setValueField( Constants.DEPARTMENT_ID );
departmentSelectItem.setOptionDataSource( DepartmentDS.getInstance() );
departmentId.setEditorType( departmentSelectItem );
The DS constructor is:
public DepartmentDS( String id )
{
setID( id );
setDataFormat( DSDataFormat.XML );
setRecordXPath( "/departmentList/departments/Row" );
DataSourceField departmentName = new DataSourceField( Constants.DEPARTMENT_NAME,
FieldType.TEXT );
DataSourceField departmentId = new DataSourceField( Constants.DEPARTMENT_ID,
FieldType.TEXT );
setFields( departmentId, departmentName );
setDataURL( BaseUrl.getUrl() + "departmentList?cz=702110804131902" );
}
Thanks!
Also, I want the fetch to occur just once.
More info needed, of course.
Actually the problem is in the code you posted. You are setting an optionDataSource and related properties for the editor only, so it applies only while editing. Use ListGridField.setOptionDataSource() (as well as setValueField / setDisplayField on ListGridField) to cause the optionDataSource to be used by the ListGridField.
Note the docs linked above provide a good overview of the different possible approaches for handling dynamic & large valueMaps.