Last active
August 29, 2015 14:02
-
-
Save bhdrk/16583928e22735c5c632 to your computer and use it in GitHub Desktop.
primefaces dataTable limited row selection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <p:dataTable var="car" value="#{listBean.cars}" | |
| selection="#{listBean.selectedCars}" | |
| rowKey="#{car.id}" | |
| paginator="true" rows="10" | |
| widgetVar="myDataTable"> | |
| <p:column selectionMode="multiple" style="width:2%;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="Manufacturer"> | |
| <h:outputText value="#{car.manufacturer}"/> | |
| </p:column> | |
| <p:column headerText="Model"> | |
| <h:outputText value="#{car.model}"/> | |
| </p:column> | |
| <p:column headerText="Color"> | |
| <h:outputText value="#{car.color}"/> | |
| </p:column> | |
| </p:dataTable> | |
| <script type="text/javascript"> | |
| $(function () { | |
| var MAX_ROW_SELECTION_COUNT = 4; | |
| if (myDataTable.isSelectionEnabled()) { | |
| var dataTableId = myDataTable.jqId; | |
| // hide check all button | |
| $(dataTableId + ' thead:first > tr > th.ui-selection-column .ui-chkbox-all').hide(); | |
| var checkboxes = $(dataTableId + ' tbody.ui-datatable-data:first > tr > td.ui-selection-column .ui-chkbox-box'); | |
| checkboxes.each(function (index, element) { | |
| var chckbx = $(element); | |
| chckbx.on("click", function (e) { | |
| var disabled = chckbx.hasClass('ui-state-disabled'), | |
| checked = chckbx.hasClass('ui-state-active'); | |
| if (!(checked || disabled)) { | |
| if (myDataTable.getSelectedRowsCount() >= MAX_ROW_SELECTION_COUNT) { | |
| alert('You cannot select more than ' + MAX_ROW_SELECTION_COUNT + ' rows.'); | |
| return false; | |
| } | |
| } | |
| }); | |
| }); | |
| } | |
| }); | |
| </script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment