1
votes

I'm using:

Server: Wildfly 8.2 JSF: 2.2 Primefaces: 5.2 JDK:8

I have a datatable with multiple selection. I use rowSelectMode='add' so clicking on different rows without modifier keys adds new rows instead of unselecting all selected ones.

<p:dataTable id="table"
          value="#{ixController.files}"
         var="item" paginatorTemplate="{FirstPageLink} {PreviousPageLink}
                                {PageLinks}
                                {NextPageLink} {LastPageLink}"
         selection="#{ixController.selecteds}"
         rowKey="#{item.id}"
         rowSelectMode="add"
         selectionMode="multiple">

<p:column headerText="Files">
    #{item.fileName}
</p:column>

As stated it works fine for adding rows, however I need to press Ctrl to unselect rows, and the customers wish to just click again on the row and unselect it.

Is that possible somehow?

2

2 Answers

0
votes

Use check box instead that way they can select and un-select just by clicking it, follow Prime Faces example:

<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>
0
votes

I cant find an attiribute for this, however, there is a way:

<p:dataTable value="#{groupBean.groups}" var="grp" rowKey="#{grp.id}"
     selectionMode="single" selection="#{groupBean.group}">

    <p:ajax event="rowSelect" listener="#{groupBean.onRowSelect}" update="@all" />

As you see, when a row clicked, selectedItem setted to groupBen.goup object.

public void onRowSelect(SelectEvent event) {
    if (prevGroupId == group.getId())
        group = new PersonelGroup();
    else
        prevGroupId = group.getId();
}

In onRowSelect method, you can check if user click same row for the second time by comparing previous selected item id. And by instanciating group object to new, DataTable has been updated to no selection state.

It is important to not forget to add update="@all" attiribute of DataTable.