Updating one line in JSF / Primefaces using AJAX

How can I update one row in p: datatable when using AJAX?

I don't want to update the whole datatype because it has many rows and it will take a while.

My layout:

<h:form id="visitForm">
        <p:dataTable id="visitTable" var="visit" value="#{visitBean.findAllVisits()}">

            <p:column headerText="${msgs['email']}"
                <h:outputText value="#{visit.contactDetail.email}"/>
            </p:column>

            <p:column headerText="${msgs['clearance']}" id="clearance">
                <p:commandButton value="${msgs['clearance.ok']}"  actionListener="#{visitBean.makeClearanceNotOk(visit)}"/>
            </p:column>
        </p:dataTable>
    </h:form>

      

I've tried some things like update = "styling" etc, but it doesn't seem to work.

I am using JSF 2.1 and Primefaces 5.2

+6


source to share


4 answers


You can use a lookup expression @row(n)

that does exactly that - it updates the nth row in the table. To update the current line, you need to pass the line index as an argument. Set the attribute rowIndexVar="rowIdx"

to <p:dataTable>

and then:



<p:commandButton ... update="@form:visitTable:@row(#{rowIdx})" />

      

+10


source


Use the "ajax" function in a utility library such as OmniFaces , or create a PrimeFaces Selector declaration .



In both cases, you need to access, for example, the 'current' row or rowId. But since you have buttons in the row, this shouldn't be a problem. Unfortunately, I don't have time to create examples for you.

0


source


I am trying with uptate = "@parent: componentAnotherColumn" and it works. Only update the same line

 <p:column>    
       <p:selectBooleanCheckbox value="#{atributoVar.booleanValue}">
            <p:ajax process="@this" update="@parent:componentAnotherColumn"/>
       </p:selectBooleanCheckbox>
 </p:column>
 <p:column>                            
     <p:selectBooleanCheckbox id="componentAnotherColumn" value="#{!atributoVar.booleanValue}"/>
 </p:column>                    

      

0


source


Not sure if I'm late to the party, but let me give 5 cents on this question on how to update the content of a string using simple ajax functions from PrimeFaces.

1) Partial update: My first approach to updating anything on a specific row using PF is not exactly updating the entire row, but updating the regions of interest within the row. This can be achieved by defining retaining panels and updating them. For example:

<h:form id="testForm">
    <p:dataTable id="myEntityTable" var="myEntity" value="#{myController.allEntities}">

        <p:column headerText="id">
            <h:outputText value="#{myEntity.id}"/>
        </p:column>

        <p:column headerText="last update">
            <p:outputPanel id="lastUpdatePanel">
                <h:outputText value="#{myEntity.lastUpdate}">
                    <f:convertDateTime pattern="HH:mm:ss" type="date" />
                </h:outputText>
            </p:outputPanel>
        </p:column>

        <p:column headerText="counter">
            <p:outputPanel id="counterPanel">
                <h:outputText value="#{myEntity.counter}"/>
            </p:outputPanel>
        </p:column>

        <p:column headerText="increment">
            <p:commandButton value="+"
                actionListener="#{myController.increment(myEntity)}"
                update="counterPanel, lastUpdatePanel"/>
        </p:column>
    </p:dataTable>
</h:form>

      

The trick is to update the target panels (snippet in this example update="counterPanel, lastUpdatePanel"

). The result is something like the following image:

enter image description here

2) Actually updating the line: Very often the previous approach works well enough. However, if you really need to update the row, you can follow the advice given in one of the previous answers and use the keyword @row

:

<h:form id="testForm">
    <p:dataTable id="myEntityTable" var="myEntity" value="#{myController.allEntities}" rowIndexVar="rowIndex">

        <p:column headerText="id">
            <h:outputText value="#{myEntity.id}"/>
        </p:column>

        <p:column headerText="last update 1">
            <p:outputPanel>
                <h:outputText value="#{myEntity.lastUpdate}">
                    <f:convertDateTime pattern="HH:mm:ss" type="date" />
                </h:outputText>
            </p:outputPanel>
        </p:column>

        <p:column headerText="last update 2">
            <h:outputText value="#{myEntity.lastUpdate}">
                <f:convertDateTime pattern="HH:mm:ss" type="date" />
            </h:outputText>
        </p:column>

        <p:column headerText="counter">
            <p:outputPanel>
                <h:outputText value="#{myEntity.counter}"/>
            </p:outputPanel>
        </p:column>

        <p:column headerText="increment">
            <p:commandButton value="+"
                actionListener="#{myController.increment(myEntity)}"
                update="@form:myEntityTable:@row(#{rowIndex})"/>
        </p:column>
    </p:dataTable>
</h:form>

      

The trick is to keep each content from the columns inside p:outputPanel

and let the update="@form:myEntityTable:@row(#{rowIndex})"

work get done. I deliberately leave the last update 2 column without the outputPanel to illustrate this point:

enter image description here

Therefore, while the "last update 1" and "counter" columns are actually updated after pressing the "+", the "last update 2" column remains unchanged. Therefore, if you need to update the content, close it outputPanel

. It should also be noted that an explicit ID is not required for outputPanels containing the contents of each column (you can add one if you like). For completeness, I've used PF 6.2 in these examples. The back is pretty simple:

package myprefferedpackage;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

@ManagedBean
@ViewScoped
public class MyController {

    private List<MyEntity> entities;

    @PostConstruct
    public void init() {
        this.entities = new ArrayList<MyEntity>(10);
        for(int i = 0; i < 10; ++i) {
            this.entities.add(new MyEntity(i));
        }
    }

    public List<MyEntity> getAllEntities() {
        return entities;
    }

    public void increment(MyEntity myEntity) {
        myEntity.increment();
    }

    public class MyEntity {
        private int id;
        private int counter;
        private Date lastUpdate;

        public MyEntity(int id) {
            this.id = id;
            this.counter = 0;
            this.lastUpdate = new Date();
        }

        public void increment() {
            this.counter++;
            this.lastUpdate = new Date();
        }

        public int getId() {
            return id;
        }

        public int getCounter() {
            return counter;
        }

        public Date getLastUpdate() {
            return lastUpdate;
        }
    }
}

      

0


source







All Articles