Search code examples
gwtsmartgwt

How to Restrict deselecting all items in smartgwt SelectItem with multiple selection enabled?


I´m using the SelectItem component with configuration:

SelectItem selectItem = new SelectItem("SelectItem");
selectItem.setShowTitle(false);
selectItem.setMultiple(true);
selectItem.setMultipleValueSeparator(",");
selectItem.setDefaultValue("1");

Now I want to restrict user to deselecting all items,at least one item must be selected.

Can anybody help me how to restrict?


Solution

  • Hy Suresh,

    here is the quick fix!

    In my example you need to use second SelectItem for saving purpose and FocusHandler.

    My example:

    LinkedHashMap<String, String> valueMap = new LinkedHashMap<>();
        valueMap.put("a", "a");
        valueMap.put("b", "b");
        valueMap.put("c", "c");
        valueMap.put("d", "d");
        valueMap.put("e", "e");
        valueMap.put("f", "f");
    
        final SelectItem saveSelectedItem = new SelectItem();
        saveSelectedItem.setMultiple(true);
        saveSelectedItem.setValueMap(valueMap);
    
        final SelectItem selectedItem = new SelectItem();
        selectedItem.setDefaultValue("Default");
        selectedItem.setMultiple(true);
        selectedItem.setTitle("Select: ");
        selectedItem.setValueMap(valueMap);
        selectedItem.addFocusHandler(new FocusHandler() {
    
            @Override
            public void onFocus(FocusEvent event) {
                if(selectedItem.getValue() == null){
                    selectedItem.setValue(saveSelectedItem.getValue());
                }else {
                    saveSelectedItem.setValue(selectedItem.getValue());
                }               
            }
        });
    

    So user will be able to un/select all values, but value will be set to last selected value in SelectItem field.

    Hope int helps.

    Regards Jakov A.