Search code examples
javagwtgxt

Dropdown window does not open on custom filtered GXT combobox


I want to filter GXT ComboBox Store. for example if I type 'st' in combobox I want combobox to show only values that contain 'st'

enter image description here

Here is my implementation

combo = new ComboBox<MerchantDTO>(store, label);
    StoreFilter<MerchantDTO> filter = new StoreFilter<MerchantDTO>() {
        @Override
        public boolean select(Store<MerchantDTO> store, MerchantDTO parent, MerchantDTO item) {
        boolean canView = (item.getName() != null &&    item.getName().toLowerCase().contains(combo.getText().toLowerCase()));
        return canView;
    }
};
store.setEnableFilters(true);
store.addFilter(filter);

This filter works and shows correct values, But combobox's dropdown list does not open automatically. I have to click on combobox manually to open dropdown list and see filtered results. I am using GXT 3.1.0 and GWT 2.7.0

I tried using combo.expand(); function but it didnt open dropdown list.

Any help would be appreciated.


Solution

  • I found solution. Here is sample how to add custom filter to GXT (version 3.1.0) ComboBox

    1) Create class which extends ListStore and add String variable for user input text

    public abstract class XListStore<M> extends ListStore<M> {
    
    private String userText;
    
    public XListStore(ModelKeyProvider<? super M> keyProvider) {
        super(keyProvider);
    }
    
    @Override
    protected boolean isFilteredOut(M item) {
        return filter(item);
    }
    
    public abstract boolean filter(M item);
    
    public String getUserText() {
        return userText;
    }
    
    public void setUserText(String userText) {
        this.userText = userText;
    }
    }
    

    2) Initialize custom list store and implement filter method

    XListStore<SampleDTO> store = new XListStore<SampleDTO>(new ModelKeyProvider<SampleDTO>() {
            @Override
            public String getKey(SampleDTO item) {
                return item.getId();
            }
        })  {
            public boolean filter(SampleDTO item) {
                boolean result = false;
                //Write you filter logic here
                return result;
            }
        };
    store.setEnableFilters(true);
    

    3) Initialize ComboBox and add Key up handler

    ComboBox<SampleDTO> comboBox = new ComboBox<SampleDTO>(store, label);
        comboBox.addKeyUpHandler(new KeyUpHandler() {
        @Override
        public void onKeyUp(KeyUpEvent event) {
            store.setUserText(comboBox.getText());
        }
    });
    

    Done. Now ComboBox will filter store according to user input and will open dropdown window automatically