Primefaces sum the particular column to show in footer - primefaces

I am using PrimeFaces 6.0 in my application.I use one of my page like bank challan.If user has type count to caluclate amount and total amount.
<p:column headerText="Denomination">
<h:outputText value="#{bank.denominationAmt}"/>
</p:column>
<p:column headerText="Count">
<p:inputText value="#{bank.count}"/>
</p:column>
<p:column headerText="Amount">
<p:inputText value="#{bank.amt}"/>
</p:column>
....
I need to user type count or amount to javascript or primefaces to show sum of amount show in footerText is possible?

You have to calculate totals in managed bean, then add a columnGroup in footer to show'em :
<p:columnGroup type="footer">
<p:row>
<p:column style="text-align:right" footerText="Totals:" />
<p:column style="text-align:right" footerText="#{managedBean.amount}" />
...
...
</p:row>
</p:columnGroup>
you need to set p:ajax with change event in the amount inputtext to recalculate the sum of amount and update the total column.

Related

Primefaces datatable filter specific column in header

In Primefaces datatable, it can filter a specific column by the following code:
<p:column filterBy="#{car.id}" headerText="Id" filterMatchMode="contains">
<h:outputText value="#{car.id}" />
</p:column>
The code above will generate a filter on the header of the column. What I want is create a filter on the header of datatable, not column. From the ShowCase, there is a code:
<f:facet name="header">
<p:outputPanel>
<h:outputText value="Search all fields:" />
<p:inputText id="globalFilter" onkeyup="PF('carsTable').filter()"/>
</p:outputPanel>
</f:facet>
This will put a filter in header of datatable, but it will filter the all fields of the datatable, which is what I don't want, I want it to filter specific column only, is it possible?
You could by using a few hacks. First add a widgetVar attribute to your p:dataTable, for example widgetVar="myTable". Then, add an id attribute to you p:column:
<p:column filterBy="#{car.id}"
headerText="Id"
filterMatchMode="contains"
id="clm">
<h:outputText value="#{car.id}" />
</p:column>
You can use this id to select the column filter field. In the global filter, you want to remove the id attribute, and add an onkeyup listener which sets the input's value in the column's filter and triggers filtering on the table using the widgetVar.
<f:facet name="header">
<p:inputText onkeyup="document.getElementById('frm:tbl:clm:filter').value=this.value; PF('myTable').filter();"/>
</f:facet>
Here I assume your form has the ID frm and your data table has the ID tbl. They probably have different IDs, so change the code accordingly.
If you don't want the filter to be visible in the column, you could hide it with a CSS rule:
.ui-datatable .ui-column-filter[id='frm:tbl:clm:filter'] {
display: none;
}
If you are using lazy loading you could also check out Primefaces lazy datatable: put my own filter instead of GlobalFilter

How to change Primefaces Datatable filter position?

Right now by default PF Datatable showing filter position with header .Is it possible to show filter position on footer of the DataTable ?
I tried below code in Datatble Footer but not worked
<f:facet name="footer">
<p:column>
<f:facet name="filter">
<p:spinner onchange="PF('carsTable').filter()" styleClass="year-spinner">
<f:converter converterId="javax.faces.Integer" />
</p:spinner>
</f:facet>
</p:column>
<p:column>
<f:facet name="filter">
<p:spinner onchange="PF('carsTable').filter()" styleClass="year-spinner">
<f:converter converterId="javax.faces.Integer" />
</p:spinner>
</f:facet>
</p:column>
</f:facet>
No, not possible in this way. I see three options:
Use DOM manipulation to move the filters to the corresponding tags in the bottom. Do not forget to do this after each ajax call. And by hiding the filters in the header with css anyway, you prevent a 'jumpy' userinterface
Using OmniFaces' moveComponent showcase.omnifaces.org/components/moveComponent as proposed by #MathieuCastets
Change the source of the PrimeFaces dataTable and create your own component.

primefaces datatable is showing blank rows. Showing the same number of rows as the records in backed list

Thanks to all those who are responding to the problems of the developers.
I am facing some issue with primefaces 5.0 in datatable.
i am having one datatable with 14 records into that backed by list as usual. It was working fine till the time i added spring into my JSF application.
list backed in datatable is getting data from database though one method which is setting the values into the list(not filling data in getter method).
now when i am checking in getter of the list, it is having 14 records into the list. The problem is datatable is not showing the values for those records. It is showing blank rows for that. It is showing 14 blank records for that.
if i am trying to print the value of the records of list it is printing fine.
i tried to see the type of the records of the list that is also the desired type.
if i will try to print the perticular value of the field of the record of the list outside of the datatable that is also printing fine in xhtml page but it is not showing the same into the datatable.
i didn't change any sysntex as it was working fine before adding spring into it.
please help me for this. Thanks to all of you. I am showing the xhtml code below.
<p:dataTable id="datatable" var="variable" value="#{myClass.existingRecordList}" resizableColumns="true"
paginator="true" rows="10" editable="true" style="font-size:12px" paginatorAlwaysVisible="true"
paginatorPosition="top" rowIndexVar="rowIndex" rowsPerPageTemplate="5,10,15,20"
paginatorTemplate="Displaying {RowsPerPageDropdown} records / page {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {CurrentPageReport}"
currentPageReportTemplate="{startRecord} - {endRecord} of {totalRecords}" >
<p:columnGroup type="header" >
<p:row>
<p:column headerText="#" style="text-align:center;white-space:normal;width:25px"/>
<p:column headerText="Field1" style="text-align:center;white-space:normal;width:50px;vertical-align:bottom"
sortBy="#{variable.field1}" filterBy="#{variable.field1}" filterMatchMode="contains" rendered="#{otherClass.field1}" />
<p:column headerText="Field2" style="text-align:center;white-space:normal;width:50px;vertical-align:bottom"
sortBy="#{variable.field2}" filterBy="#{variable.field2}" filterMatchMode="contains" rendered="#{otherClass.field2}"/>
<p:column headerText="Field3" style="text-align:center;white-space:normal;width:50px;vertical-align:bottom"
sortBy="#{variable.field3}" filterBy="#{variable.field3}" filterMatchMode="contains" rendered="#{otherClass.field3}"/>
</p:row>
</p:columnGroup>
<p:column headerText="#" style="text-align:center;white-space:normal;width:20px" exportable="false">
<h:outputText value="#{rowIndex+1}" style="text-align:center"/> <!-- This is printing the value -->
</p:column>
<p:column headerText="Field1" style="text-align:center;" rendered="#{otherClass.field1}">
<h:outputText value="#{variable.field1}" style="text-align:center;white-space:normal"/><!-- This is not printing the value showing blank-->
</p:column>
<p:column headerText="Field2" style="text-align:center;" rendered="#{otherClass.field2}">
<h:outputText value="#{variable.field2}"/> <!-- This is not printing the value showing blank-->
</p:column>
<p:column headerText="Field3" style="text-align:center;" rendered="#{otherClass.field3}">
<h:outputText value="#{variable.field3}"/> <!-- This is not printing the value showing blank-->
</p:column>
</p:dataTable>
<h:outputText value="This is the value #{myClass.existingRecordList.get(0).field1}"></h:outputText> <!-- This is printing the value -->

How to simplify sorting in Primefaces p:dataTable component. Unexpected sorting icons founded

I want to incorporate simple sorting to datatable in Primefaces.
My code is:
<p:dataTable id="tbl" var="data" value="#{dataView.avaliableData}"
paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink}"
paginator="true" rows="10" sortMode="simple" paginatorPosition="bottom">
<p:column headerText="#{msg.id}" sortBy="#{data.id}">
<h:outputText value="#{data.id}" />
</p:column>
<p:column headerText="#{msg.name}" sortBy="#{data.name}">
<h:outputText value="#{data.name}" />
</p:column>
<p:column headerText="#{msg.entity}" sortBy="#{data.entity}">
<h:outputText value="#{data.entity}" />
</p:column>
<p:column headerText="#{msg.date}" sortBy="#{data.date}">
<h:outputText value="#{data.date}" />
</p:column>
</p:dataTable>
But the result is so confusing. This picture illustrates it (I don't have enough reputation to post it here directly).
Click here for viewing the datatable anomaly example
Why do I see several sorting items? I just want to see only an icon which sorts in ascending or descending order.
You just remove sortMode = "simple" and you will see only one sort icon and to show the paginator only it exceeds the number of rows use this.
paginatorAlwaysVisible="false"
in dataTable tag.
Apparently it was caused by the misapplication of the stylesheet incorporating background icon.
I gather that it was due to the version of Primefaces. I have overwritten the
.ui.datatable .ui-sortable-column-icon
entry in my stylesheet and the strange icons has disappeared.

Bug when editing date field in DataTable component

I've got a weird bug when editing a date field using a Calendar component inside of an editable DataTable. To explain, I'm going to need to show you some pictures.
First, here is the actual page I'm working from:
Now, let's change date 1 in the list to the 13th of February and click OK.
As you can see, the table changes, but the row takes on another row's data.
Other than in the GUI, the update's gone just fine, and if we reload the page the date has been updated in the database. This is what the table looks like after the page has been refreshed:
I've turned the code inside and out trying to get at this bug, and I'm starting to wonder if it's a bug in PrimeFaces itself. I've stepped through the entire workflow and have concluded that the backing ArrayList has the correct values at all times. I was thinking it may have had something to do with me sorting the list both when fetching it from the database and using the sortBy attribute of the DataTable. After disabling both however, the bug continues to manifest itself.
Here's my code for the page. Can anyone see anything I've done wrong? If so, you're my hero =)
<ui:composition template="/WEB-INF/standard-page-template.xhtml">
<ui:define name="title">Change Dates</ui:define>
<ui:define name="content">
<p:panel header="Change dates" style="width:760px">
<h:form>
<p:panel>
Just the search stuff here...
</p:panel>
<p:panel header="Search">
<h:panelGrid columns="1">
<p:dataTable id="listDatesTable" value="#{view.dateWrappers}" var="wrapper"
rowIndexVar="index" editable="true"
emptyMessage="Search for an ID above">
<p:ajax event="rowEdit" listener="#{changePublishDateView.changeDate}"/>
<p:column headerText="Dragning" width="90">
<h:outputText value="#{wrapper.id}"/>
</p:column>
<p:column headerText="Beskrivning" width="180">
<h:outputText value="#{wrapper.description}"/>
</p:column>
<p:column headerText="Distribution" width="100">
<h:outputText value="#{wrapper.id2}"/>
</p:column>
<p:column headerText="Publiceringstid" width="170" styleClass="#{view.duplicateDateInRow(wrapper)}">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{wrapper.newhDate}">
<f:convertDateTime pattern="yyyy-MM-dd HH:mm"/>
</h:outputText>
</f:facet>
<f:facet name="input">
<p:calendar
mindate="#{view.minDate}" stepMinute="30" minHour="8" maxHour="22"
pattern="yyyy-MM-dd HH:mm" value="#{wrapper.newDate}"/>
</f:facet>
</p:cellEditor>
</p:column>
<p:column width="50">
<p:rowEditor/>
</p:column>
</p:dataTable>
</h:panelGrid>
</p:panel>
</h:form>
</p:panel>
</ui:define>
As it turns out, this bug was due to the data in the individual rows being mostly identical.
Solution: Set a rowKey attribute on the datatable, to the date field. Now it works fine with or without sorting.
http://www.developer.am/primefaces/?page=RowKey