2

I have a requirement as follows

A datatable with checkbox select option and row select also retain the multi selection,

When the user clicks on the check box the respective rows should selected and at the same time if any row of the datatable is selected by row select event that should retian the exisiting selected item and the current selection also vice versa.

Due to the primefaces frawork limitation I could not achieve this requirement, can any one help me to achieve this requirement

Sample Xhtml :

<p:dataTable id="checkboxDT" var="car" value="#{dtSelectionView.cars6}" selection="#{dtSelectionView.selectedCars}" rowKey="#{car.id}" style="margin-bottom:0">
        <f:facet name="header">
            Checkbox
        </f:facet>
        <p:column selectionMode="multiple" style="width:16px;text-align:center"/>
        <p:column headerText="Id">
            <h:outputText value="#{car.id}" />
        </p:column>
        <p:column headerText="Year">
            <h:outputText value="#{car.year}" />
        </p:column>
        <p:column headerText="Brand">
            <h:outputText value="#{car.brand}" />
        </p:column>
        <p:column headerText="Color">
            <h:outputText value="#{car.color}" />
        </p:column>
        <f:facet name="footer">
            <p:commandButton process="checkboxDT" update=":form:multiCarDetail" icon="ui-icon-search" value="View" oncomplete="PF('multiCarDialog').show()" />
        </f:facet>
    </p:dataTable>

Sample Bean : scope : Group conversational Scope

public void onRowSelect(SelectEvent event) {
    FacesMessage msg = new FacesMessage("Car Selected", ((Car) event.getObject()).getId());
    FacesContext.getCurrentInstance().addMessage(null, msg);
}

public void onRowUnselect(UnselectEvent event) {
    FacesMessage msg = new FacesMessage("Car Unselected", ((Car) event.getObject()).getId());
    FacesContext.getCurrentInstance().addMessage(null, msg);
}

Note :

Please verify the primefaces showcase for default component behaviour

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
Kannan JB
  • 31
  • 4
  • 2
    Please post your Java code for #{employeeBean.selectedEmployeeList} so we can see it. What scope is the employeeBean? – Melloware Jul 30 '18 at 16:21
  • 1
    Better yet, post a [mcve] – Kukeltje Jul 30 '18 at 17:16
  • My EmployeeBean.java is in GroupConversationScope public void rowSelect(Employee employee) { this.selectedEmployeeList.add(employee); } public void rowUnSelect(Employee employee) { this.selectedEmployeeList.remove(employee); } – Kannan JB Aug 01 '18 at 10:00
  • 1
    Please edit your question to include the entire code for EmployeeBean. Also make sure to study this working example: https://www.primefaces.org/showcase/ui/data/datatable/selection.xhtml I have no problem with selection so keep debugging and simplifying your use case until you figure it out. – Melloware Aug 01 '18 at 18:05
  • There is no sample xhtml visible in your question... – Kukeltje Aug 02 '18 at 10:02

1 Answers1

2

"Due to the primefaces frawork limitation I could not achieve this requirement, can any one help me to achieve this requirement"

It's not a limitation, it is as designed. With ctrl-click you can add additional rows when clicking on them. But if you want to change this behaviour, the relevant info is in on page 161 of the PrimeFaces 6.2 documentation.

rowSelectMode     new     String     Defines row selection mode for multiple selection.
                                     Valid values are "new", "add" and "checkbox".

So adding

rowSelectMode="add"

to your datatable most likely does the trick.

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
  • Thanks for your valuable quick response. Now the selection is working fine but when I am unselect the row it is not unselecting the checkbox. – Kannan JB Aug 02 '18 at 10:29
  • That was not in your question ;-) (use ctrl) Effectively your question is now a duplicate of https://stackoverflow.com/questions/31591407/do-not-unselect-rows-when-clicking-on-another-rows-in-primefaces-datatable – Kukeltje Aug 02 '18 at 10:43
  • Could you please provide he patch or some solution for this.? I think that question is with Primefaces 3.5 but mine is later version Primefaces 5.0 still i need to fire the rowUnselect event. – Kannan JB Aug 02 '18 at 11:16
  • The source is open... Download it, check it, create a patch... or disable clicking on a row in general by adding `rowSelectMode="checkbox"` https://stackoverflow.com/questions/31709425/how-to-avoid-unselection-when-selecting-a-single-row-in-primefaces-datatable – Kukeltje Aug 02 '18 at 11:21