简体   繁体   中英

JSF Primefaces ajax on p:datatable

I am using JSF Primefaces for my UI. I have a specific requirement about ajax on p:datatable. This is a sample page of my project:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<ui:composition
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui"
    xmlns:fn="http://java.sun.com/jsp/jstl/functions"
    xmlns:comp="http://java.sun.com/jsf/composite/components/">

    <ui:define name="content">

        <h:form id="form1">

            <p:growl id="messages" showDetail="false" />

            <p:panel header="Available Rows" style="width: 15%;">

                <p:dataTable id="table" value="#{rowBean.rows}" var="row" 
                    widgetVar="50" style="width: 60px;" editable="true">

                    <p:ajax event="rowEdit" listener="#{rowBean.onEdit}" 
                        update="form1:messages" />
                    <p:ajax event="rowEditCancel" listener="#{rowBean.onCancel}" 
                        update=":form1:messages" />

                    <p:column>
                        <f:facet name="header">  
                            <h:outputText value="Row ID" />
                        </f:facet>
                        <p:cellEditor>
                            <f:facet name="output">
                                <h:outputText value="#{row.id}" />
                            </f:facet>
                            <f:facet name="input">
                                <p:inputText value="#{row.id}" 
                                    style="width:100%" label="Row ID">
                                </p:inputText>
                            </f:facet>
                        </p:cellEditor>
                    </p:column>

                    <p:column>
                        <f:facet name="header">  
                            <h:outputText value="Row Value" />
                        </f:facet>
                        <p:cellEditor>
                            <f:facet name="output">
                                <h:outputText value="#{row.value}" />
                            </f:facet>
                            <f:facet name="input">
                                <p:inputText disabled="true" value="#{row.value}" 
                                    style="width:100%" label="Row Value">
                                </p:inputText>
                            </f:facet>
                        </p:cellEditor>
                    </p:column>

                    <p:column style="width:6%">
                        <p:rowEditor />
                    </p:column>

                </p:dataTable>

            </p:panel>

            <p:spacer height="5px;"/>

            <p:panel id="createPanel" header="Create Row" closable="false" toggleable="true" 
                collapsed="true" style="width: 15%;">
                <p:panelGrid columns="6">

                    <h:outputLabel for="rowBean" value="Row ID" />
                    <p:inputText value="#{rowBean.row.id}" 
                        required="true" label="Row ID" style="width:125px" />

                    <h:outputLabel for="rowBean" value="Row Value" />
                    <p:inputText value="#{rowBean.row.value}" 
                        required="true" label="Row Value" style="width:125px" />

                    <f:facet name="footer">
                        <p:commandButton value="Create" action="#{rowBean.createRow}" 
                            update=":form1" />
                    </f:facet>
                </p:panelGrid>
            </p:panel>

        </h:form>

    </ui:define>

</ui:composition>

Here I have a data table which lists down all the rows created. Each row is having id and value. Value is editable. There is one more panel within the form which creates new rows. Backing bean is pre-populated with all the created rows with @PostConstruct. Backing bean is having other methods to edit, cancel and create. This page is working and given below is the new requirement.

In some cases there can be an error associated with each row. If there is an error attached with a row, then the value text needs to be updated like a link. While clicking on the link, the row ID and a parameter needs to be passed to backing bean. With these params, backing bean fetches the error details and passes it to the page. Then the error message needs to be shown on a pop-up.

Can you tell me how to do this?

On the XHTML side:

<p:messages id="messages" showDetail="true" autoUpdate="true" closable="true" />

On the JAVA side, in the function you call when you press the button:

if(error) {    
    FacesContext.getCurrentInstance().addMessage(
    null,
    new FacesMessage(FacesMessage.SEVERITY_WARN, "Error",
    "Your error message.")); 
}

PD: This is my first answer, i'm getting used to the post options, i'm sorry if you dont see my message properly ;)

Maybe like this?:

<f:facet name="output">
    <h:outputText value="#{row.value}" rendered="#{!row.hasError}" />
    <h:commandLink value="#{row.value}" rendered="#{row.hasError}" command="#{row.command(parameters..)}" />
</f:facet>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM