title: Primefaces datatable roweditor: only permit one row editing
body: <p>I'm working with JSF 2.1.6 and Primefaces 3.4.1 and I have a question.</p>
  
  <p>I have an editable datatable with row editor. You can click the pencil button of each row, and  the row will be editable.
  But by default it's possible to click many pencil button, and for this reason many rows will be editable.</p>
  
  <p>But I want only one row in edit mode.</p>
  
  <p>This is a sample of my code:</p>
  
  <pre><code>&lt;p:dataTable value="rows" var="row" editable="true" 
   id="myTable" widgetVar="myTableVar" styleClass="myTableStyle"&gt;
  
      &lt;p:ajax event="rowEdit" listener="#{myBean.onUpdateRow}" /&gt;
      &lt;p:ajax event="rowEditCancel" /&gt;
  
      &lt;p:columnGroup type="header"&gt;
          &lt;p:column headerText="Name" /&gt;
          &lt;p:column headerText="Age" /&gt;
          ...
          &lt;p:column headerText="Edit" /&gt;
      &lt;/p:columnGroup&gt;
  
      &lt;p:column&gt;
          &lt;p:cellEditor&gt;
          &lt;f:facet name="output"&gt;
                  &lt;h:outputText value="#{row.name}" /&gt;
              &lt;/f:facet&gt;
              &lt;f:facet name="output"&gt;
                   &lt;h:inputText value="#{row.name}" /&gt; 
              &lt;/f:facet&gt;
          &lt;/p:cellEditor&gt;
      &lt;/p:column&gt;
  
      &lt;p:column&gt;
          &lt;p:cellEditor&gt;
          &lt;f:facet name="output"&gt;
                  &lt;h:outputText value="#{row.age}" /&gt;
              &lt;/f:facet&gt;
              &lt;f:facet name="output"&gt;
                   &lt;h:inputText value="#{row.age}" /&gt; 
              &lt;/f:facet&gt;
          &lt;/p:cellEditor&gt;
      &lt;/p:column&gt;
  
      ...
  
      &lt;p:column&gt;
          &lt;p:commandLink update="myTable"&gt;
              &lt;p:rowEditor /&gt;
          &lt;/p:commandLink&gt;
      &lt;/p:column&gt;
  
  &lt;/p:dataTable&gt;
  
  &lt;p:commandButton icon="ui-icon-plus" action="#{myBean.addNewRow}" update="myTable"
   oncomplete="$('.myTableStyle tbody.ui-datatable-data tr:last-child td span.ui-row-editor span.ui-icon-pencil').click()"
   title="Add new row" /&gt;
  </code></pre>
  
  <p>I've encapsulated the row editor component in a command link component, because now I can add Javascript code when clicking the row editor.</p>
  
  <p>I've tried adding this Javascript code to the commandLink: </p>
  
  <pre><code>onclick="$('.myTableStyle tbody.ui-datatable-data tr td span.ui-row-editor span.ui-icon-cancel').click()"
  </code></pre>
  
  <p>But this create so much recursion and not works. </p>
  
  <p>The row editor has three span links: one for open the edit mode (ui-icon-pencil), other that saves the edition (ui-icon-check), and other that cancel the edition (ui-icon-close). Ther is an ajax event for saving (rowEdit), and other event for cancelling (rowEditCancel).</p>
  
  <p>Files where edit mode is not activated, the row editor spans like this:</p>
  
  <pre><code>&lt;span class="ui-icon ui-icon-pencil"&gt;&lt;/span&gt;
  &lt;span class="ui-icon ui-icon-check" style="display:none"&gt;&lt;/span&gt;
  &lt;span class="ui-icon ui-icon-close" style="display:none"&gt;&lt;/span&gt;
  </code></pre>
  
  <p>And files where edit mode is activated the row editor spans like this:</p>
  
  <pre><code> &lt;span class="ui-icon ui-icon-pencil" style="display:none"&gt;&lt;/span&gt;
   &lt;span class="ui-icon ui-icon-check"&gt;&lt;/span&gt;
   &lt;span class="ui-icon ui-icon-close"&gt;&lt;/span&gt;
  </code></pre>
  
  <p>So, how can I click only the rows where edit mode is activated? Or there is a function or property to permit only one row in edit mode?
  Can I click only with jQuery the spans with icon ui-icon-close when that span has display inline, and not the others with display none?</p>
  
  <p><strong>Update: Solution I have Found</strong> </p>
  
  <p>I've just found a homemade solution. Here it is:
  I've added a onstart function to the link, but this generates a performance issue: it's called both as to save, to edit, and to cancel. And also I've changed the oncomplete function of the add row button.</p>
  
  <pre><code>&lt;p:dataTable value="rows" var="row" editable="true" 
   id="myTable" widgetVar="myTableVar" styleClass="myTableStyle"&gt;
  
      &lt;p:ajax event="rowEdit" listener="#{myBean.onUpdateRow}" /&gt;
      &lt;p:ajax event="rowEditCancel" /&gt;
  
      &lt;p:columnGroup type="header"&gt;
          &lt;p:column headerText="Name" /&gt;
          &lt;p:column headerText="Age" /&gt;
          ...
          &lt;p:column headerText="Edit" /&gt;
      &lt;/p:columnGroup&gt;
  
      &lt;p:column&gt;
          &lt;p:cellEditor&gt;
          &lt;f:facet name="output"&gt;
                  &lt;h:outputText value="#{row.name}" /&gt;
              &lt;/f:facet&gt;
              &lt;f:facet name="output"&gt;
                   &lt;h:inputText value="#{row.name}" /&gt; 
              &lt;/f:facet&gt;
          &lt;/p:cellEditor&gt;
      &lt;/p:column&gt;
  
      &lt;p:column&gt;
          &lt;p:cellEditor&gt;
          &lt;f:facet name="output"&gt;
                  &lt;h:outputText value="#{row.age}" /&gt;
              &lt;/f:facet&gt;
              &lt;f:facet name="output"&gt;
                   &lt;h:inputText value="#{row.age}" /&gt; 
              &lt;/f:facet&gt;
          &lt;/p:cellEditor&gt;
      &lt;/p:column&gt;
  
      ...
  
      &lt;p:column&gt;
          &lt;p:commandLink update="myTable" onstart="$('.myTableStyle tbody.ui-datatable-data tr td .ui-cell-editor-input').hide(); $('.myTableStyle tbody.ui-datatable-data tr td .ui-cell-editor-output').show(); $('.myTableStyle tbody.ui-datatable-data tr td span.ui-row-editor span.ui-icon-pencil').show();  $('.myTableStyle tbody.ui-datatable-data tr td span.ui-row-editor span.ui-icon-check').hide(); $('.myTableStyle tbody.ui-datatable-data tr td span.ui-row-editor span.ui-icon-close').hide(); $('.myTableStyle tbody.ui-datatable-data tr').removeClass('ui-state-highlight'); return false;"&gt;
              &lt;p:rowEditor /&gt;
          &lt;/p:commandLink&gt;
      &lt;/p:column&gt;
  
  &lt;/p:dataTable&gt;
  
  &lt;p:commandButton icon="ui-icon-plus" action="#{myBean.addNewRow}" update="myTable"
   oncomplete="$('.myTableStyle tbody.ui-datatable-data tr:last-child td .ui-cell-editor-input').show(); $('.myTableStyle tbody.ui-datatable-data tr:last-child td .ui-cell-editor-output').hide(); $('.myTableStyle tbody.ui-datatable-data tr:last-child td span.ui-row-editor span.ui-icon-pencil').hide();  $('.myTableStyle tbody.ui-datatable-data tr:last-child  td span.ui-row-editor span.ui-icon-check').show(); $('.myTableStyle tbody.ui-datatable-data tr:last-child  td span.ui-row-editor span.ui-icon-close').show(); $('.myTableStyle tbody.ui-datatable-data tr:last-child').addClass('ui-state-highlight'); return false;
   title="Add new row" /&gt;
  </code></pre>
  
  <p><strong>Update-2:</strong> Finally I found a solution to the performance issue. My problem was that the Javascript action was called when clicking to edit, save and cancel the row edition. To prevent that, I changed the onstart function to an onclick function that changes the other rows to non editable only when clicking the edit row button (pencil icon). To do that I use event.target, to know what element has clicked. As row edit, row edit save and row edit cancel button has different classes (ui-icon-pencil, ui-icon-check and ui-icon-close), it can be possible you can differentiate which button was pressed. So this is the function that replaces the onstart function:</p>
  
  <p><code>onclick="$(if($(event.target).hasClass('ui-icon-pencil')) {'.myTableStyle tbody.ui-datatable-data tr td .ui-cell-editor-input').hide(); $('.myTableStyle tbody.ui-datatable-data tr td .ui-cell-editor-output').show(); $('.myTableStyle tbody.ui-datatable-data tr td span.ui-row-editor span.ui-icon-pencil').show();  $('.myTableStyle tbody.ui-datatable-data tr td span.ui-row-editor span.ui-icon-check').hide(); $('.myTableStyle tbody.ui-datatable-data tr td span.ui-row-editor span.ui-icon-close').hide(); $('.myTableStyle tbody.ui-datatable-data tr').removeClass('ui-state-highlight');} return false;"</code></p>