简体   繁体   中英

Primefaces datatable with selection attribute - dynamic tables

First some explain:

I create dynamically an accordeonpanel and in each Tab i create ap:datatable ( also dynamically).

I now have to edit the lines of the tables but my method in the "selection" is not called. I think it's because of the above explained dynamism.

the method is: evaluationController.deleteIndicator()

Does anyone have an idea how to fix that?

Here the code:

XHTML:

<!-- **************************************** ACCORDEON **************************************** -->
            <p:panel id="mainAccordeonHeader" header="Capacités et dégrés de maîtrise" styleClass="panelSearch">
                <h:form id="evalAccordionForm" styleClass="orgForm">      

                    <p:accordionPanel id="evalAccordion" 
                                      styleClass="orgAccordion" 
                                      value="#{evaluationController.availableCapacitys}"
                                      var="capListItem">
                        <p:tab id="evalAccordTitle">
                            <f:facet name="title">
                                <h:panelGrid columns="4">
                                    <h:outputText value="#{capListItem.name}"/>                                      
                                    <h:outputText value="#{capListItem.isThresholdOfSuccess ? 'Capacité Déterminante' : 'Degré de Maitrise'}" />
                                    <h:outputText value="Nr Indicateurs: #{evaluationController.findNumberOfIndicators(capListItem)}"/>
                                    <h:outputText value="Pondération: #{evaluationController.findCapacityPonderation(capListItem)}"/>
                                </h:panelGrid>
                            </f:facet>
                            <!-- CAPACITY DETAIL-->
                            <table border="0" >
                                <thead>
                                    <tr>
                                        <th>
                                            <p>Description</p>
                                        </th>
                                        <th>
                                            <p style="float: right">Pondération</p>
                                        </th>
                                    </tr>
                                </thead>
                                <tbody>                                     
                                    <tr>
                                        <td>
                                            <p:inputTextarea id="capDescTextArea" rows="3" cols="113" value="#{capListItem.description}" readonly="true"/>
                                        </td>
                                        <td>
                                            <p:inputTextarea style="float: right" id="capPonderationTextArea"  rows="1" cols="1" readonly="true" value="#{evaluationController.findCapacityPonderation(capListItem)}"/>                                           
                                        </td>
                                    </tr>
                                </tbody>
                            </table>                                    
                            <p:separator />
                            <!-- INDICATORS-->
                            <h:form>
                                <p:contextMenu for="indicatorTable">                               
                                    <p:menuitem value="Modifier" update="indicatorTable" icon="ui-icon-wrench" actionListener="#{evaluationcontroller.updateIndicator()}"/>
                                    <p:menuitem value="Supprimer" update="indicatorTable" icon="ui-icon-trash" actionListener="#{evaluationController.deleteIndicator()}"/>
                                    <p:menuitem value="Ajouter" update="indicatorTable" icon="ui-icon-plus" actionListener="#{evaluationController.addIndicator()}"/>
                                    <p:separator/>
                                     <p:menuitem value="Annuler action"/>
                                </p:contextMenu>
                                <p:dataTable
                                    id="indicatorTable"
                                    value="#{evaluationController.findIndicatorsByCapacity(capListItem)}"                                      
                                    var="indicator"
                                    editable="true"
                                    tableStyle="indicatorTableStyle"     
                                    rowKey="#{indicator.id}"
                                    selection="#{evaluationcontroller.selectedIndicatorRow}" selectionMode="single"
                                    >
                                    <f:facet name="header">Les Indicateurs ( #{evaluationController.findNumberOfIndicators(capListItem)})
                                    </f:facet>
                                    <p:column headerText="Org Ut" >
                                        <h:outputText value ="#{indicator.organizedUt.id}"/>
                                    </p:column>
                                    <p:column headerText="Date">
                                        <h:outputText value ="#{indicator.organizedUt.toString()}"/>
                                    </p:column>
                                    <p:column headerText="Nom">
                                        <h:outputText value ="#{indicator.name}"/>
                                    </p:column>
                                    <p:column headerText="Description" toggleable="false" width="450">
                                        <p:outputLabel value ="#{indicator.description}"  />
                                    </p:column>
                                    <p:column headerText="Max" toggleable="false" width="30">
                                        <p:inputTextarea style="float: none"   rows="1" cols="1" readonly="true" value="#{indicator.maxPossible}"/> 
                                    </p:column>
                                </p:dataTable>    
                                <p:commandButton styleClass="addButtonStyle" icon="ui-icon-plus" value="Ajouter un indicateur"/> 
                                <p:commandButton styleClass="deleteAllButtonStyle" icon="ui-icon-trash" value="Supprimer tous les indicateurs" actionListener="#{evaluationController.deleteIndicator()}"/> 

                            </h:form>
                        </p:tab>
                    </p:accordionPanel>   
                </h:form>
            </p:panel>

CONTROLER:

Here is the problem : the Getter Setter are not called when i select a row!

public Indicator getSelectedIndicatorRow() {
    System.out.println("GETTER - selectedIndicatorRow");
    return selectedIndicatorRow;
}

public void setSelectedIndicatorRow(Indicator selectedIndicatorRow) {
    this.selectedIndicatorRow = selectedIndicatorRow;
    System.out.println("SETTER - selectedIndicatorRow");
}


public void deleteIndicator(){
    System.out.println("Function: deleteIndicator");
    try {
        System.out.println("TRY TO DELETE  INDICATOR");
        indicatorFacade.remove(selectedIndicatorRow);

        List<Indicator> tempIndicatorsList = actualGridHM.get(selectedIndicatorRow.getCapacity());
        actualGridHM.remove(selectedIndicatorRow.getCapacity());
        tempIndicatorsList.remove(selectedIndicatorRow);
        actualGridHM.put(selectedIndicatorRow.getCapacity(), tempIndicatorsList);
        System.out.println("INDICATOR DELETED!");
    }
    catch (Exception e) {
        System.out.println("ERROR - DELETE OF INDICATOR NOT WORKED!");
    }

Does anyone have a solution? Maybe an another way to edit this dynamic table ( placed in a dynamic accordeon)??

First obvious problem with your code is that you have two <h:form> . You have to delete the inner <h:form> and test again.

Try this way below; put your command buttons in facets at the bottom of your datatable like

<p:dataTable>
  ...
  <f:facet>
    <p:commandButton styleClass="addButtonStyle" icon="ui-icon-plus" value="Ajouter un indicateur"/> 
    <p:commandButton styleClass="deleteAllButtonStyle" icon="ui-icon-trash" value="Supprimer tous les indicateurs" action="#{evaluationController.deleteIndicator}"/> 
  </f:facet>
</p:dataTable>

and use "action" attribute instead of "actionListener" without '()' at the end of your action method in your command button or add ActionEvent parameter to your action method.

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