Keep selection on DataTable when updated

UI Components for JSF
Post Reply
slash3584
Posts: 8
Joined: 11 Jun 2014, 10:18

23 Oct 2014, 17:18

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?

kukeltje
Expert Member
Posts: 9605
Joined: 17 Jun 2010, 13:34
Location: Netherlands

23 Oct 2014, 20:43

1: yes, use ajax to add/remove the selected items to the list and do not use the setter directly (implement it empty)
2: no idea

User avatar
andyba
Expert Member
Posts: 2473
Joined: 31 Mar 2011, 16:27
Location: Steinfeld, near Bremen/Osnabrück, DE
Contact:

24 Oct 2014, 07:49

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

kukeltje
Expert Member
Posts: 9605
Joined: 17 Jun 2010, 13:34
Location: Netherlands

24 Oct 2014, 09:56

it did not work this way afairc in earlier versions. There it was cleared. Hence my suggestion to use ajax. But it might work like you mention now.

slash3584
Posts: 8
Joined: 11 Jun 2014, 10:18

24 Oct 2014, 14:52

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:

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>
Bean:

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;
    }
   
}

kukeltje
Expert Member
Posts: 9605
Joined: 17 Jun 2010, 13:34
Location: Netherlands

24 Oct 2014, 15:52

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

slash3584
Posts: 8
Joined: 11 Jun 2014, 10:18

27 Oct 2014, 11:41

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)

Code: Select all

<p:ajax event="rowSelect" async="true"/>
And now it works perfectly, Thanks guys!

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 28 guests