Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wicket: ListView - skipping an item

is it possible to skip an item in populateItem() ? e.g. by setting something on the item component, or whatever.

ListView view = new ListView("quickLinks", quickLinks) {
    @Override
    protected void populateItem( ListItem item ){
        if( item.getModel(). /*something*/ )
                    item.skip()
    }
};

Thanks

like image 742
Ondra Žižka Avatar asked Dec 12 '22 16:12

Ondra Žižka


2 Answers

I would prefer filtering my model instead of doing an if / else check in the view.

Here, use this abstract class:

public abstract class FilteredListModel<K> extends LoadableDetachableModel<List<K>>{

    private static final long serialVersionUID = 1L;
    private IModel<List<? extends K>> inner;

    @Override
    protected void onDetach(){
        inner.detach();
    }

    public FilteredListModel(IModel<List<? extends K>> inner){
        this.inner = inner;
    }
    public FilteredListModel(List<? extends K> inner){
        this.inner = Model.ofList(inner);
    }

    @Override
    protected final List<K> load(){
        List<? extends K> input = inner.getObject();
        List<K> result = new ArrayList<K>(input.size());
        for(K k : input){
            if(accept(k))result.add(k);
        }
        return result;
    }

    protected abstract boolean accept(K k);
}

Now wrap your list model into it:

ListView<String> view = new ListView<String>(
    "quickLinks",
    new FilteredListModel<String>(quickLinks){

      protected boolean accept(String value){
          return yourCheckHere();
      }

    }) {

    @Override
    protected void populateItem( ListItem<String> item ){
        // no ifs here
    }
};
like image 200
Sean Patrick Floyd Avatar answered Dec 27 '22 01:12

Sean Patrick Floyd


Wouldn't item.setVisible(false) work?

like image 43
Karussell Avatar answered Dec 27 '22 00:12

Karussell