JSF中从后端Bean打开Dialog时如何设置Id?
Hey there, I get your frustration—setting the dialog ID via the options map doesn't work in PrimeFaces 5.3 because that version doesn't support the 'id' key in the options for openDialog(). Let me walk you through the correct ways to handle this based on your use case:
Option 1: Use a widgetVar to reference the dialog (for dynamic dialogs)
If you're loading a view (like /some.xhtml) as the dialog content, you can't set a custom ID directly, but you can assign a widgetVar to the dialog and use that to reference it (including getting its generated ID if needed):
Map<String, Object> options = new HashMap<>(); options.put("resizable", true); options.put("width", 1400); options.put("contentWidth", "100%"); options.put("height", 800); options.put("widgetVar", "myCustomDialog"); // Assign a widgetVar to the dialog RequestContext.getCurrentInstance().openDialog("/some.xhtml", options, null);
Then in JavaScript, you can access the dialog and its generated ID like this:
// Get the dialog widget instance var dialogWidget = PF('myCustomDialog'); // Retrieve the auto-generated HTML ID of the dialog element var dialogId = dialogWidget.getId();
Option 2: Define the dialog explicitly in your page (for full control over ID)
If you need a fixed, custom ID for the dialog, define the <p:dialog> component directly in your parent page (not in the target view), set its id and widgetVar, then open it using the widgetVar as the first parameter to openDialog():
First, add the dialog to your parent page:
<p:dialog id="someId" widgetVar="myExplicitDialog" resizable="true" width="1400" height="800"> <!-- Include your target content directly or via ui:include --> <ui:include src="/some.xhtml" /> </p:dialog>
Then in your backing bean:
Map<String, Object> options = new HashMap<>(); options.put("contentWidth", "100%"); // Add any additional options you need RequestContext.getCurrentInstance().openDialog("myExplicitDialog", options, null);
This way, the dialog's HTML ID will be exactly what you set (someId—or prefixed with the parent naming container's ID if it's inside one, like formId:someId).
Hope that helps you get the dialog ID working as expected!
内容的提问来源于stack exchange,提问作者SergeiK

