[英]How to make a list of objects which are checked in GWT?
我正在開發一個使用復選框的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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.