title: To do ajax refresh of JSF/primefaces datatable
body: <p>i am trying to do JSF/primefaces ajax update on the datatable on click of a command link using which i am deleting the records.On clicking the command link i the database get supdated but the datatable doesn't get refreshed even if i use update="@form" attribute.Any guidance on how to solve this problem would be very helpful.</p>
  
  <p>My JSF code snippet which is a row editable datatable that contain few columns 
  like state,region is as below</p>
  
  <pre><code>&lt;h:form id="lpcForm"&gt;
  
      &lt;div id="content"&gt;
          &lt;p:commandLink id="cmdLinkDelete" value="Delete" ajax="true"
              action="#{lpcBean.deleteRecords}" update="lpcDataTable" /&gt;
  
          &lt;p:dataTable var="lpcData" id="lpcDataTable" widgetVar="lpcTable"
              value="#{lpcBean.lpcIdList}" selection="#{lpcBean.selectedRows}"
              editable="true" scrollable="true" scrollWidth="1110"
              scrollHeight="330" styleClass="datatable" resizableColumns="true"&gt;
              &lt;p:ajax event="rowEdit" listener="#{lpcBean.onEdit}" /&gt;
              &lt;p:ajax event="rowEditCancel" listener="#{lpcBean.onCancel}" /&gt;
  
              &lt;p:column selectionMode="multiple" style="width:15px" /&gt;
  
              &lt;p:column headerText="LPC ID" style="width:20px"&gt;
                  &lt;h:outputText value="#{lpcData.LPCID}" /&gt;
              &lt;/p:column&gt;
  
              &lt;p:column headerText="Region" style="width:40px"&gt;
                  &lt;p:cellEditor&gt;
                      &lt;f:facet name="output"&gt;
                          &lt;h:outputText value="#{lpcData.region}" /&gt;
                      &lt;/f:facet&gt;
                      &lt;f:facet name="input"&gt;
                          &lt;h:selectOneMenu value="#{lpcData.region}"&gt;
                              &lt;f:selectItems value="#{lpcBean.regions}" /&gt;
                          &lt;/h:selectOneMenu&gt;
                      &lt;/f:facet&gt;
                  &lt;/p:cellEditor&gt;
              &lt;/p:column&gt;
  
  
              &lt;p:column headerText="State" style="width:15px"&gt;
                  &lt;p:cellEditor&gt;
                      &lt;f:facet name="output"&gt;
                          &lt;h:outputText value="#{lpcData.homeState}" /&gt;
                      &lt;/f:facet&gt;
                      &lt;f:facet name="input"&gt;
                          &lt;h:selectOneMenu value="#{lpcData.homeState}"&gt;
                              &lt;f:selectItems value="#{lpcBean.mainStates}" /&gt;
                          &lt;/h:selectOneMenu&gt;
                      &lt;/f:facet&gt;
                  &lt;/p:cellEditor&gt;
              &lt;/p:column&gt;
  
              .
              .
              .
  
              &lt;p:column style="width:10px"&gt;
                  &lt;p:rowEditor /&gt;
              &lt;/p:column&gt;
  
          &lt;/p:dataTable&gt;
  
      &lt;/div&gt;
  &lt;/h:form&gt;
  </code></pre>
  
  <p>The variable lpcIdList is of the type of the class that implements <strong>DataModel</strong>.</p>
  
  <p>Below is the Managed bean snippet that contains <strong>deleteRecords</strong> method.Managed bean is of <strong>RequestScoped</strong></p>
  
  <pre><code>public String deleteRecords()
  {
      System.out.println("inside delete records");
      System.out.println("Selected rows length:"+ selectedRows.length);
      for(LPC itemSelect:selectedRows)
      {
          String lpcId=itemSelect.getLPCID();
          JdbcConnection jdbcConnection=new JdbcConnection();
          connection=jdbcConnection.getJdbcConnection();
          try{
              preparedStmt=connection.prepareStatement("delete from LPC where LPCID=?");
              preparedStmt.setString(1, lpcId);
              preparedStmt.execute();
  
          }
          catch(Exception e)
          {
              e.printStackTrace();
          }
  
  
      }
      return null;
  
  
  }
  </code></pre>
  
  <p>The variable <strong>selectedRows</strong> is the selection attribute of the datatable</p>
  
  <p>The method getLpcFields which fetches the data from database</p>
  
  <pre><code>public void getLpcFields() {
  
      try
      {
  
          JdbcConnection jdbcConnection =new JdbcConnection();
          connection=jdbcConnection.getJdbcConnection();
          System.out.println("connected to the database");
  
          selectStmt1 = connection.createStatement();
          ResultSet rs1 = selectStmt1.executeQuery("select * from LPC order by LPCID");
  
  
          while(rs1.next()){
  
  
              lpcItems.add(new LPC(rs1.getString("LPCID"),rs1.getString("Name_First"),rs1.getString("Name_Last"),rs1.getString("Email_Address"),rs1.getString("Region"),
                      rs1.getString("HomeState"),rs1.getString("ProfitCenter"),rs1.getString("Active_LPC"),rs1.getString("Name")));
          }
          selectStmt2=connection.createStatement();
          ResultSet rs2=selectStmt2.executeQuery("Select * from Tbl_state");
  
  
          while(rs2.next())
          {
              mainStates.add(rs2.getString("State"));
  
          }
          selectStmt3=connection.createStatement();
          ResultSet rs3=selectStmt3.executeQuery("Select * from Tbl_Regional_Managers");
  
          while(rs3.next())
          {
              regions.add(rs3.getString("Location"));
          }
          selectStmt4=connection.createStatement();
          ResultSet rs4=selectStmt4.executeQuery("Select * from YesNo order by YesNo_Key desc");
  
  
  
          while(rs4.next())
          {
              activeLpcs.add(rs4.getString("Description"));
          }
  
  
      }
      catch (Exception e)
      {
  
          e.printStackTrace();
      }
      finally
      {
          try {
  
              connection.close();
          } catch (SQLException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
  
      }
  
  
  }
  </code></pre>