Page 1 of 2

p:dataTable selection model gets out of sync after reload in

Posted: 09 Jan 2012, 16:35
by breilly
I believe I found a bug with p:dataTable while investigating a question on StackOverflow (http://stackoverflow.com/questions/8764 ... ean-even-a). The issue is that, when reloading a page, Firefox tries to save changed form values and restore them after reload so that the user does not lose what they entered. Since p:dataTable uses a hidden form element to keep track of the selection, Firefox restores this value. This leads to the rendered selection and the actual selection being different.

I think that the p:dataTable component should either explicitly clear out the hidden input or read the value and render the selection in the table to reflect the hidden input. I personally tested this on FF4 (Mac OS X) and the original reporter said that they'd seen it on FF8, though someone else followed up saying that they didn't see it on FF9.

Thanks.

Re: p:dataTable selection model gets out of sync after reloa

Posted: 09 Jan 2012, 16:53
by breilly
Update: a further comment on StackOverflow suggests adding autocomplete="off". Sounds like adding this to the hidden input would also fix the problem. Thanks.

Re: p:dataTable selection model gets out of sync after reloa

Posted: 09 Jan 2012, 18:31
by Oleg
Yes, this is a Firefox "feature". There are many links about this.

http://alexscordellis.blogspot.com/2010 ... -with.html
http://stackoverflow.com/questions/6487 ... -auto-fill

Just interesting why PrimeFaces doesn't have autocomplete="off" for hidden fields. I think other components are affected too. I could see this issue in 2.2.1. Please create an issue ticket.

Re: p:dataTable selection model gets out of sync after reloa

Posted: 09 Jan 2012, 20:57
by KingdomHeart

Re: p:dataTable selection model gets out of sync after reloa

Posted: 10 Jan 2012, 16:27
by KingdomHeart
Oddly, I can still replicate this under FF9. Can anyone try this code and see if they can replicate this? Thanks in advance. PF 3 Final + tomcat 7

Code: Select all

<p:growl id="messages" showDetail="true" />  
<p:messages id="msgs"/>
<h:form id="form">  
    <p:dataTable value="#{viewBean.foodList}" var="item" 
                  selection="#{viewBean.selectedFoods}"
                  selectionMode="multiple"
                  rowKey="#{item}">
        <p:column>
             #{item}
        </p:column>
        <f:facet name="footer">
            <p:commandButton value="Submit" update=":form:display :dataList" 
                                 action="#{viewBean.checkSelection}"/>
        </f:facet>
    </p:dataTable>
    <p:dataList id="display" value="#{viewBean.selectedFoods}" var="item"
                    itemType="disc">
        #{item}
    </p:dataList>
</h:form>
<p:dialog id="dialog1" widgetVar="dialog1" dynamic="true" width="200">
    <p:dataList id="dataList" value="#{viewBean.selectedFoods}" var="item"
                    itemType="disc">
        #{item}
    </p:dataList>
</p:dialog>

Code: Select all

@ManagedBean
@ViewScoped
public class ViewBean implements Serializable {
    private List<String> foodList;
    private String[] selectedFoods;

    @PostConstruct
    public void init() {
        foodList = new ArrayList<String>();
        foodList.add("Pizza");
        foodList.add("Pasta");
        foodList.add("Hamburger");
    }

    public void checkSelection(){
        RequestContext requestContext = RequestContext.getCurrentInstance();
        if(selectedFoods.length > 0){
            requestContext.execute("dialog1.show()");
        }else{
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Error", "Please select"));
            requestContext.addPartialUpdateTarget("messages");
        }
    }
    //setter, getter
 }


Re: p:dataTable selection model gets out of sync after reloa

Posted: 10 Jan 2012, 16:31
by cagatay.civici
Try setting autocomplete off to the hidden field and let me know if it fixes the issue, if so we can add it to PrimeFaces. To do this, you need to know how to build PF from source for a local test.

Re: p:dataTable selection model gets out of sync after reloa

Posted: 10 Jan 2012, 23:08
by cagatay.civici
I have added autocomplete off to trunk, you can try with the build from repo to see if it fixes your issue.

Re: p:dataTable selection model gets out of sync after reloa

Posted: 10 Jan 2012, 23:21
by KingdomHeart
Thank you Optimus, I will do it as soon as I get home. Thanks a lot.

Re: p:dataTable selection model gets out of sync after reloa

Posted: 12 Jan 2012, 16:40
by KingdomHeart
Hi Optimus, sorry that it took me two days to test this out, but I have compile the latest SNAPSHOT, and it seems like remove autocomplete fix this problem. Thank you. I also notice that p:selectOneMenu suffer from the same problem. You can test at http://www.primefaces.org/showcase-labs ... neMenu.jsf. Select one item in the drop down, and refresh. On chrome and IE, the drop down value reset back to "Select One", but on Firefox, it retain this value. Anyway, I will make this a separate post.

Re: p:dataTable selection model gets out of sync after reloa

Posted: 12 Jan 2012, 16:44
by cagatay.civici
There is no hidden input in select one menu.