Hello,
I am having some trouble using a SelectItem editor backed by an option data source to add a listgrid record with both a data value and a display value.
I have an editable listgrid with one of the fields requiring a databound dependant select. The field has both a human readable text value and a numeric id. I want the text value to be displayed in the grid and picklist and the id to be stored in a hidden field in the record.
I can make this work by setting the option data source on the field directly and also by setting a Value Map on the editor. Neither of these methods are suitable for me because I have a large data set than needs to be filtered by other fields in the record, so I am using a SelectItem editor backed by an option data source.
The problem is that whem I use an option datasource with the SelectItem editor, the numeric id field is stored in both the name and id fields of the list grid record. I would expect the numeric id to be stored in the id field and the human readable value to be diplayed in the name field (i.e. the behaviour seen when setting a static value map on the editor).
I have looked at the showcase examples and I don't think they cover my specific use case. Databound select items in the examples seem to return and store only the display value.
I am using SmartGwt 2.5 and GWT 2.3.
Here is a complete code sample showing what I have tried. I have also shown developer console output showing the difference between Option Data Source and Value Map approaches.
Thanks a lot.
Option data source console output:
Value map console output:
I am having some trouble using a SelectItem editor backed by an option data source to add a listgrid record with both a data value and a display value.
I have an editable listgrid with one of the fields requiring a databound dependant select. The field has both a human readable text value and a numeric id. I want the text value to be displayed in the grid and picklist and the id to be stored in a hidden field in the record.
I can make this work by setting the option data source on the field directly and also by setting a Value Map on the editor. Neither of these methods are suitable for me because I have a large data set than needs to be filtered by other fields in the record, so I am using a SelectItem editor backed by an option data source.
The problem is that whem I use an option datasource with the SelectItem editor, the numeric id field is stored in both the name and id fields of the list grid record. I would expect the numeric id to be stored in the id field and the human readable value to be diplayed in the name field (i.e. the behaviour seen when setting a static value map on the editor).
I have looked at the showcase examples and I don't think they cover my specific use case. Databound select items in the examples seem to return and store only the display value.
I am using SmartGwt 2.5 and GWT 2.3.
Here is a complete code sample showing what I have tried. I have also shown developer console output showing the difference between Option Data Source and Value Map approaches.
Thanks a lot.
Code:
private static final String NAME = "name";
private static final String ID = "id";
public void onModuleLoad() {
final ListGrid grid = new ListGrid();
grid.setCanEdit(true);
grid.setDataSource(new GridDataSource());
grid.setAutoFitData(Autofit.BOTH);
ListGridField field = new ListGridField();
field.setTitle("Item");
field.setWidth(100);
// I want to store the value of the id
field.setName(ID);
field.setValueField(ID);
// I want to display the human readable name in the grid and pick list
field.setDisplayField(NAME);
// If I set the option data source on the field only the
// ID is stored. I want both name and id in the record, following the
// advice in the java doc for large value sets
// field.setOptionDataSource(new OptionDataSource());
// So I use a select item editor instead, backed by a datasource to provide the
// pick list values. In real life, this will be a dependent select based
// on other values in the record, so the criteria will be set, but this is omitted.
// This displays the pick list ok, but saves the ID in both the name and id fields
// of the record. I have tried various combos of setValueField() and setDisplayField()
// with no success
SelectItem editor = new SelectItem();
editor.setOptionDataSource(new OptionDataSource());
// Setting the value map on the editor like this gives the desired behaviour. The id
// and names are stored in the correct fields and everything looks ok. But I need to use the
// optionDataSource so I can do dynamic data lookup for dependent selects
// LinkedHashMap<String, String> valueMap = new LinkedHashMap<String, String>();
// valueMap.put("1", "Item One");
// valueMap.put("2", "Item Two");
// valueMap.put("3", "Item Three");
// editor.setValueMap(valueMap);
field.setEditorType(editor);
grid.setFields(field);
Button editButton = new Button("Edit New");
editButton.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
grid.startEditingNew();
}
});
Button logButton = new Button("Log");
logButton.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
for(Record record : grid.getDataAsRecordList().toArray()) {
SC.logEcho(record.getJsObj());
}
}
});
VLayout vLayout = new VLayout();
vLayout.setMembersMargin(10);
vLayout.addMember(grid);
vLayout.addMember(editButton);
vLayout.addMember(logButton);
vLayout.draw();
}
private static class GridDataSource extends DataSource {
private GridDataSource() {
setClientOnly(true);
DataSourceTextField idField = new DataSourceTextField(ID);
DataSourceTextField nameField = new DataSourceTextField(NAME);
setFields(idField, nameField);
}
}
private static class OptionDataSource extends DataSource {
private OptionDataSource() {
setClientOnly(true);
DataSourceTextField idField = new DataSourceTextField(ID);
DataSourceTextField nameField = new DataSourceTextField(NAME);
Record option1 = new Record();
option1.setAttribute(ID, "1");
option1.setAttribute(NAME, "Item 1");
Record option2 = new Record();
option2.setAttribute(ID, "2");
option2.setAttribute(NAME, "Item 2");
Record option3 = new Record();
option3.setAttribute(ID, "3");
option3.setAttribute(NAME, "Item 3");
setFields(idField, nameField);
setCacheData(option1, option2, option3);
}
}
Code:
0:43:09.030:TMR8:INFO:ResultSet:isc_ResultSet_0 (created by: isc_ListGrid_0):Updating cache: operationType 'add' submitted by 'isc_ListGrid_0',1 rows update data:
[
{id: "2",
name: "2"}
]
Code:
10:44:57.307:TMR1:INFO:ResultSet:isc_ResultSet_0 (created by: isc_ListGrid_0):Updating cache: operationType 'add' submitted by 'isc_ListGrid_0',1 rows update data:
[
{id: "2",
name: "Item Two"}
]
Comment