开发者

Wicket: ListView - skipping an item

开发者 https://www.devze.com 2023-02-02 06:30 出处:网络
is it possible to skip an item in populateItem() ? e.g. by setting something on the item component, or whatever.

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


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
    }
};


Wouldn't item.setVisible(false) work?


You could override the iterator() method in your ListView in order to only iterate the item you wish to render.

0

精彩评论

暂无评论...
验证码 换一张
取 消