0

I know this is not a bug. But i need a suggestion, what can i do not to lose checked knowledge of checked lines?

On JSF page:

<h:form prependId="false" id="searchUserFormTable" > 
                    <p:dataTable  id="selectUserTable" var="user" 
                    value="#{userListController.lazyDataModel}" paginator="true"
                    paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
                    rows="2" paginatorPosition="bottom" rowKey="#{user}"
                    lazy="true"
                    rowsPerPageTemplate="2,10,20,30,40,50"
                    rendered="#{userListController.lazyDataModel != null}" selection="#{systemMessageEditController.receiverList}"
                    scrollable="true" scrollHeight="250" resizableColumns="false" >


                    <p:column selectionMode="multiple" width="18" />

                    <f:facet name="header">
                        <h:outputText value=" Users (#{userListController.lazyDataModel.rowCount})"  />
                    </f:facet>

                    <p:column filterBy="#{user.name}" headerText="Name" filterMatchMode="contains" width="80">
                        #{user.name}
                    </p:column>
                    <p:column filterBy="#{user.surname}" headerText="Surname" filterMatchMode="contains" width="80">
                        #{user.surname}
                    </p:column>   
                    <p:column headerText="Gender" width="80">
                        #{user.gender}
                    </p:column>

                    <p:column headerText="City" width="80">                             
                        #{user.address.city.name}
                    </p:column>

                    <p:column headerText="Status" width="80">                               
                        #{user.userStatus}
                    </p:column>

                    <p:column headerText="User Detail" width="80">
                        <h:link value="User Detail" outcome="/view/admin/usermanagement/userInfo.jsf">
                            <f:param name="userId" value="#{user.id}" />
                        </h:link>
                    </p:column>



                </p:dataTable>
                        <p:commandButton value="Tamam" oncomplete="userListDlg.hide();" update=":newMessageForm"></p:commandButton>
            </h:form>

All the controller:

    @ManagedBean(name="systemMessageEditController")
@ViewScoped
public class SystemMessageEditController {

    @ManagedProperty(value = "#{systemMessageService}")
    SystemMessageService systemMessageService;

    @ManagedProperty(value = "#{securityBean}")
    SecurityBean securityBean;

    @ManagedProperty(value = "#{systemMessageReceiverService}")
    SystemMessageReceiverService systemMessageReceiverService;

    @ManagedProperty(value = "#{systemMessageAnswerService}")
    SystemMessageAnswerService systemMessageAnswerService;

    private SystemMessage systemMessage = new SystemMessage();

    private SystemMessageAnswer systemMessageAnswer = new SystemMessageAnswer();

    private SystemMessageReceiver systemMessageReceiver = new SystemMessageReceiver();

    private User[] receiverList;
    private boolean dummy;


    public void prepareToEdit(){
        systemMessage = systemMessageService.findById(systemMessage.getId());
    }

    public void prepareToSendMessage(){
        systemMessage = new SystemMessage();
        receiverList = null ;

    }

    public void sendMessage(){
        if(Util.isNotNull(systemMessage.getTitle())){
            boolean success = false;
            systemMessageAnswer.setUser(securityBean.getUser());
            systemMessage.getSystemMessageAnswerList().add(systemMessageAnswer);
            systemMessage.setUserSender(securityBean.getUser());
            systemMessageAnswer.setSystemMessage(systemMessage);
            if(!systemMessage.isSendAll()){
                addMessageReceiver();   
            }


            systemMessage = systemMessageService.save(systemMessage);

            systemMessageAnswer = new SystemMessageAnswer();

            success = true;
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, FacesUtils.getMessageByKey("systemmessage.send.success"), FacesUtils.getMessageByKey("systemmessage.send.success"));
            FacesContext.getCurrentInstance().addMessage("", message);
            RequestContext context = RequestContext.getCurrentInstance();
            context.addCallbackParam("success", success);
        }
        else{
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, FacesUtils.getMessageByKey("systemmessage.null.title"), FacesUtils.getMessageByKey("systemmessage.null.title"));
            FacesContext.getCurrentInstance().addMessage("", message);
        }
    }

    public void setRead(SystemMessage message){
        message.setReaded(true);
        systemMessageService.save(message);
    }


    public void addMessageReceiver(){
        for(User user : receiverList){
            SystemMessageReceiver messageReceiver = new SystemMessageReceiver();
            messageReceiver.setReceiver(user);
            messageReceiver.setSystemMessage(systemMessage);
            messageReceiver.setReaded(false);
            systemMessage.getSystemMessageReceiverList().add(messageReceiver);
        }

    }

    public void deleteMessage(){
        boolean success = false;
        systemMessageService.delete(systemMessageService.findById(systemMessage.getId()));

        success = true;
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, FacesUtils.getMessageByKey("systemmessage.delete.success"), FacesUtils.getMessageByKey("systemmessage.delete.success"));
        FacesContext.getCurrentInstance().addMessage("", message);
        RequestContext context = RequestContext.getCurrentInstance();
        context.addCallbackParam("success", success);
    }
    public void sendMessageAnswer(){
        systemMessageAnswer.setUser(securityBean.getUser());
        systemMessageAnswer.setSystemMessage(systemMessage);
        systemMessageAnswerService.save(systemMessageAnswer);

        if(!systemMessage.isSendAll()){
            systemMessage.setSystemMessageReceiverList(systemMessageService.getReceiverListOfMessage(systemMessage));
            for(SystemMessageReceiver systemMessageReceiver : systemMessage.getSystemMessageReceiverList()){
                if(!systemMessageReceiver.getReceiver().getId().equals(securityBean.getUser().getId())){
                    systemMessageReceiver.setReaded(false);
                    systemMessageReceiver.setShowed(false);
                    systemMessageReceiverService.save(systemMessageReceiver);
                }
            }
        }

        systemMessage.getSystemMessageAnswerList().add(systemMessageAnswer);

        systemMessageAnswer = new SystemMessageAnswer();
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, FacesUtils.getMessageByKey("systemmessage.send.success"), FacesUtils.getMessageByKey("systemmessage.send.success"));
        FacesContext.getCurrentInstance().addMessage("", message);
        RequestContext context = RequestContext.getCurrentInstance();
        context.addCallbackParam("success", true);
    }



    public SystemMessageService getSystemMessageService() {
        return systemMessageService;
    }

    public void setSystemMessageService(SystemMessageService systemMessageService) {
        this.systemMessageService = systemMessageService;
    }

    public SystemMessage getSystemMessage() {
        return systemMessage;
    }

    public void setSystemMessage(SystemMessage systemMessage) {
        ArrayList<String> fetchedAttributes = new ArrayList<String>();
        fetchedAttributes.add("systemMessageAnswerList");
        this.systemMessage = systemMessageService.loadWithFetchedAttributes(systemMessage.getId(),fetchedAttributes);
    }

    public SystemMessageAnswer getSystemMessageAnswer() {
        return systemMessageAnswer;
    }

    public void setSystemMessageAnswer(SystemMessageAnswer systemMessageAnswer) {
        this.systemMessageAnswer = systemMessageAnswer;
    }
    public SecurityBean getSecurityBean() {
        return securityBean;
    }
    public void setSecurityBean(SecurityBean securityBean) {
        this.securityBean = securityBean;
    }
    public SystemMessageReceiverService getSystemMessageReceiverService() {
        return systemMessageReceiverService;
    }
    public void setSystemMessageReceiverService(
            SystemMessageReceiverService systemMessageReceiverService) {
        this.systemMessageReceiverService = systemMessageReceiverService;
    }
    public SystemMessageAnswerService getSystemMessageAnswerService() {
        return systemMessageAnswerService;
    }
    public void setSystemMessageAnswerService(
            SystemMessageAnswerService systemMessageAnswerService) {
        this.systemMessageAnswerService = systemMessageAnswerService;
    }
    public SystemMessageReceiver getSystemMessageReceiver() {
        return systemMessageReceiver;
    }
    public void setSystemMessageReceiver(SystemMessageReceiver systemMessageReceiver) {
        this.systemMessageReceiver = systemMessageReceiver;
    }
    public User[] getReceiverList() {
        return receiverList;
    }
    public void setReceiverList(User[] receiverList) {
        this.receiverList = receiverList;
    }

I am losing checked info while jumping one pagination number to another, when i checked some rows, then jump another pagination number in datatable, and go back to old pagination number i loose the checked info, i see nothing has changed.

merveotesi
  • 2,145
  • 15
  • 53
  • 84

3 Answers3

1

This is how @ViewScoped works. See the following answer from BalusC:

A view scoped bean lives as long as you interact with the same view (i.e. you return void or null in bean action method). When you navigate away to another view, e.g. by clicking a link or by returning a different action outcome, then the view scoped bean will be trashed by end of render response and not be available in the next request.

which explains that in cases like yours (I just read in one of your comment that you jump to another page before you lose the selection, you should note that in your question too!) the bean is destroyed, so when you navigate back to your dataTable view it gets reconstructed again. This is the reason why receiverList is empty => no selection.

If you want to save your data while navigating between different views, use a bean with scope wider than @ViewScoped, or simply save the selection to a table in your DB.

EDIT

The problem could be in your rowKey attribute. In the current code it points to the user object, in this case make sure that this objects has proper hashCode, equals and toString implementations. If the user class actually has a key or something, it would be easier to change rowKey to rowKey="#{user.id}".

Community
  • 1
  • 1
Akos K
  • 7,071
  • 3
  • 33
  • 46
  • i am sorry, but as saying "jumping another page", i mean clicking "another datatable pagination number below datatable" – merveotesi Jan 09 '13 at 09:43
  • @tuxi well anyway, you should mention in the question when you loose selection. I also edited my answer. – Akos K Jan 09 '13 at 09:48
0

Check the Showcase. I think it works just like you want.

partlov
  • 13,789
  • 6
  • 63
  • 82
0

You are probably sending the data via ajax request you should make sure that it process dataTable itself

Panda
  • 21
  • 3
  • I am not doing any custom sending, i am only using `datatable column selectionmode` – merveotesi Jan 09 '13 at 09:29
  • Try getting rid of filtering on columns. Primefaces caches filtered data inside the page, so that may be the reason I think of. – Panda Jan 09 '13 at 09:51