我正在开发一个使用复选框的 GWT 应用程序。我有复选框中的 GwtRoles 列表,但我不知道如何获取选中的 GwtRoles。这是我的代码:
@Override
public void createBody() {
for (GwtRole gwtRole : roleList) {
checkBox = new CheckBox();
checkBox.setBoxLabel(gwtRole.getName());
for (GwtAccessRole gwtAccessRole : lista) {
if (gwtRole.getId().equals(gwtAccessRole.getRoleId())) {
checkBox.setValue(true);
}
RoleList 是复选框中的 GwtRoles 列表。此列表是用户打开表单时应预先检查的项目列表。我不太熟悉 GWT 中的复选框。 我使用了 CheckBox 列表,并且有方法 getChecked() 返回所有已检查的 GwtRoles 的列表,但在这里使用此复选框时我没有该选项。 在此方法中,我应该创建一个已检查的 GwtRoles 列表:
@Override
public void submit() {
List<GwtAccessRoleCreator> listCreator = new ArrayList<GwtAccessRoleCreator>();
for (GwtRole role : list) {
GwtAccessRoleCreator gwtAccessRoleCreator = new GwtAccessRoleCreator();
gwtAccessRoleCreator.setScopeId(currentSession.getSelectedAccount().getId());
gwtAccessRoleCreator.setAccessInfoId(accessInfoId);
gwtAccessRoleCreator.setRoleId(role.getId());
listCreator.add(gwtAccessRoleCreator);
}
GWT_ACCESS_ROLE_SERVICE.createCheck(xsrfToken, currentSession.getSelectedAccount().getId(), userId, listCreator, new AsyncCallback<GwtAccessRole>() {
@Override
public void onSuccess(GwtAccessRole arg0) {
exitStatus = true;
exitMessage = MSGS.dialogAddConfirmation();
hide();
}
有人可以帮助我如何设置已检查的 GwtRoles 列表吗?
最佳答案
在 Map
中跟踪您的 CheckBox
,然后仅返回选中复选框的 GwtRole
。
private Map<GwtRole, CheckBox> mapping = new HashMap<>();
@Override
public void createBody() {
for (GwtRole gwtRole : roleList) {
CheckBox checkBox = new CheckBox();
mapping.put(gwtRole, checkBox);
// Your other code here.
}
}
// Java 8
public List<GwtRole> getChecked()
{
return mapping.entrySet().stream()
.filter(e -> e.getValue().getValue())
.map(Map.Entry::getKey)
.collect(Collectors.toList());
}
// Pre-Java 8
public List<GwtRole> getChecked()
{
List<GwtRole> result = new ArrayList<>();
for(Map.Entry<GwtRole, CheckBox> e : map.entrySet())
{
if(e.getValue().getValue())
result.add(e.getKey());
}
return result;
}
关于java - 如何创建 GWT 中检查的对象列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45999910/