ComboBoxItem suggestion…
Dear,
After hours trying to find a good solution, finally I found a hack to achieve my requirement…
If someone get a better/simpler solution, I will appreciate.
First the requirement:
Get a data bound combo box, where the user CANNOT put an unknown value (autocomplete feature but only for allowed values, from the list).
First was to use a SelectItem, but the user cannot type for values… only select.
Second was to use a ComboBoxItem, but if the user do not select/type any value from the list, then the resulting value of the item will be the one he put… (getValue())
How to achieve:
- First, add a new function to the ComboBoxItem class
class MyComboBoxItem extends ComboBoxItem {
public MyComboBoxItem() {
setType( "ComboBoxItem" );
}
public MyComboBoxItem(final JavaScriptObject jsObj) {
super( jsObj );
}
public MyComboBoxItem(final String name) {
setName( name );
setType( "ComboBoxItem" );
}
public native Boolean isRecordSelected()
/*-{
var self = this.@com.smartgwt.client.core.DataClass::getJsObj()();
var isRecordSelected = ( self.getSelectedRecord() ) ? true : false;
return @com.smartgwt.client.util.JSOHelper::toBoolean(Z)(isRecordSelected);
}-*/;
}
On the previous code, I would be great to add the ‘getSelectedRecord()’ function available from Java…
- Second declare a blurRegistred (explained later) :
boolean isSectionBlurRegistered = false;
- Third add the ComboBoxItem to the form :
MyComboBoxItem section = new MyComboBoxItem( "section" );
section.setWidth( 250 );
section.setCompleteOnTab( true );
section.addChangedHandler( new ChangedHandler() {
public void onChanged(final ChangedEvent event) {
if ( !isSectionBlurRegistered ) {
section.addBlurHandler( new BlurHandler() {
public void onBlur(final BlurEvent event) {
if ( section.isRecordSelected() == false ) {
section.setValue( "" );
}
}
} );
isSectionBlurRegistered = true;
}
}
} );
- Last, an extract from the DataSource of the DynamicForm the field belong to:
final DataSourceTextField section = new DataSourceTextField( "section", "Section" );
section.setForeignKey( "section_DS.section" );
section.setRequired( true );
That’s it, if the use type a value without any matching records, then the combobox will be emptied when leaving the field.
On this code, there is another hack, the ‘isSectionBlurRegistered’ boolean… in fact, the onBlur handler is invoked when the user press a key for the first type in the combo box, not any more after, I don’t know why, may be someone can explain…
Hope that may help someone else.
In any case, it would be great if the smartgwt/client team could introduce this feature, with an attribute like ‘allowUnknownValues (boolean)’
Best Regards
Alexandre