Highlight row when single selection with a command component - events

Based on the primefaces documentation:
Single Selection with a Command Component
This method is implemented with a command component such as commandLink or
commandButton. Selected row can be set to a server side instance by passing as a parameter if you
are using EL 2.2 or using f:setPropertyActionListener.
<p:dataTable var="car" value="#{carBean.cars}">
<p:column>
<p:commandButton value="Select">
<f:setPropertyActionListener value="#{car}"
target="#{carBean.selectedCar}" />
</p:commandButton>
</p:column>
...columns
</p:dataTable>
I need the row get highlighted when I press the button without selecting the row directly
This is my code:
<p:dataTable
rowKey="${xxx.y1}-${xxx.y2}"
selection="${managedBean.selectedRow}"
selectionMode="single"
value="#{managedBean.listOfBeans}" var="xxx">
<p:column>
<p:commandButton action="${managedBean.saveSomethingInDB}"
update="vvvComponent">
<f:setPropertyActionListener value="#{currentRow}"
target="#{managedBean.selectedRow}" />
</p:commandButton>
</p:column>
</p:dataTable>

I solved it in this way:
<p:dataTable
rowKey="${xxx.y1}-${xxx.y2}"
selection="${managedBean.selectedRow}"
selectionMode="single"
value="#{managedBean.listOfBeans}" var="xxx">
<p:column>
<p:commandButton action="${managedBean.saveSomethingInDB}" onclick="highlightRow(jQuery(this));"
update="vvvComponent">
<f:setPropertyActionListener value="#{currentRow}"
target="#{managedBean.selectedRow}" />
</p:commandButton>
</p:column>
</p:dataTable>
function highlightRow(commandButton) {
$('.ui-state-highlight').removeClass('ui-state-highlight');
commandButton.closest('table').closest('tr').addClass('ui-state-highlight');
}

Your code works for me.
thanks to the propertyActionListener, the row is highlighted when I press the button
<f:setPropertyActionListener value="#{currentRow}"
target="#{managedBean.selectedRow}" />

i know its too late but this works
<p:commandButton action="${managedBean.saveSomethingInDB}" onclick="$('.ui-state-highlight').removeClass('ui-state-highlight');PF('tableWidgetVar').selectRow($(this).parents('tr:first').addClass('ui-state-highlight'));" >
<f:setPropertyActionListener value="#{currentRow}"
target="#{managedBean.selectedRow}" />
</p:commandButton>
and this way u dont need to selection or select mode on datatable

Related

Getting Primefaces datatable row variable via non-ajax button post request

I have a datatable and polling in my Primefaces page. On Datatable on every row there is a commanButton. Because of polling, f:setPropertyActionListener does not works properly on button click. So I set ajax=false at button and trying to get datatable row "var" via a POST request. Is there a way to do that?
<p:poll interval="15"
listener="#{batchOperation.generateImportFTDBatchFileReport}"
update=":batchOperationsForm:growl :batchOperationsForm:batchfilestbl
:batchOperationsForm:dataimporttypeselectiontabview:importFTDbatchfilestbl
:batchOperationsForm:dataimporttypeselectiontabview:importFTDerrorbatchfilestbl
:batchOperationsForm:dataimporttypeselectiontabview:importFTDStatusTxtId"
widgetVar="myPoll" autoStart="true"/>
<p:dataTable id="batchfilestbl" var="batchFile"
value="#{batchOperation.batchFileModel}" paginator="true"
rows="#{batchOperation.batchFileModel.pageSize}"
paginatorPosition="bottom"
sortBy="#{batchOperation.createTime}"
sortOrder="descending"
emptyMessage="#{messages['common.datatable.emptymessage']}"
selectionMode="single"
selection="#{batchOperation.selectedFile}">
<p:column headerText="#{messages['content.batchoperations.datatable.header']}">
<p:commandButton actionListener="#{batchOperation.createBatchFileForDownloadLatestRevision}"
id="excelCreate" disabled="#{disableExcelVar}"
value="#{messages['content.batchoperations.createexcel.button.label']}"
ajax="false">
<f:setPropertyActionListener value="#{batchFile}"
target="#{batchOperation.selectedFile}" />
</p:commandButton>
</p:column>
</p:dataTable>
If you want to send the whole model object as a parameter to a backing bean method, you can send it in using your var for the data table.
<p:dataTable var="myVar">
<p:column>
<p:commandButton actionListener="#{bean.method(myVar)}" />
</p:column>
</p:dataTable>
Alternatively, if you are only interested in the row index, you can send just that using the rowIndexVar value for the data table.
<p:dataTable rowIndexVar="rowIndex">
<p:column>
<p:commandButton actionListener="#{bean.method(rowIndex)}" />
</p:column>
</p:dataTable>
It looks like you are trying to do file download here. You may want to checkout this question.
Instead of using f:setPropertyActionListener, f:attribute solved my problem.
<p:commandButton actionListener="#{batchOperation.createBatchFileForDownloadLatestRevision}"
id="excelCreate" disabled="#{disableExcelVar}"
value="#{messages['content.batchoperations.createexcel.button.label']}"
ajax="false">
<f:attribute name="bf" value="#{batchFile}" />
</p:commandButton>
Method:
public synchronized void createBatchFileForDownloadLatestRevision(ActionEvent event) throws Exception {
selectedFile = (BatchFile) ((CommandButton) event.getSource()).getAttributes().get("bf");
.
.
}

Primefaces validate cell editing values

I have a datatable, where I add rows dynamically (a button click), and let users edit those rows with primefaces cell editing.
I want to add validation for the cells, however, the validation does not occur when the input texts (in the cell) are pristine.
In order for the validation to work, the user have to actually write something in the input text (make it dirty), then deletes it, only then the validation (required for this case) will be kicked-in.
example code:
<f:view>
<p:growl autoUpdate="true"/>
<h:form>
<p:commandButton value="add row" update="table" action="#{myBean.addRow}"/>
<p:dataTable value="#{myBean.users}" var="var" id="table" editMode="cell" editable="true">
<p:column headerText="Quantity">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{var.name}"/>
</f:facet>
<f:facet name="input">
<p:inputText value="#{var.name}" required="true">
<f:validateRequired />
</p:inputText>
</f:facet>
</p:cellEditor>
</p:column>
</p:dataTable>
<p:commandButton value="submit" action="#{myBean.submit}"/>
</h:form>
</f:view>
Using primefaces 5.3 and JSF 2.2.
Update:
This bug is fixed since version 6.2
https://github.com/primefaces/primefaces/issues/2512

Primefaces two dataTable update and add data from one to another

I have two dataTables in my JSF page they are placed in a dialog that pops up on some action, these dataTables has data from a list in my managed bean, what I am trying to do is when I double click on a row in my first data I want to add it to the second one and I have achieved that but it only occurs when I close the dialog and open it again, I have tried to use ajax update to update my second dataTable on the action but the issue is still occurring and my other issue that I want to HIDE the data from the first dataTable when I move it to the second one without removing it from the list or (at least dont allow the user to move the same item twice) any suggestions please? Here is my dialog and data tables:
<p:dialog modal="true" width="50%" height="50%" widgetVar="test"
id="dialog">
<p:dataTable id="tblAllQuestions" value="#{mbInstructor.fullList}"
var="asEmp" selectionMode="single"
selection="#{mbInstructor.currentExamQuestion}" rowKey="#{asEmp.id}"
rowIndexVar="rowIndex">
<p:ajax event="rowDblselect"
update="#parent:tblAllQuestions2"
process="#this" listener="#{mbInstructor.submitForm()}" />
<p:column headerText="#" width="20">
#{rowIndex+1}
</p:column>
<p:column headerText="#{msg2.get('employee')}" width="200">
#{asEmp.questionTxt}
</p:column>
</p:dataTable>
<p:dataTable id="tblAllQuestions2"
value="#{mbInstructor.formsQuestion}" var="as" selectionMode="single"
selection="#{mbInstructor.currentExamQuestion}" rowKey="#{as.id}"
rowIndexVar="rowIndex">
<p:ajax event="rowDblselect"
update="#parent:pnlfourmData"
process="#this" />
<p:column headerText="#" width="20">
#{rowIndex+1}
</p:column>
<p:column headerText="#{msg2.get('employee')}" width="200">
#{as.question.questionTxt}
</p:column>
</p:dataTable>
<h:outputText value="[no selection made]"
rendered="#{empty mbInstructor.model.target}" />
<p:commandButton id="formSubmit" value="Submit"
actionListener="#{mbInstructor.submitForm()}" style="margin-top:5px" />
</p:dialog>
I have tried to update the whole dialog as well but it disappear when i do the action, so I added the following to my ajax to show it again
oncomplete="PF('test').show()"
but the dialog does not show up when it finish.

How to call method on ticking the SelectAll checkbox in p:dataTable through ajax

I need your help in calling a method from a backing bean once I ticked the SelectAll checkbox at the top of a dataTable. I am able to call the method ShowTotal
once I ticked or unticked the checkbox of each individual item, however when I tick the SelectAll checkbox from the top, no listener is called. Here is my code:
<p:dataTable id="PendingRequests" var="hr" selection="#{hrdirector.selectedRequests}"
value="#{hrdirector.listPendingRequests}" rowKey="#{hr.requestNo}"
filteredValue="#{hrdirector.filteredRequests}" widgetVar="dataTableWidgetVar">
<p:ajax event="rowSelectCheckbox" listener="#{hrdirector.ShowTotal}" process="#this"
update=":form:PendingRequests:sum"/>
<p:ajax event="rowUnselectCheckbox" listener="#{hrdirector.ShowTotal}" process="#this"
update=":form:PendingRequests:sum"/>
<p:column selectionMode="multiple" style="width:16px;text-align:center"></p:column>
<p:column headerText="Request No.">
<h:outputText value="#{hr.requestNo}"/>
</p:column>
<p:column headerText="Request Amount">
<h:outputText value="#{hr.requestAmount}"/>
</p:column>
</p:dataTable>
<h:outputText id="Sum" value="#{hr.Sum}"/>
I tried to add the below code in the column which it has the selection, but it gave me an error:
<p:column selectionMode="multiple" style="width:16px;text-align:center">
<p:ajax listener="#{hrdirector.ShowTotal}" />
</p:column>
Page 172 of the fabulous PrimeFaces documentation has the answer for you. What you need is called the toggleSelect event.

Datatable not refreshing with ajax

I have two calendar controls and one command button. basically i want to give a range of date to pick the data from the database and then when i press the command button the datatable is displayed in the dialog box, the problem is java bean class is working it is loading the exact data which i want, but the datatable is not refreshing with command button click
my code is given;
<h:form id="form" >
<p:calendar value="#{calendarBean.dateFrom}" id="calFrom" pattern="yyyy-mm-dd">
<p:ajax event="dateSelect" listener="#{calendarBean.handleDateSelectFrom}" />
</p:calendar>
<p:calendar value="#{calendarBean.dateTo}" id="calTo" pattern="yyyy-mm-dd">
<p:ajax event="dateSelect" listener="#{calendarBean.handleDateSelectTo}" />
</p:calendar>
<p:commandButton value="Submit" action="#{calendarBean.submit()}" update="fTable" onclick="aDlg.show()">
<f:ajax render=":form:fTable" execute="#form"></f:ajax>
</p:commandButton>
<p:dialog id="aDialog" header="Filter List" widgetVar="aDlg"
modal="true" showEffect="explode"
hideEffect="explode" >
<p:panel id="pnl">
<p:dataTable id="fTable" value="#{calenderBean.list}" var="row" >
<p:column headerText="ID">
<h:outputText value="#{row.ID}"/>
</p:column>
<p:column headerText="Name">
<h:outputText value="#{row.Name}"/>
</p:column>
<p:column headerText="Time">
<h:outputText value="#{row.Time}"/>
</p:column>
<p:column headerText="User">
<h:outputText value="#{row.userName}"/>
</p:column>
</p:dataTable>
</p:panel>
</p:dialog>
</h:form>
kindly guide me regarding this issue
First thing to do, is remove the f:ajax, it's not working good with Primefaces. Also the update attribute should do everything.
This said, I have the very same code as yours which is working good.
If removing f:ajax is not working, try to check your managed bean scope (May be it's of request scope and data is re-initialized with every request)
Try Updating the dialog instead.
update="fTable"

Resources