Dialog box close not working in primefaces - jsf

I am opening dialog through
public void openDialogRegionList() {
RequestContext.getCurrentInstance().openDialog("RegionList");
}
I have a commandLink onClick of that and i need to close dialog box.
public void setId(ActionEvent event){
UIComponent component = event.getComponent();
String value = (String) component.getAttributes().get("value");
this.regionId = value;
RequestContext.getCurrentInstance().closeDialog("RegionList");
}
But the dialog box is not closing.
Thanks in advance

You can add oncomplete="PF('dialogName').close()" attribute to your html button. Wether it doesn't work, other solution is to call the hide() method.

Related

JSF Custom component extending Primefaces SelectOneMenu

I need to create a custom component that either extend or include a Primefaces SelectOneMenu. This is done so that I can deliver the select items based on the field (for now, they are hardcoded in the example below and the tag is properly registered).
The component is rendered and the select items are also displayed fine. However, when I save, the record's field is not updated with the selected item's value. Is there some primeface method I should override to actually set the value?
Are there any tutorials on how to extend primeface (or atleast jsf) components? I could hardly find any. Thank you in advance
#FacesComponent(value = ComponentRegistry.INPUT_SELECTDROPDOWN_COMPONENT_TYPE)
public class InputSelectDropdown extends SelectOneMenu {
#Override
public void encodeBegin(FacesContext context) throws IOException {
this.setValueExpression("value", this.getValueExpression("value"));
UISelectItem select1 = new UISelectItem();
select1.setItemLabel("item 1");
select1.setItemValue("item1");
select1.encodeAll(context);
this.getChildren().add(select1);
UISelectItem select2 = new UISelectItem();
select2.setItemLabel("item 2");
select2.setItemValue("item2");
select2.encodeAll(context);
this.getChildren().add(select2);
super.encodeBegin(context);
}
}

Primefaces DialogFramework close on init

I have the same requirement as mentioned in "
primefaces close dialog on init don't work" but the only answer is not a working solution, becaue in Dialog Framework i don't have a widgetVar.
My #PostConstruct method looks similar:
#PostConstruct
private void init() {
Tree tree = initTree();
if (tree.getChildCount() == 1) {
// #todo close dialog if tree has only 1 child
// this does not close the dialog
RequestContext.getCurrentInstance().closeDialog(true);
// this does not work, because there is no widgetVar
//RequestContext.getCurrentInstance().execute("yourDialogWidgetVar.close()");
}
}
So, how can I close a DialogFramework xhtml page from its #PostConstruct method ?
I'm using Primefaces 6.0 on Wildfly 10.0.0.Final

Execute java method on JSF button click [duplicate]

This question already has answers here:
commandButton/commandLink/ajax action/listener method not invoked or input value not set/updated
(12 answers)
Closed 6 years ago.
When I click on button nothing happens.
<h:commandButton value="Generate PDF" type="button"
action="#{parseHtml12.createPdf}" />
This button is in XHTML file which I want convert to pdf.
Java class code is here :
public class ParseHtml12 {
public static final String DEST = "C:\\Users\\User\\Desktop/report.pdf";
public static final String HTML = "web/data.xhtml";
public static void main(String[] args) throws IOException, DocumentException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new ParseHtml12().createPdf(DEST);
}
public void createPdf(String file) throws IOException, DocumentException {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
writer.setInitialLeading(12);
document.open();
XMLWorkerHelper.getInstance().parseXHtml(writer, document,
new FileInputStream(HTML));
document.close();
}
}
This code is good , only problem is how execute class on button click.
When I run class in IDE gives me the result but the problem is that the content in XHTML is dynamic and does not retrieve values.
If I execute class when the value will be filled , this will give me desired result.
UPDATE: When the button is clicked dynamic data disappear. And if I click once again happens like this:
javax.el.PropertyNotFoundException: /data.xhtml #48,45 action="#{parseHtml12.createPdf}": Target Unreachable, identifier 'parseHtml12' resolved to null
I don't have enough points to comment.
try wrapping your commandButton inside a <h:form> tag, and add type="submit" to the button.

Can't attach listener to close dialog button

I'm using PrimeFaces dialog framework. The dialog window have small x in the corner which closes it. Can I attach some code to it?
I've tried:
<p:commandButton id="btn_open" value="add" process="" update="messages" action="#{bean.openDialog}">
<p:ajax event="dialogReturn" listener="#{bean.onDialogClose}" update="messages"/>
</p:commandButton>
public void onDialogClose(CloseEvent event){
}
but no avail, I never get to onDialogClose function.
I think it's wrong event or signature, but I can't find the answer in documentation.
dialogReturn event must be handled by
public void onDialogReturn(SelectEvent event)
{
Object obj = event.getObject();
}
which is explicitly fired by
RequestContext.getCurrentInstance().close(obj);
on the dialog side.
Closing the dialog with the upper-right "X" button does not fire the dialogReturn event.
I don't know if it is possible at all to handle a dialog framework close event, the first thing that comes into my mind is to open the dialog with option closable="false" and create another button for closing it.
public void open(String outcome)
{
Map<String, Object> options = new HashMap<>();
options.put("closable", false);
RequestContext.getCurrentInstance().openDialog(outcome, options, null);
}
and then manage this state:
public void onDialogReturn(SelectEvent event)
{
Object obj = event.getObject();
if(obj == null)
{
// handle close
}
else
{
// handle object returned
}
}

How to identify clicked commandButton in my managed bean

I have an actionListener method in my managed bean which is called by many command buttons.
public void verifyTestDisponibility(ActionEvent actionEvent) {
if (button1 clicked) {
// ...
}
if (button2 clicked) {
// ...
}
}
The part I'm stucking at is identifying the clicked command button. How can I identify it?
You can use it like this
In xhtml page we can use a <h:commandButton> tag with actionListener
<h:commandButton id="btn1" action="#{yourBean.method}" value="ButtonValue"
actionListener="#{yourBean.commandClicked}"></h:commandButton>
In your managed bean
private String buttonId;
/* getters and setters for buttonId goes here */
public void commandClicked(ActionEvent event) {
/* retrieve buttonId which you clicked */
buttonId = event.getComponent().getId();
/*check for which button you clicked*/
if(buttonId.equals("btn1")){
}else{
}
}
You can use the event.getComponent().getClientId();
if (event.getComponent().getClientId().equals("firstButton")).....
This works with ajax listener too. I had used with Primefaces 5 ajax listener. For example:
public void myListener(AjaxBehaviorEvent ev) {
String clientId = ev.getComponent().getClientId();
...
}

Resources