Primefaces lazy datatable : how to retrieve the visible list - jsf

I have a lazydatatable displaying thousands of cars. I ll have a buy button on the top of the page.
Basically, i want the user to buy all the cars visibles on the list when I press on the Buy Button. I m struggling to get a list limited to these cars.
How can I do?
xhtml
<p:dataTable var="car" value="#{carView.listCars}" paginator="true" rows="10" paginatorTemplate="{RowsPerPageDropdown} {FirstPageLink} {PreviousPageLink} {CurrentPageReport} {NextPageLink}
{LastPageLink}" rowsPerPageTemplate="5,10,15" selectionMode="single" selection="#{carView.selectedCar}" id="carTable" lazy="true">
<p:column headerText="Year">
<h:outputText value="#{car.year}" />
</p:column>
<p:column headerText="Brand">
<h:outputText value="#{car.brand}" />
</p:column>
CarService
public void buy{
...
}
CarView
public void init(){
LazyCarDataModel cars= restService.getCars();
}

Using a p:datatable in lazy mode, you provide the data for the 'visible' list via the load method in your LazyDataModel. So if you keep that list in one way or another on the server (e.g. storing it in a #Viewscoped bean`), it is available when you click 'buy'.
Another way is to support multiple selection and have a 'select all' toggle box at the top of the selection. And then use the selected files server side

Related

change an image with a p:commandButton click

<h:form>
<p:dataTable var="btsEvent" value="#{eventController.eventsList}" rows="5"
paginator="true"
paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
rowsPerPageTemplate="5,10,15">
<p:column id="Acknowledged" headerText="Acknowledged" >
<!-- conditional rendering -->
<p:graphicImage id="#{btsEvent.id}" value="resources/elements/confirm.png" rendered="#{btsEvent.ack}" />
<p:commandButton value="OK" id="AckButton" rendered="#{!btsEvent.ack}"
actionListener="#{btsEvent.getAcknowledgedOnClick(btsEvent.id)}"
styleClass="ui-priority-primary"/>
</p:column>
</p:dataTable>
</h:form>
I'm trying to assign a dynamic id to graphicImage in order to be able to catch it later with javascript and change its source attribute via btsEvent.getAcknowledgedOnClick(btsEvent.id). It seems that making dynamic id is not available here since I get "Empty id attribute is not allowed".
My goal is to reneder an OK image or a confirmation button that when clicked some code is executed and then another js code changes the image.
How do I assign dynamic id to the image or access corresponding graphicImage from getAcknowledgedOnClick function?
Other option is to have the button itself renedered as an image and change that image in code. How can I do that?

Unable to Select Table Rows in PrimeFaces Using RadioButtons

I am trying to develop a web application using a PrimeFaces data table. I am having difficulty getting the object that corresponds to the selected row. Whenever I do that, a null value gets selected. Here is the relevant code:
<p:dataTable id="presTable" var="pres" value="#{presBean.presentations}"
rows="10" scrollable="true" scrollWidth="85%"
selection="#{presBean.selectedPres}" rowKey="#{pres.id}" paginator="true"
paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
rowsPerPageTemplate="10,25,100" widgetVar="presTable" filteredValue="#{presBean.filteredPres}" selectionMode="single">
</p:column>
<f:facet name="footer">
<p:commandButton id="updateButton" process="presTable" value="Update" action="#{presBean.printSelectedPres()}"/>
</f:facet>
</p:dataTable>
Java backing bean:
public Presentation getSelectedPres() {
System.out.println("Returning selected presentation: " + selectedPres);
return selectedPres;
}
public void setSelectedPres(Presentation selectedPres) {
System.out.println("Setting selected presentation to " + selectedPres);
this.selectedPres = selectedPres;
}
public void printSelectedPres() {
System.out.println("Printing list of checked presentations:");
System.out.println(selectedPres);
}
Add a column with seletionMode=single
<p:column selectionMode="single" style="width:16px;text-align:center"/>
you can find sample for radio button here : https://www.primefaces.org/showcase/ui/data/datatable/selection.xhtml

How to fix my Primefaces datatable pagination layout?

I use primefaces to display a list in a datatable with pagination.
The layout for the Page DropDown is not correct.
Screenshot:
How can I set this layout?
It now stretches along the whole width of the table. This is not good.
<p:dataTable id="dataTable" value="#{bean.lazyModel}" var="item"
paginator="true"
rows="10"
paginatorTemplate="{CurrentPageReport} {FirstPageLink}
{PreviousPageLink} {PageLinks} {NextPageLink}
{LastPageLink} {RowsPerPageDropdown}"
rowsPerPageTemplate="5,10,15,50,100"
lazy="true"
selectionMode="single"
selection="#{orderEntrySearchBacking.selected}"
rowKey="#{item.id}">
<f:facet name="header">
Patients
<p:commandButton id="toggler" type="button" value="Columns"
style="float:right" icon="ui-icon-calculator" />
<p:columnToggler datasource="dataTable" trigger="toggler" />
</f:facet>
<p:column>
<h:outputText value="#{item.name}"/>
</p:column>
<p:column>
<h:outputText value="#{item.date}"/>
</p:column>
....
This problem is related to the {RowsPerPageDropdown}, but I can't get the style correct. Help is appreciated.
That's all folks!
It looks like your <select> element has style width: 100%.
Inspect dropdown and find where style rule come from.
To fix it you should override this rule. One of the ways is to add following style rule to your .css file.
select.ui-paginator-rpp-options{
width:auto;
}

No data load my outputText of Primefaces?

I have a question I have a DataTable, where I have a button in a column edit, capture the Id of the record, and make a list that record to load the data in the object system, peroHola, I have a question I have a DataTable, where I have a button in a column edit, capture the Id of the record, and make a list that record to load the data in the object system, but my text box value is charging.
<p:dataTable id="dataTable" var="sis" value="#{listSistMB.system}"
paginator="true" rows="10" rowIndexVar="rowIndex" selection="#{listSistMB.system}"
paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
rowsPerPageTemplate="5,10,15">
<p:column>
<f:facet name="header">
<h:outputText value="Status" />
</f:facet>
<p:selectBooleanButton value="#{sis.stStatus}" onLabel="A" offLabel="I" />
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="Edit" />
</f:facet>
<p:commandButton id="selectEdit" action="#{listSistMB.editSystem}"
icon="ui-icon-search" title="Editar System">
<f:param name="codCode" value="#{sis.cdCode}"/>
</p:commandButton>
</p:column>
</p:dataTable>
In the controller I headed this way the system object.
#Component("listSistMB")
#Scope("view")
public class MantenanceSystemController implements Serializable{
private System system = new System();
public String editSystem(){
try {
String cdCode = (String)FacesContext.getCurrentInstance().getExternalContext().
getRequestParameterMap().get("codCode");
system = confSistemaService.buscarPorId(Long.valueOf(cdCode));
} catch (Exception e) {
e.printStackTrace();
}
return "register-system";
}
It's not charging me the value of the text box.
<p:inputText value="#{listSistMB.system.nbNomCorto}" />
The MantenanceSystemController bean is in #Scope("view")
If the p:dataTable and p:inputText are not on the same view, the information will be lost when changing views.
Consider a different scope (e.g., Conversation) for the bean.

How to set a value for a target element using Datatable LazyLoading

I want to set something like this in the LazyLoading Datatable , I am not able to use the below code.
<p:dataTable var="studyPlanList" value="#{editBean.lazyModel}"
paginator="true" rows="10"
paginatorTemplate="{RowsPerPageDropdown} {FirstPageLink} {PreviousPageLink} {CurrentPageReport} {NextPageLink} {LastPageLink}"
rowsPerPageTemplate="5,10,15" selectionMode="single"
selection="#{editBean.selectedStudyPlan}"
id="studyPlanTable">
<p:ajax event="rowSelect" listener="#{editBean.onRowSelect}" update=":studyPlanEditForm:display" >
</p:ajax>
<p:column headerText="StudyPlan" sortBy="#{studyPlanList.name}" filterBy="#{studyPlanList.name}">
<h:outputText value="#{studyPlanList.name}"/>
</p:column>
</p:dataTable>
Instead of p:column , if it is h:link it is working, I want to achieve similar results;Please Help.
This is from the facelet tag documentation on f:setPropertyActionListener:
Register an ActionListener instance on the UIComponent associated with
the closest parent UIComponent custom action
So you need a triggering action to make it work. This could be a h:commandButton or h:commandLink. Here is a quick example:
<h:commandButton action="#{editBean.submitAction}" value="Select">
<f:setPropertyActionListener target="#{editBean.name}"
value="#{studyPlanList.name}" />
</h:commandButton>
Since you are using Primefaces, you could take a look at the row selection feature of p:dataTable. Here is an example from the showcase.
UPDATE:
The type of the selection should be the same as the type of your row element. I see from your code that selection="#{editBean.selectedStudyPlan.name}" but it should be
selection="#{editBean.selectedStudyPlan}"

Resources