gpt4 book ai didi

java - 如何在选择每个子项时设置根复选框被选中(GWT)

转载 作者:行者123 更新时间:2023-12-02 12:10:34 25 4
gpt4 key购买 nike

我正在构建 GWT 应用程序,其中有带有复选框的 Tree 和 TreeItems。我有一个名为 allCheckBox 的根 CheckBox 及其子元素 rootCheckBox(此复选框也有它们的子元素,但这对此无关紧要)。我希望,当用户打开带有复选框的对话框时,如果选择了每个 childCheckBox,则选择此复选框。我已经做到了,当选择根复选框时,子复选框也会被选择。
这是我的一段代码:

enter cod  GWT_DOMAIN_SERVICE.findAll(new AsyncCallback<List<GwtDomain>>() {

@Override
public void onFailure(Throwable caught) {
exitMessage = MSGS.dialogAddPermissionErrorDomains(caught.getLocalizedMessage());
exitStatus = false;
hide();
}

@Override
public void onSuccess(List<GwtDomain> result) {

for (final GwtDomain gwtDomain : result) {

GWT_DOMAIN_SERVICE.findActionsByDomainName(gwtDomain.name(), new AsyncCallback<List<GwtAction>>() {

@Override
public void onFailure(Throwable caught) {
exitMessage = MSGS.dialogAddPermissionErrorActions(caught.getLocalizedMessage());
exitStatus = false;
hide();
}

@Override
public void onSuccess(List<GwtAction> result) {
checkedItems = new GwtCheckedItems();
checkedItems.setName(gwtDomain);

rootCheckBox = new CheckBox();
rootCheckBox.setBoxLabel(gwtDomain.toString());
listCheckBoxes.add(rootCheckBox);
rootTreeItem = new TreeItem(rootCheckBox);
childCheckBoxMapList = new HashMap<GwtAction, CheckBox>();
checkedItems.setMap(childCheckBoxMapList);
for (GwtAccessPermission gwtAccessPermission : checkedPermissionsList) {
if (gwtAccessPermission.getPermissionDomain().toString().equals(checkedItems.getName().toString())) {
if (gwtAccessPermission.getPermissionAction().toString().equals(GwtAction.ALL.toString())) {
rootCheckBox.setValue(true);
}
}
}
if (listOfNewClass.size() == checkedPermissionsList.size()) {
allCheckBox.setValue(true);
}
for (final GwtAction gwtAction : result) {

final CheckBox childTreeItemCheckox = new CheckBox();
treeItem = new TreeItem(childTreeItemCheckox);
childTreeItemCheckox.setBoxLabel(gwtAction.toString());

rootTreeItem.addItem(treeItem);

childListOfNewClass.add(gwtAction);
allTreeItem.addItem(rootTreeItem);
childCheckBoxMapList.put(gwtAction, childTreeItemCheckox);
for (GwtAccessPermission gwtAccessPermission : checkedPermissionsList) {
if (gwtAccessPermission.getPermissionDomain().toString().equals(gwtDomain.toString())) {

if (gwtAccessPermission.getPermissionAction().toString().equals(gwtAction.toString())) {
childTreeItemCheckox.setValue(true);
}
}
}
}

listOfNewClass.put(checkedItems, rootCheckBox);

}

});

}

allCheckBox.addListener(Events.OnClick, new Listener<BaseEvent>() {

@Override
public void handleEvent(BaseEvent be) {
if (allCheckBox.getValue()) {
for (CheckBox checkBox : listCheckBoxes) {
if (!checkBox.getValue()) {
checkBox.setValue(true);
}
}
} else {
for (CheckBox checkBox : listCheckBoxes) {
checkBox.setValue(false);
}
}
}
});

如何设置当所有 rootCheckBox 都被选中时,allCheckBox 也被选中?

编辑:这个checkedPermissionsList是被选中的rootCheckBox的列表。

最佳答案

监听器必须迭代子框并查看它们是否全部被选中,并相应地设置父框

Listener<BaseEvent> listener = new Listener<>() {
public void handleEvent(BaseEvent be) {
boolean allSet = listCheckBoxes.stream().allMatch(CheckBox::getValue);
allCheckBox.setValue(allSet); // this will also unselect if appropriate
}
}

所有框的监听器都是相同的,因此将其添加到每个框

listCheckBoxes.forEach(box -> box.addListener(Event.OnClick, listener));

在 Java 8 之前的版本中:

Listener<BaseEvent> listener = new Listener<>() {
public void handleEvent(BaseEvent be) {
boolean allSet = true;
for (CheckBox child : listCheckBoxes) {
if (!child.getValue()) {
allSet = false; // found a non-checked box
break;
}
}
allCheckBox.setValue(allSet); // this will also unselect if appropriate
}

// and set the listener to the children with
for (CheckBox box : listCheckBoxes) {
box.addListener(Event.Clicked, listener);
}

关于java - 如何在选择每个子项时设置根复选框被选中(GWT),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46583433/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com