Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use p:ajax to update several components in order

I'm trying to render the following JSF page:

<h:form id="form" prependId="false">
    <h:panelGrid width="100%">
        <h:panelGroup id="tableDiv" layout="block">
            <h:panelGroup layout="block" style="text-align: center;">
                <p:dataTable id="table" var="_item" 
                        value="#{primeBean.findTableModel()}">
                        ...
                </p:dataTable>
            </h:panelGroup>
        </h:panelGroup>
        <h:panelGrid columns="2" width="100%">
            <h:panelGroup id="barChartDiv" layout="block">
                <p:barChart id="barChart"
                    value="#{primeBean.findCartesianModel()}">
                    <p:ajax event="itemSelect" listener="#{primeBean.handleItemSelect}"
                            update="pieChartDiv,tableDiv" />
                </p:barChart>
            </h:panelGroup>
            <h:panelGroup id="pieChartDiv" layout="block">
                <h:panelGroup layout="block">
                    <p:pieChart id="pieChart">
                        <p:ajax event="itemSelect" listener="#{primeBean.handleItemSelect}" 
                                update="tableDiv" />
                    </p:pieChart>
                </h:panelGroup>
            </h:panelGroup>
        </h:panelGrid>
    </h:panelGrid>
</h:form>

When I click in any bar on p:barChart, I expect that the components in the update attribute would be rendered in the order that I declare (pieChartDiv,tableDiv), but they are rendered in reverse order (tableDiv,pieChartDiv).

Is this normal behavior? How can I update a component list in the order the items are declared?

like image 684
gustavodevmax Avatar asked Dec 11 '22 16:12

gustavodevmax


2 Answers

That's not possible this way. The components are searched and updated in exactly the same order as they appear in the JSF component tree.

One way would be to rearrange/swap the both components in the tree hierarchy and reposition them visually with help of CSS. This is however rather clumsy.

Another way would be to update the 2nd component on complete of the update of the 1st component (and thus effectively end up with 2 ajax requests instead of 1). You can achieve that with help of <p:remoteCommand> which is invoked on complete of the <p:ajax>.

So, instead of

<p:ajax ... update="pieChartDiv,tableDiv" />

use

<p:ajax ... update="pieChartDiv" oncomplete="updateTableDiv()" />
...
<p:remoteCommand name="updateTableDiv" update="tableDiv" />
like image 87
BalusC Avatar answered Dec 28 '22 23:12

BalusC


Not so elegant but working solution can be this:

<p:barChart id="barChart" 
    value="#{primeBean.findCartesianModel()}">
    <p:ajax event="itemSelect" listener="#{primeBean.handleItemSelect}" 
        update="pieChartDiv" /> <!-- This will be updated as first. -->
    <p:ajax event="itemSelect" 
        update="tableDiv" /> <!-- And this will be updated as second. -->
</p:barChart>
like image 34
Viktoria Nora Ress Avatar answered Dec 29 '22 01:12

Viktoria Nora Ress