title: PrimeFaces DataTable - need to enable/disable components based on row selection
body: <p>I'm using a PrimeFaces DataTable to display records (randomly generated in a sandbox application).  I am using the check box selection version.  The basic DataTable works perfectly, including the Delete and Cancel buttons (which functionality only really is available from the confirmation dialog).  I am trying to add functionality to the DataTable so that when a check box is selected, other controls on the page are enabled or disabled based on the selection.  </p>
  
  <p>In other words, if no rows are selected (no check boxes are checked) certain buttons and/or menu items are disabled or not rendered.  Selecting one or more rows by clicking the check box should enable or render the controls.  I have tried using the built in JavaScript event handlers, but I cannot make this work.</p>
  
  <p>Right now my page displays a DataTable 5 columns: a check box selection column, First Name, Last Name, Age.  I made something like this work in another sandbox of mine using simple boolean check boxes and updating a boolean with the onclick event.  Unfortunately, there doesn't seem to be anything similar in this DataTable - or if there is I don't know how to implement it.</p>
  
  <p>My index page:</p>
  
  <pre><code>&lt;?xml version='1.0' encoding='UTF-8' ?&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"
        xmlns:f="http://java.sun.com/jsf/core"
        xmlns:h="http://java.sun.com/jsf/html"
        xmlns:p="http://primefaces.prime.com.tr/ui"
        xmlns:c="http://java.sun.com/jsp/jstl/core"
        xmlns:ui="http://java.sun.com/jsf/facelets"&gt;
  
      &lt;body&gt;
  
          &lt;ui:composition template="./newTemplate.xhtml"&gt;
  
  
              &lt;ui:define name="content"&gt;
              &lt;h:form&gt;
  
                  &lt;p:dataTable rowSelectListener="#{tableBean.onRowSelect}" var="data" value="#{tableBean.data}" paginator="true" rows="10"
                       selection="#{tableBean.selectedNames}"&gt;
  
                  &lt;f:facet name="header"&gt;
                      Customer List
                  &lt;/f:facet&gt;
  
  
                  &lt;p:column selectionMode="multiple" /&gt;
  
                  &lt;p:column headerText="Cust ID"&gt;
                      &lt;h:outputText value="#{data.id}" /&gt;
                  &lt;/p:column&gt;
  
                  &lt;p:column headerText="First Name"&gt;
                      &lt;h:outputText value="#{data.firstName}" /&gt;
                  &lt;/p:column&gt;
  
                  &lt;p:column headerText="Last Name"&gt;
                      &lt;h:outputText value="#{data.lastName}" /&gt;
                  &lt;/p:column&gt;
  
                  &lt;p:column headerText="Age"&gt;
                      &lt;h:outputText value="#{data.age}" /&gt;
                  &lt;/p:column&gt;
  
                  &lt;f:facet name="footer"&gt;                        
                      &lt;p:commandButton update="deleteList" value="Delete" oncomplete="deleteDlg.show()" /&gt;
                  &lt;/f:facet&gt;
  
              &lt;/p:dataTable&gt;
  
              &lt;p:dialog header="Delete Selected Records" modal="true" widgetVar="deleteDlg"
                        &gt;
  
                  &lt;h:outputText value="You are about to permanently delete records." /&gt;&lt;br /&gt;&lt;br /&gt;
                  &lt;h:outputText value="Are you sure you want to continue?" /&gt;&lt;br /&gt;&lt;br/&gt;
  
                  &lt;h:commandButton value="CANCEL" action="#{tableBean.cancelDelete()}" /&gt; &lt;h:commandButton value="Delete" action="#{tableBean.deleteNames()}" /&gt;
              &lt;/p:dialog&gt;
  
          &lt;/h:form&gt;
              &lt;/ui:define&gt;
  
          &lt;/ui:composition&gt;
  
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Code from my backing bean which may be relevant:</p>
  
  <pre><code>    public void deleteNames()
      {
          for(Data person : selectedNames)
          {
              data.remove(person);
          }   
      }
  
      public void cancelDelete()
      {
          for(Data name : selectedNames)
              selectedNames = null;
      }
  
      public void onRowSelect(SelectEvent event)
      {
          if(selectedNames == null || selectedNames.length &lt; 1)
              setDisable(true);
          else
              setDisable(false);
      }
  
  public boolean isDisable() {
          if(selectedNames == null || selectedNames.length &lt; 1)
              disable = true;
          else
              disable = false;
          return disable;
      }
  
      public void setDisable(boolean disable) {
          this.disable = disable;
      }
  </code></pre>