nicBBB
nicBBB

Reputation: 257

jsf2 make datatable row editable does show inputtext fields

I am trying to edit a datatable row with JSF2. In debugging the editAction is showing the correct row, but the outputtext is not transformed into inputtext to allow editing, There seems to be a rendering problema executing the edit action. My bean is sessionscoped and when hitting one of the buttons (edit, add, delete, cancel) the method getListaNoticias is executed as many times as there are inputfields.

My code:

historial.xhtml

    <h:form>
        <h:dataTable styleClass="tablaHistorial"
            value="#{historialBean.listaNoticias}" var="o">

            <h:column>
                <f:facet name="header">Fecha</f:facet>
                #{o.fecha}
            </h:column>

            <h:column>
                <f:facet name="header">Noticia</f:facet>
                <h:inputTextarea value="#{o.titulo}" rendered="#{o.editable}"/>
                <h:outputText value="#{o.titulo}" rendered="#{not o.editable}" />
            </h:column>

            <h:column>
                <h:commandButton action="#{historialBean.editAction(o)}">
                    <f:ajax render="@form" />
                </h:commandButton>
            </h:column>

            <h:column>
                <h:commandButton action="#{historialBean.save(o)}">
                    <f:ajax render="@form" execute="@form" />
                </h:commandButton>

            </h:column>
            <h:column>
                <h:commandButton action="#{historialBean.cancelarAccion(o)}">
                    <f:ajax render="@form" />
                </h:commandButton>
            </h:column>
            <h:column>
                <h:commandButton action="#{historialBean.borrar(o)}">
                    <f:ajax render="@form" />
                </h:commandButton>
            </h:column>

        </h:dataTable>

    </h:form>

historialBean class

    @ManagedBean
    @SessionScoped
    public class HistorialBean implements Serializable {

        private static final long serialVersionUID = 1L;

        public List<Noticia> listaNoticias;

        public HistorialBean() {
        }

        public String irAConsola() {
            return navigationBean.redirectToLoggedIn();


        public List<Noticia> getListaNoticias() {
                ConexionUtil conexion = new ConexionUtil();
                listaNoticias = new ArrayList<Noticia>();
                listaNoticias = conexion.prepararListaNoticiasBBDDExterna();
            return listaNoticias;
        }

        public void setListaNoticias(List<Noticia> listaNoticias) {
            this.listaNoticias = listaNoticias;
        }

        public void editAction(Noticia noticia) {
            noticia.setEditable(true);
        }

        public void editar(Noticia noticia) {
            ConexionUtil conexion = new ConexionUtil();
            conexion.editarNoticiaBBDDExterna(noticia);
            noticia.setEditable(false);
        }

        public void borrar(Noticia noticia) {
            ConexionUtil conexion = new ConexionUtil();
            conexion.deshabilitarNoticiaBBDDExterna(noticia);
        }

        public void cancelarAccion(Noticia noticia) {
            noticia.setEditable(false);
        }

    }

Noticia class

    public class Noticia {


        private String titulo;
        private String fecha;

        private boolean editable;



        public Noticia(String titulo,String fecha) {
            super();
            this.titulo = titulo;
            this.fecha = fecha;

        }

        public String getTitulo() {
            return titulo;
        }

        public void setTitulo(String titulo) {
            this.titulo = titulo;
        }

        public String getFecha() {
            return fecha;
        }

        public boolean isEditable() {
            return editable;
        }

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


    }       

Upvotes: 0

Views: 605

Answers (1)

nicBBB
nicBBB

Reputation: 257

At the end I solved it putting the creation of the list in the bean constructor:

    public HistorialBean() {
            ConexionUtil conexion = new ConexionUtil();
            listaNoticias = new ArrayList<Noticia>();
            listaNoticias = conexion.prepararListaNoticiasBBDDExterna();
    }


    public List<Noticia> getListaNoticias() {

        return listaNoticias;
    }

Upvotes: 1

Related Questions