请参阅本文:Wicket 模型魔术:地图支持的 ListView
public CustomFieldsPanel( String id, final IModel<Map<String,ProductCustomField>> fieldMapModel, final FeedbackPanel feedbackPanel ) {
super( id, fieldMapModel );
this.feedbackPanel = feedbackPanel;
this.setOutputMarkupId( true ); // AJAX JavaScript code needs to have some id="...".
IModel<List<ProductCustomField>> listModel = new LoadableDetachableModel() {
@Override protected List<ProductCustomField> load() {
Map<String,ProductCustomField> map = (Map) CustomFieldsPanel.this.getDefaultModelObject();
return new ArrayList(map.values());
}
};
ListView<ProductCustomField> listView;
add( listView = new ListView<ProductCustomField>("fieldsRows", listModel){
@Override
protected void populateItem( final ListItem<ProductCustomField> item ) {
item.add( new CustomFieldRowPanel("fieldRow", item.getModel()){
// Delete icon was clicked.
@Override
protected void onDelete( AjaxRequestTarget target ) {
Map<String,ProductCustomField> fieldsMap = (Map) CustomFieldsPanel.this.getDefaultModelObject();
fieldsMap.remove( item.getModelObject().getName() );
target.add( CustomFieldsPanel.this ); // Update UI.
try {
CustomFieldsPanel.this.onChange( target ); // Persists.
} catch (Exception ex){
feedbackPanel.error( ex.toString() );
}
}
});
}
});
...
}