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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With