我有一个对象包含一组另一个对象Roles的用户。我创建了一个新面板ListPanel,用于保存用户角色的列表视图。所有这些都包含在DataTable中。我希望能够过滤掉那个角色列表,这就是我被困住的地方。当我尝试过滤DataTable上的角色时,我收到以下错误:“无法转换值:管理员输入:java.util.Set。无法找到兼容的转换器。”管理员是我从下拉列表中获得的价值。
我迷失了从哪里开始。从我读过的内容来看,你不能使用带有DropDownChoice的转换器,所以我研究了创建一个渲染器,但是传递给渲染器的值是一个String而不是Role对象。我认为渲染器是正确的选择,但有些东西需要在其他地方改变以使其发挥作用。
实体
public class User {
private String id;
private String name;
private Set<Role> roles;
......properties/getters/setters.......
}
public class Role {
private String role;
private int active;
......properties/getters/setters.......
}
搜索面板
public class UserSearchPanel extends BreadCrumbPanel {
private static final int ROWS_PER_PAGE = 20;
@Inject
private UserDao userDao;
@Inject
private RoleDao roleDao;
private final UserDataTable dataTable;
public UserSearchPanel( final String id, final IBreadCrumbModel breadCrumbModel ) {
super( id, breadCrumbModel );
NotificationPanel feedback = new NotificationPanel( "feedback" );
feedback.setOutputMarkupId( true );
add( feedback );
UserDataProvider dataProvider = new UserDataProvider( userDao );
dataProvider.setFilterState( loadFilterState() );
// create the form used to contain all filter components
final FilterForm<User> form = new FilterForm<User>( "userSearchForm", dataProvider ) {
@Override
protected void onSubmit() {
dataTable.setCurrentPage( 0 );
}
};
dataTable = new UserDataTable( "dataTable", createColumns(), dataProvider, ROWS_PER_PAGE, this );
dataTable.addTopToolbar( new FilterToolbar( dataTable, form, dataProvider ) );
form.add( dataTable );
add( form );
}
/**
* The columns shown on the page.
*/
private List<IColumn<User, String>> createColumns() {
List<IColumn<User, String>> columns = new ArrayList<IColumn<User, String>>();
List<String> choices = new ArrayList<String>();
try {
choices = roleDao.getAvailableRoles();
} catch ( Exception e ) {
e.printStackTrace();
}
if ( choices.size() > 1 ) {
IModel<List<? extends String>> filterChoices = Model.ofList( choices );
columns.add( createChoiceColumn( "datatable.roles.label", "roles", "roles", filterChoices ) );
}
return columns;
}
private ChoiceFilteredPropertyColumn<User, String, String> createChoiceColumn( final String key, final String sortProperty, final String propertyExpression, final IModel<List<? extends String>> filterChoices ) {
IModel<String> label = getLabel();
return new ChoiceFilteredPropertyColumn<User, String, String>( label, sortProperty, propertyExpression, filterChoices ) {
@Override
protected boolean enableAutoSubmit() {
return false;
}
@Override
public void populateItem( Item<ICellPopulator<User>> cellItem, String componentId, IModel<User> rowModel ) {
cellItem.add( new ListPanel( componentId, rowModel ) );
}
};
}
private class ListPanel extends Panel {
public ListPanel( String id, IModel<User> model ) {
super( id );
User user = (User) model.getObject();
addList( user );
}
private void addList( User user ) {
Set<Role> rolesSet = user.getRoles();
List<Role> roles = new ArrayList<>();
for ( Role role : rolesSet ) {
roles.add( role );
}
ListView<Role> view = new ListView<Role>( "list", roles ) {
@Override
protected void populateItem( ListItem<Role> item ) {
final Role role = (Role) item.getModelObject();
item.add( new Label( "roles", role.getRole() ) );
}
};
add( view );
}
}
}
答案 0 :(得分:1)
我建议您创建一个类来代表您在User
中过滤并对这些值做出反应的内容,而不是使用您的IDataProvider
对象。
目前发生的情况是您使用User对象代表您的FilterState
并且wicket尝试设置String&#34; ADMIN&#34;到Set<Role>
个User
对象的 class FilterState implements Serializable {
private String roles;
public String getRoles() {
return roles;
}
public void setRoles(String roles) {
this.roles = roles;
}
}
class UserDataProvider implements IDataProvider<User>, IFilterStateLocator<FilterState> {
private FilterState state;
private UserDao userDao;
public UserDataProvider() {
state = new FilterState();
}
....
@Override
public Iterator<? extends User> iterator(long first, long count) {
//just some example filter logic, handled by the dao
if (state == null || state.getRoles() == null) {
return userDao.getUserList(first, count).iterator();
} else {
return userDao.getUserListByRoleName(state.getRoles(), first, count).iterator();
}
}
@Override
public FilterState getFilterState() {
return state;
}
@Override
public void setFilterState(FilterState state) {
this.state = state;
}
}
。
只需在单独的Object中声明不同的过滤器选项。
像:
Sub SetFilesReadOnly(Optional location As Folder)
Dim fso As Scripting.FileSystemObject
Set fso = New Scripting.FileSystemObject
If location Is Nothing Then
Set location = fso.GetFolder("C:\Temp")
End If
Dim target As File
For Each target In location.Files
target.Attributes = target.Attributes + ReadOnly
Next target
Dim directory As Folder
For Each directory In location.SubFolders
SetFilesReadOnly directory
Next directory
End Sub