Is there any way to keep the current selection of a datatable when updated?
Or better yet, a way to select a specific row and make it a frozen row?
Keep selection on DataTable when updated
- andyba
- Expert Member
- Posts: 2473
- Joined: 31 Mar 2011, 16:27
- Location: Steinfeld, near Bremen/Osnabrück, DE
- Contact:
If your datatable backing beanhas sufficient scope then the selected pojo should remain selected unless you change it yourself.
PF 4.x (Elite versions), PF 5, Pf 5.1, PF 6.0
Glassfish 4.1, Mojarra 2.x, Java 8, Payara 4.1.1.
If you haven't read the forum rules read them now
Glassfish 4.1, Mojarra 2.x, Java 8, Payara 4.1.1.
If you haven't read the forum rules read them now
Could this problem have anything to do with me using push to update the datatable?
I did some tests and if I update the table with a commandButton it works as intended and keeps the selected row, but if I update it using a socket the selection is lost.
This is part of my code:
xhtml:
Bean:
I did some tests and if I update the table with a commandButton it works as intended and keeps the selected row, but if I update it using a socket the selection is lost.
This is part of my code:
xhtml:
Code: Select all
<h:body>
<p:socket channel="/parrilla">
<p:ajax event="message" delay="425" update="form:parrillaTable"/>
</p:socket>
<ui:include src="templates/topMenu.xhtml" />
<h:form id="form">
<p:dataTable id="parrillaTable" value="#{parrilla.parrillas}" var="par"
sortOrder="descending" sortBy="#{par.rentabilidad}" styleClass="tableFont"
scrollable="true" scrollHeight="600"
emptyMessage="No hay datos de parrilla"
selection="#{parrilla.selected}" selectionMode="single"
rowKey="#{par.hashCode()}">
<p:column width="15%" headerText="Instrumento" sortBy="#{par.instrumento}">
<h:outputText value="#{par.instrumento} "/>
@<h:outputText value=" #{par.exchange}"/>
</p:column>
<p:column headerText="Vencimiento" sortBy="#{par.vto}">
<h:outputText value="#{par.vto}">
<f:convertDateTime pattern="YYYY-MM-dd"/>
</h:outputText>
</p:column>
<p:column width="18%" styleClass="centerAlign" headerText="Strikes" sortBy="#{par.put}">
<h:outputText styleClass="putcall" value="PC " rendered="#{par.put == par.call}"/>
<h:outputText styleClass="putcall" value="P " rendered="#{par.put != par.call}"/>
<h:outputText value="#{par.put}">
<f:convertNumber type="number"/>
</h:outputText>
<h:outputText styleClass="putcall" value=" C " rendered="#{par.put != par.call}"/>
<h:outputText value="#{par.call}" rendered="#{par.put != par.call}">
<f:convertNumber type="number"/>
</h:outputText>
</p:column>
<p:column headerText="Dist" sortBy="#{par.distancia}">
<h:outputText value="#{par.distancia}">
<f:convertNumber type="number"/>
</h:outputText>
</p:column>
<p:column headerText="Bid" sortBy="#{par.bid}">
<h:outputText value="#{par.bid}">
<f:convertNumber type="number"/>
</h:outputText>
</p:column>
<p:column headerText="Ask" sortBy="#{par.ask}">
<h:outputText value="#{par.ask}">
<f:convertNumber type="number"/>
</h:outputText>
(<h:outputText value="#{par.volatilidadImpl}">
<f:convertNumber maxFractionDigits="2"/>
</h:outputText>)
</p:column>
<p:column width="5%" headerText="vBE" sortBy="#{par.sigmaBreakEven}">
<h:outputText value="#{par.sigmaBreakEven}">
<f:convertNumber maxFractionDigits="2"/>
</h:outputText>
</p:column>
<p:column width="5%" headerText="Días" sortBy="#{par.diasVto}">
<h:outputText value="#{par.diasVto}"/>
</p:column>
<p:column headerText="Rentabilidad" sortBy="#{par.rentabilidad}">
<h:outputText value="#{par.rentabilidad}">
<f:convertNumber maxFractionDigits="3"/>
</h:outputText>
</p:column>
</p:dataTable>
</h:form>
</h:body>
Code: Select all
@ManagedBean(name = "parrilla")
@ViewScoped
public class ParrillaBean implements Serializable {
private Parrilla selected;
private List<Parrilla> parrillas;
public ParrillaBean() {
ParrillaThread parrillaThread = (ParrillaThread) FacesContext.getCurrentInstance()
.getExternalContext().getApplicationMap().get("parrillaThread");
parrillas = parrillaThread.getParrillas();
}
public Parrilla getSelected() {
return selected;
}
public void setSelected(Parrilla selected) {
this.selected = selected;
}
public List<Parrilla> getParrillas() {
return parrillas;
}
public void setParrillas(List<Parrilla> parrillas) {
this.parrillas = parrillas;
}
}
try what happens if the scope of the ParrillaBean is set to Session. Might indeed be that the update from the push requests are not in the same 'view' as the initial request. Longer scope might solve that and I don't think that a ViewAccessScoped will succeed either, but you may give it a try as well
I changed the bean's scope to session and inserted the following line inside the dataTable (because sometimes the updates were so fast that the select event would take a couple secs to get registered)
And now it works perfectly, Thanks guys!
Code: Select all
<p:ajax event="rowSelect" async="true"/>
-
- Information
-
Who is online
Users browsing this forum: No registered users and 28 guests