select all in datatable jsf primefaces

帅比萌擦擦* 提交于 2020-02-28 15:05:34

问题


Im trying to create a select all button or check box that when clicked all the selectbooleanCheck boxes will be checked. is there no straight forward easy way.ive started creating the selectcheckbox that will when changed selectAll. thanks

 <p:dataTable value="#{illRequestModel.list}"  
                        var="illRequestRecord" width="100%" styleClass="request-table"
                        rows="10" paginator="true" id="requestGrid" 
                        currentPageReportTemplate="Viewing Page {currentPage}"
                        paginatorTemplate="{CurrentPageReport}   {PageLinks}  "
                        paginatorPosition="bottom">
                        <p:column >
                        <f:facet name="header">
                                <h:selectBooleanCheckbox id="checkbox2" title="emailUpdates2" onchange="CheckAll()" > 

                                </h:selectBooleanCheckbox>
                            </f:facet>
                            <h:selectBooleanCheckbox id="checkbox" title="emailUpdates"
                                value="#{illRequestRecord.selected}"
                                onchange="addNumber(#{illRequestRecord.localRequestId})"> 
                                <f:ajax listener="#{illRequestRecord.selectmethod}" />
                                </h:selectBooleanCheckbox>
                        </p:column>
                        <p:column id="localRequestIdCol"
                            sortBy="#{illRequestRecord.localRequestId}">
                            <f:facet name="header">
                                <h:outputText value="ID" />
                            </f:facet>
                            <h:commandLink value="#{illRequestRecord.localRequestId}"
                                action="#{requestListController.displaySingleRecord}">
                                <f:param name="selectedItemId"
                                    value="#{illRequestRecord.localRequestId}"></f:param>
                            </h:commandLink>
                        </p:column>

回答1:


Why don't you use the same multi-selection datatable used in the PrimeFaces DataTable showcase As you can see in:

 <p:dataTable id="multiCars" var="car" value="#{tableBean.mediumCarsModel}" paginator="true" rows="10" selection="#{tableBean.selectedCars}">

It will automatically add a check-all functionality. In case you want an external checkbox to check all, you can do the following. Give a widgetVar to you datatable, let's call it dataTableWV

 <p:dataTable widgetVar="dataTableWV" id="multiCars" var="car" value="#{tableBean.mediumCarsModel}" paginator="true" rows="10" selection="#{tableBean.selectedCars}">  

And you have a checkbox:

 <input id="checkAll" type="checkbox" />

You can register a click event on it like the next:

 <script>
      $(document).ready(function() {
          $('#checkAll').on('click', function() {
               //selects all records on the displayed page if pagination is supported.
               dataTableWV.selectAllRowsOnPage();

               //or you can select all the rows across all pages.
               dataTableWV.selectAllRows();
          });
      });
 </script>



回答2:


<p:dataTable widgetVar="dataTableWV" id="multiCars" var="car" value="#{tableBean.mediumCarsModel}" paginator="true" rows="10" selection="#{tableBean.selectedCars}">
  <f:facet name="header">
      <p:commandButton value="select" onclick="PF('dataTableWV').selectAllRows()" />
      <p:commandButton value="unselect" onclick="PF('dataTableWV').unselectAllRows()" />
  </f:facet>
</p:dataTable>


来源:https://stackoverflow.com/questions/22965320/select-all-in-datatable-jsf-primefaces

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!