rowsPerPageTemplate Dropdown dissapears after selection change and clicking away - jsf

I have Datatables(PrimeFaces 8.0.1) on multiple pages, only one of them had rows and rowsPerPageTemplate attributes, now i need to modify all other DataTables in the same way. All DataTables (dataTableA,dataTableB,..) are almost identical and have own #SessionScoped Beans(ControllerA, ControllerB,..). At first all DataTables seem to be OK, but if i change the rows per page selection of a dataTableX and click away to another page/dataTable and then i turn back to dataTableX, rows per page dropdown isn't there anymore (for the rest of the session). Can anyone help please? Thank you.
listViewA.xhtml
<h:form id="listAUserForm">
<p:dataTable id="dataTableA"
value="#{controllerA.listA}"
reflow="true"
paginator="true"
paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} #{msg.rows_per_page}: {RowsPerPageDropdown}"
currentPageReportTemplate="#{prop.current_page_report_template}"
paginatorAlwaysVisible="false"
var="entryA"
rows="#{controllerA.rowsPerPage}"
rowsPerPageTemplate="10,20,30,{ShowAll|'All'}"
rowKey="#{entryA}"
selection="#{controllerA.entriesSelected}">
<p:ajax event="rowSelectCheckbox" listener="#{controllerA.onRowSelect}" update=":toolbarA" />
<p:ajax event="rowUnselectCheckbox" listener="#{controllerA.onRowUnselect}" update=":toolbarA" />
<p:ajax event="toggleSelect" process="#this" partialSubmit="true" update=":toolbarA"/>
<p:column selectionMode="multiple" style="width:16px;text-align:center"/>
....
....
</h:form>
ControllerA.java
#ManagedBean(name = "ControllerA")
#SessionScoped
public class ControllerA extends BaseController
{
private int rowsPerPage = 10;
private List<EntryA> entries= null;
public int getRowsPerPage()
{
return rowsPerPage;
}
public void setRowsPerPage(int rowsPerPage)
{
this.rowsPerPage = rowsPerPage;
}
public List<EntryA> getListA()
{
if (entries== null)
{
entries= new ArrayList<>();
List<EntryA> tmpListA;
try
{
tmpListA= getSecurityServiceLocator().getBeanA().getAllEntries();
}
catch (SecurityException ex)
{
LOGGER.error("Error retrieving listA", ex);
return Collections.emptyList();
}
if (!isFilterExist())
{
entries.addAll(tmpListA);
return entries;
}
for (EntryA tmpEntryA : tmpListA)
{
if (tmpEntryA.filterMatch(filterValue.toLowerCase()))
{
entries.add(tmpEntryA);
}
}
}
return entries;
}
...

Related

Can't change page p:dataTable

I have implemented code to do a table lazy in primefaces so I have method paginated which ones retrieves info. The problem the number of each page it is not showed. So if I have 100 I only see 15 elements anymore. The page button is not showed.
Like in this screenshot:
In the bean:
#PostConstruct
public void init() {
listadoPedidos = new LazyDataModel<Pedido>() {
private static final long serialVersionUID = 1L;
#SuppressWarnings("unchecked")
#Override
public List<Pedido> load(int first, int pageSize, String sortField, sortOrder sortOrder,
Map<String, Object> filters) {
List<Pedido> lista = new ArrayList<Pedido>();
boolean ordenar = sortOrder == SortOrder.ASCENDING ? true : false;
try {
lista = commonService.obtenerListaPaginada(Pedido.class, first, pageSize, "", "", "", "", campos);
listadoPedidos.setRowCount(lista.size());
} catch (Exception e) {
e.printStackTrace();
MensajesErrores.error(e.getMessage());
}
return lista;
}
};
}
And the JSF is:
<p:dataTable
id="tablaUsuario"
rowIndexVar="secuencial"
emptyMessage="#{msgs.lbl_no_hay_datos}"
value="#{pedidoBean.listadoPedidos}"
var="_pedido"
paginator="true"
paginatorPosition="top"
style="width:100%"
selection="#{pedidoBean.pedido}"
selectionMode="single"
rowKey="#{_pedido.rowKey}"
sortBy="#{_pedido.id}"
sortOrder="DESCENDING"
lazy="true"
rows="15"
paginatorTemplate="
{CurrentPageReport}
{FirstPageLink}
{PreviousPageLink}
{PageLinks}
{NextPageLink}
{LastPageLink}
{RowsPerPageDropdown}"
rowsPerPageTemplate="15,25,50,100"
>
<p:column filterStyleClass="input-text"
styleClass="columnaDTCodigo tableCell"
headerText="#{msgs.lbl_numero}">
<h:outputText value="#{_pedido.id}" />
</p:column>
</p:dataTable>
I really donĀ“t see the error, the dropdown With number of register works well.
My mistake was in this line:
listadoPedidos.setRowCount(lista.size());
replace with
listadoPedidos.setRowCount(TOTAL_ROWS_IN_DATABASE);

How to use p:chart in a column of every row with a datatable

I would like to know if is possible to use a pie chart in a column of every row in a datatable and how to do it in JSF2.2 with primefaces showcase:
https://primefaces.org/showcase/ui/chart/pie.xhtml
My idea is to show a pie chart 100% with a color based in the id into a grid
for every row and a description.
1.blue
2.yellow
3.red.
etc...
I've try the basic code below from the showcase with no success and I have not found any similar example. My xhtml works fine but when I add the pie chart get an error.
#ManagedBean(name = "info")
#ViewScoped
public class InfoView implements java.io.Serializable {
#ManagedProperty(value = "#{infoBo}")
private InfoBo infoBo;
private Long id;
private String description;
private List<Info> listInfo;
private InfoDataModel infoDataModel;
private Info infoSelected;
private PieChartModel pieModel1;
#PostConstruct
public void init() {
createPieModels();
}
public PieChartModel getPieModel1() {
return pieModel1;
}
private void createPieModel() {
createPieModel1();
}
private void createPieModel1() {
pieModel1 = new PieChartModel();
pieModel1.set("Blue", 100);
pieModel1.setTitle("Blue Simple Pie");
pieModel1.setLegendPosition("w");
pieModel1.setShowDataLabels(true);
pieModel1.setDiameter(150);
}
public getters & setters {
}
View xhtlm:
<p:dataTable id="datatableInfo" var="varInfo" value="#{Info.infoDataModel}" widgetVar="wdatatableInfo" style="width: 98%"
paginator="true" paginatorPosition="bottom" rows="10" rowsPerPageTemplate="10,20,30,40,50"
paginatorTemplate="{FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink}"
selectionMode="single" tableStyle="table-layout:fixed;width:120" >
<p:column id="id_Reg" headerText="ID" style="width: 2%" sortBy="#{varInfo.id}" >
<h:outputText value="#{varInfo.idReg}"/>
</p:column>
<p:column id="id_description" headerText="Description" style="width: 10%" >
<h:outputText value="#{varInfo.description}"/>
</p:column>
<p:column id="id_chart" headerText="Chart" style="width: 80%" >
<p:chart type="pie" model="#{varInfo.pieModel1}" style="width:40px;height:30px" />
</p:column>
Thanks.
It works fine, chart only appears (in my app) with pieModel1.setLegendPosition("s")

Primefaces slected row id == null

I have a working datatable that can list out restaurant objects.
I want to delete/edite the selected ones but when I select one the following exception shows up:
org.springframework.dao.InvalidDataAccessApiUsageException: The given id must not be null!
Here is the table:
<h:form id="restaurantForm">
<p:dataTable var="restaurant"
value="#{restaurantLazyBean.lazyDataModel}" paginator="true"
rows="10" rowsPerPageTemplate="5,10,50" id="carTable" lazy="true"
selectionMode="single" selection="#{RestaurantEditBean.selected}"
rowKey="#{restaurant.id}"
paginatorTemplate="{RowsPerPageDropdown} {FirstPageLink} {PreviousPageLink} {CurrentPageReport} {NextPageLink} {LastPageLink}">
<p:ajax event="rowSelect"
listener="#{RestaurantEditBean.onRowSelect()}"/>
<p:column headerText="ID">
<h:outputText value="#{restaurant.id}" />
</p:column>
</p:dataTable>
</h:form>
Now all ids appear in the table but on selection the exception shows up.
I tried to do everything according to the primefaces example. But they didn't even have the rowKey attribute.
Heres the bean if thats relevant.
#Named("RestaurantEditBean")
#ViewScoped
#EJB(name = "ejb.RestaurantService", beanInterface = RestaurantService.class)
public class RestaurantEditBean {
#EJB
private RestaurantService restaurantService;
private RestaurantDTO selected;
public void onRowSelect(SelectEvent event) {
selected = ((RestaurantDTO) event.getObject());
}
public RestaurantService getRestaurantService() {
return restaurantService;
}
public void setRestaurantService(RestaurantService restaurantService) {
this.restaurantService = restaurantService;
}
public RestaurantDTO getSelected() {
return selected;
}
public void setSelected(RestaurantDTO selected) {
this.selected = selected;
}
}
Primefaces: 5.3
JSF: 2.2
I found out that I did a terrible mistake.
In my LazyDataModel I had to override a function.
#Override
public RestaurantDTO getRowData(String rowKey) {
Long id = Long.parseLong(rowKey);
return restaurantService.findById(id);
}
The issue was cause by the previous Long.getLong(rowKey) and that one returned null.

binding <p:datable> row with a form

When I click a row in <p:dataTable> and click a command button I want to show a new dialog and set the value of the row to a form(in new dialog). I use session bean to keep data of selected row but the data isn't changed when i select a new row. I think this is because of session bean which keeps data until the session expires. I've tried all scopes but no one worked
Here's my dataTable
<p:dataTable var="customer"
widgetVar="customerTable"
value="#{customerFacade.findAll()}"
selectionMode="single"
rowKey="#{customer.customerid}"
selection="#{editCustomerBean.customer}"
paginator="true" rows="10"
paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
rowsPerPageTemplate="5,10,15">
<p:ajax event="rowSelect" listener="#{editCustomerBean.onRowSelect}" update=":form:growl"/>
Here's my command button and dialog
<h:form>
<p:dialog id="edit" header="Edit Customer" widgetVar="editCustomerDlg" resizable="false" showEffect="fade">
<h:panelGrid id="editDisplay" columns="2" columnClasses="alignTop">
<p:outputLabel for="editCompanyName" value="Company Name" />
<p:inputText id="editCompanyName"label="Compnay Name" value="#{editCustomerBean.customer.companyname}" required="true" />
<h:panelGroup/>
<p:commandButton value="Edit Customer" update="editDisplay" oncomplete="if (!args.validationFailed){editCustomerDlg.hide()}" actionListener="#{customerController.editCustomer}" />
</h:panelGrid>
</p:dialog>
<p:commandButton widgetVar="editBut" icon="ui-icon-tag" value="Edit" oncomplete="editCustomerDlg.show()"/>
</h:form>
here's my session bean
#Named(value = "editCustomerBean")
#SessionScoped
public class EditCustomerBean implements Serializable {
private Customer customer;
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
public void onRowSelect(SelectEvent event) {
customer = (Customer) event.getObject();
System.out.println(customer.getAddress());
FacesMessage msg = new FacesMessage("Customer Selected", ((Customer) event.getObject()).getPhone());
FacesContext.getCurrentInstance().addMessage(null, msg);
}
public EditCustomerBean() {
}
}

"p: datatable" selection

I am using a p:datatable and selection to handle in my bean the selected record. This works. But I want to preset a selected row when initializing the datatable. Is this possible? How?
Now it doesn't preset a record, even though selectedCompany is set. Is it because I do it before the return to jsf has been made?
This is the code I have
Bean
public Map<String, Object> getSelectedCompany() {
return selectedCompany;
}
public void setSelectedCompany(Map<String, Object> selectedCompany) {
this.selectedCompany = selectedCompany;
}
public MWSGenericMapList getHandlingCompanies() {
if (companyItems == null) {
companyItems = retrieveHandlingCompanyItems(partnerIds);
for (int i = 0; i < companyItems.size(); i++) {
if (companyItems.get(i).get("businesspartnerid").equals(getnewCustomerPartnerId())) {
setSelectedCompany(companyItems.get(i));
}
}
}
return companyItems;
}
Jsf
<p:dataTable styleClass="ptable100" id="handlingCompanies" var="company" value="#{switchCompany.handlingCompanies}" width="100%" height="200"
emptyMessage="#{msg.all_lists_no_records_found}" paginator="true" rows="10" rowsPerPageTemplate="5,10,20,50,100"
paginatorTemplate="{FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} #{msg.all_lists_numberOfRowsDisplayed_label} {RowsPerPageDropdown}"
selection="#{switchCompany.selectedCompany}" selectionMode="single" rowKey="#{company.businesspartnerid}">
<p:ajax event="rowSelect" update="#form" listener="#{switchCompany.handleCompanyChange}"/>
nevermind, the selected row did get set, I was just not on the first page (too many rows to display on 1 page) so I missed it. But it works the way I did it.

Resources