1

I'm trying to make something that change <h:outputText> into <h:inputText> after clicking on button or link. Somtehing similar to this: How to build "edit" button in JSF and switch between h:outputText and h:inputText but I would like to use PrimeFaces.

I think this would be usefull: http://www.primefaces.org/showcase/ui/inplace.jsf but I would like to make that edit mode fires after clicking on edit button or hyperlink, not after clicking on the text I want to change.

Of course after clicking edit button I would like that change to "accept" button that will allow me to save changes and change inputText into outputText

Community
  • 1
  • 1
WojciechKo
  • 1,511
  • 18
  • 35

1 Answers1

2

Take a look at this SSCCE, it does what you want.

TestBean.java

package com.mycompany;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

@ManagedBean
@ViewScoped
public class TestBean {

    /**
     * Controls if the input field is available or not
     */
    private boolean editable = false;

    /**
     * The String value you want to edit
     */
    private String value = "Default value";

    /**
     * Changes between the inputText and the outputText
     */
    public void changeEditable() {
        editable = !editable;
    }

    public String getValue() {
        return value;
    }

    public boolean isEditable() {
        return editable;
    }

    /**
     * Definitely saves the value
     */
    public void saveValue() {
        FacesMessage message = new FacesMessage("Value " + value + " saved!");
        FacesContext.getCurrentInstance().addMessage(null, message);
    }

    public void setEditable(boolean editable) {
        this.editable = editable;
    }

    public void setValue(String value) {
        this.value = value;
    }

}

index.xhtml

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:p="http://primefaces.org/ui">

<h:head>
    <title>JSF Demo</title>
</h:head>
<h:body>
    <p:messages />
    <h:form>
        <h:panelGroup rendered="#{!testBean.editable}">
            <h:outputText value="#{testBean.value}" />
        </h:panelGroup>
        <h:panelGroup rendered="#{testBean.editable}">
            <p:inputText value="#{testBean.value}" />
        </h:panelGroup>

        <p:commandButton
            value="#{testBean.editable ? 'Confirm value' : 'Change value'}"
            update="@form" actionListener="#{testBean.changeEditable}" />

        <p:commandButton value="Save value" ajax="false"
            action="#{testBean.saveValue}" />
    </h:form>

</h:body>
</html>
Aritz
  • 30,971
  • 16
  • 136
  • 217