英文:
How to access button elements within Vaadin confirm dialog?
问题
I want to set the focus on one of the buttons manually. What is the proper way to access these from within the Java class. I cannot see the button when I do
getElement().getChildren()...
When I inspect the dialog with the browser developer tools I can see the Element right there:
Thanks and Best Regards
Matt
英文:
I want to set the focus on one of the buttons manually. What is the proper way to access these from within the Java class. I cannot see the button when I do
getElement().getChildren()...
When I inspect the dialog with the browser developer tools I can see the Element right there:
<vaadin-button theme="tertiary" slot="cancel-button" tabindex="0" role="button">Cancel</vaadin-button>
Thanks and Best Regads
Matt
答案1
得分: 2
你需要在那里使用自定义按钮。请参考下面的示例。我创建了两个按钮,另一个是“是”按钮。我在附加时以编程方式将焦点设置为它(即对话框已打开)。当您按下回车键时,将触发“是”按钮,而不是“否”按钮。
@Override
public void afterNavigation(AfterNavigationEvent event) {
Button confirmButton = new Button("是");
confirmButton.addThemeVariants(ButtonVariant.LUMO_PRIMARY);
Button noButton = new Button("否");
noButton.addThemeVariants(ButtonVariant.LUMO_TERTIARY);
ConfirmDialog dialog = new ConfirmDialog();
dialog.add("确认");
dialog.setCancelButton(confirmButton);
dialog.setCancelable(true);
dialog.setConfirmButton(confirmButton);
dialog.addConfirmListener(e -> Notification.show("已确认"));
dialog.addCancelListener(e -> Notification.show("已取消"));
dialog.open();
confirmButton.addAttachListener(e -> confirmButton.focus());
}
英文:
You need to use custom buttons there. See the example below. I create two buttons, the other being "Yes" button. I programmatically focus that when attached (i.e. Dialog has been opened). When you hit enter key, that will trigger "Yes" button, not the "No" button.
@Override
public void afterNavigation(AfterNavigationEvent event) {
Button confirmButton = new Button("Yes");
confirmButton.addThemeVariants(ButtonVariant.LUMO_PRIMARY);
Button noButton = new Button("No");
noButton.addThemeVariants(ButtonVariant.LUMO_TERTIARY);
ConfirmDialog dialog = new ConfirmDialog();
dialog.add("Confirm me");
dialog.setCancelButton(confirmButton);
dialog.setCancelable(true);
dialog.setConfirmButton(confirmButton);
dialog.addConfirmListener(e -> Notification.show("Confirmed"));
dialog.addCancelListener(e -> Notification.show("Cancelled"));
dialog.open();
confirmButton.addAttachListener(e -> confirmButton.focus());
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论