title: How to disable sorting on Primefaces lazy datatable?
body: <p>I have a datatable with lazy data model and pagination. I want to disable default sorting when I click on "search" button and enable it again when I click on "display" button. So didn't put "sortBy" in my xhtml file and instead, I set it dynamically in my backing bean.</p>
  
  <p>Everything works properly until I click on the header to flip sort order between descending and ascending. It means the "search" button disables the sorting and "display" button enables it correctly if I don't click on the header. But when I click on the header and then click the "search" button, in <code>load</code> funciton of LazyDataModel, value of the argument <code>sortField</code> is <code>"date"</code> and the datatable will be sorted by date though the "date" column is not colored apparently!!</p>
  
  <p>This is my datatable:</p>
  
  <pre><code>&lt;h:form id="contents-form"&gt;
  ...
      &lt;p:dataTable id="tbl" widgetVar="tbl" var="msg" value="#{homeController.messagesModel}" lazy="true"
                   currentPageReportTemplate="سطر {startRecord}-{endRecord} از {totalRecords}"
                   paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
                   paginator="true" rows="10" rowsPerPageTemplate="5,10,15"
                   emptyMessage="هیچ پیامکی وجود ندارد."
                   selection="#{homeController.selectedMessages}" rowSelectMode="checkbox"
                   rowKey="#{msg.id}" filteredValue="#{homeController.filteredMessage}"
                   editable="true" editMode="cell"
                   style="width:100%" dir="rtl"&gt;
  
          &lt;f:facet name="header"&gt;
              &lt;p:commandButton value="خروجی" action="#{homeController.prepareExport}" icon="fa fa-save"
                               update="export-dlg-box" oncomplete="PF('exportDlgBox').show()"/&gt;
              &lt;p:commandButton value="واژگان" action="#{homeController.showListTermsChart}"
                               icon="fa fa-bar-chart" update="contents-form chart-form msgs"
                               oncomplete="PF('chartDlgBox').show()"/&gt;
          &lt;/f:facet&gt;
  
          &lt;p:ajax event="cellEdit" listener="#{homeController.onCellEdit}" oncomplete="updateDescFilter()"/&gt;
  
          &lt;p:column selectionMode="multiple" style="width:16px;text-align:center"/&gt;
  
          &lt;p:column headerText="فرستنده" style="width:100px" filterBy="#{msg.sender.number}"
                    sortBy="#{msg.sender.number}"&gt;
              &lt;p:commandLink action="#{homeController.setPhoneNumToShow(msg.sender)}" update="phone-form"
                             oncomplete="PF('phoneDlgBox').show()" styleClass="simple-command-link"&gt;
                  &lt;h:outputText value="#{msg.sender.number}"/&gt;
              &lt;/p:commandLink&gt;
          &lt;/p:column&gt;
  
          &lt;p:column headerText="گیرنده" style="width:100px" filterBy="#{msg.receiver.number}"
                    sortBy="#{msg.receiver.number}"&gt;
              &lt;p:commandLink action="#{homeController.setPhoneNumToShow(msg.receiver)}" update="phone-form"
                             oncomplete="PF('phoneDlgBox').show()" styleClass="simple-command-link"&gt;
                  &lt;h:outputText value="#{msg.receiver.number}"/&gt;
              &lt;/p:commandLink&gt;
          &lt;/p:column&gt;
  
          &lt;p:column headerText="متن" sortBy="#{msg.text}"&gt;
              &lt;h:outputText value="#{msg.getTrimmedText()}"/&gt;
          &lt;/p:column&gt;
  
          &lt;p:column headerText="زمان" style="width:100px" filterBy="#{msg.date}" sortBy="#{msg.date}"&gt;
              &lt;f:facet name="filter"&gt;
                  &lt;p:calendar pattern="yyyy-MM-dd"&gt;
                      &lt;p:ajax event="dateSelect" oncomplete="PF('tbl').filter()" update="tbl"/&gt;
                  &lt;/p:calendar&gt;
              &lt;/f:facet&gt;
              &lt;h:outputText value="#{msg.getJalaliDate()}"/&gt;
          &lt;/p:column&gt;
  
          &lt;p:column headerText="منبع" style="width:70px" filterBy="#{msg.source}" sortBy="#{msg.source}"&gt;
              &lt;f:facet name="filter"&gt;
                  &lt;p:selectOneMenu onchange="PF('tbl').filter()" style="width:30px; direction:ltr"&gt;
                      &lt;f:selectItem itemLabel="همه" itemValue="#{null}" noSelectionOption="true"/&gt;
                      &lt;f:selectItems value="#{homeController.sources}" var="source"
                                     itemValue="#{source}" itemLabel="#{source}"/&gt;
                  &lt;/p:selectOneMenu&gt;
              &lt;/f:facet&gt;
              &lt;h:outputText value="#{msg.source}"/&gt;
          &lt;/p:column&gt;
  
          &lt;p:column headerText="توضیح" style="width:70px" filterBy="#{msg.description}"
                    sortBy="#{msg.description}"&gt;
              &lt;f:facet name="filter"&gt;
                  &lt;p:selectOneMenu id="desc-filter-select" onchange="PF('tbl').filter()"
                                   style="width:30px; direction:ltr"&gt;
                      &lt;f:selectItem itemLabel="همه" itemValue="#{null}" noSelectionOption="true"/&gt;
                      &lt;f:selectItems value="#{homeController.descriptions}" var="desc"
                                     itemValue="#{desc}" itemLabel="#{desc}"/&gt;
                  &lt;/p:selectOneMenu&gt;
              &lt;/f:facet&gt;
              &lt;p:cellEditor&gt;
                  &lt;f:facet name="output"&gt;&lt;h:outputText value="#{msg.description}"/&gt;&lt;/f:facet&gt;
                  &lt;f:facet name="input"&gt; &lt;p:inputText id="desc-input" value="#{msg.description}"
                                                      style="width:85%"/&gt;&lt;/f:facet&gt;
              &lt;/p:cellEditor&gt;
          &lt;/p:column&gt;
  
          &lt;p:column headerText="جزئیات" style="width:40px"&gt;
              &lt;p:commandLink action="#{homeController.setMessageToShow(msg)}" ajax="true"
                             update="img-dlg-box" oncomplete="PF('imgDlgBox').show()"&gt;
                  &lt;i class="fa fa-blue fa-desktop"/&gt;
              &lt;/p:commandLink&gt;
          &lt;/p:column&gt;
  
          &lt;p:column headerText="مشاهده گفتگو" style="width:40px"&gt;
              &lt;p:commandLink action="#{homeController.showConversation(msg.sender, msg.receiver)}"
                             ajax="true" update="conv-dlg-box" oncomplete="PF('convDlgBox').show()"&gt;
                  &lt;i class="fa fa-blue fa-wechat"/&gt;
              &lt;/p:commandLink&gt;
          &lt;/p:column&gt;
      &lt;/p:dataTable&gt;
  &lt;/h:form&gt;
  </code></pre>
  
  <p>My "search" button:</p>
  
  <pre><code>&lt;p:commandButton value="جست‌وجو" action="#{homeController.search}" update=":contents-form:tbl msgs"
                                       icon="fa fa-search"/&gt;
  </code></pre>
  
  <p>My "display" button:</p>
  
  <pre><code>&lt;p:commandButton value="نمایش" icon="fa fa-desktop" action="#{homeController.loadArchive}"
                   update="contents-form msgs"/&gt;
  </code></pre>
  
  <p><code>search</code> method in the backing bean:</p>
  
  <pre><code>public void search() {
      if (this.selectedArchive == null) {
          FacesContext.getCurrentInstance().addMessage(null,
                  new FacesMessage(FacesMessage.SEVERITY_ERROR, "خطا!", "لطفا یک مجموعه را انتخاب کنید"));
          return;
      }
  
      if ((this.textQuery != null &amp;&amp; !this.textQuery.equals(""))
              || (this.selectedNodes != null &amp;&amp; this.selectedNodes.length &gt; 0)) {
          logger.info(String.format("searching '%s' in archive %d ...", this.textQuery, this.selectedArchive.getId()));
          List&lt;String&gt; selectedWords = this.getSelectedWords(this.selectedNodes);
          searchText(this.textQuery, selectedWords);
      } else {
          FacesContext.getCurrentInstance().addMessage(null,
                  new FacesMessage(FacesMessage.SEVERITY_ERROR, "خطا!", "برای جست‌وجو هیچ متنی وارد نشده و هیچ واژه‌ای انتخاب نشده است."));
          return;
      }
  
      extractChoices();
  
      // Disable default sorting:
      DataTable table = (DataTable) FacesContext.getCurrentInstance().getViewRoot().findComponent(":contents-form:tbl");
      table.setValueExpression("sortBy", null);
      table.setValueExpression("sortOrder", null);
  }
  </code></pre>
  
  <p>And <code>loadArchive</code> method in the backing bean:</p>
  
  <pre><code>public void loadArchive() {
      if (this.selectedArchive == null) {
          FacesContext.getCurrentInstance().addMessage(null,
                  new FacesMessage(FacesMessage.SEVERITY_ERROR, "خطا!", "لطفا یک مجموعه را انتخاب کنید"));
          return;
      }
      this.messagesModel = new LazyMessageModel(this.selectedArchive, true, this.messageService);
  
      extractChoices();
  
      // Enable default sorting:
      FacesContext context = FacesContext.getCurrentInstance();
      ExpressionFactory ef = context.getApplication().getExpressionFactory();
      DataTable table = (DataTable) context.getViewRoot().findComponent(":contents-form:tbl");
      ValueExpression sortVe = ef.createValueExpression(context.getELContext(), "#{msg.date}", Message.class);
      table.setValueExpression("sortBy", sortVe);
      table.setSortOrder("descending");
  
      logger.info(String.format("archive %d loaded", (this.selectedArchive.getId())));
  }
  </code></pre>
  
  <p>Spec:</p>
  
  <ul>
  <li>Primefaces 6.2</li>
  <li>JSF 2.2.14</li>
  <li>Java 1.8</li>
  </ul>