如何获取被检查的表行。分页

时间:2019-07-31 02:20:34

标签: jquery foreach pagination

我的代码中有一个小错误,希望您能对我有所帮助。我有一个名为contacts-employee的表,其中包含员工信息,并且在第一列中具有一个复选框。我能够在第一页上获得选中的行,但是当我单击下一页并检查了一些行时,我单击了发送按钮。即使我检查了一些行也找不到任何检查的行。

to be more specific. 


var contactEmployeesPageNo = 0;

//this is the code for send
$('button[id=btn-save-emp]').click(function(){
      let employees = new Array();
      let tableControl = $('table[id=contacts-employees]');
      let sendtoctr = 0;

      $('input:checkbox:checked', tableControl).each(function() {
        let index = $(this).closest('tr').find('span[name=tbl-ctr]').text();
        if(index != ""){
          alert(index );
        }
      }).get();
    }
  });

// this is the button for pagination left
$('#contacts-employees-page-left').click(function(){
   if (contactEmployeesPageNo > 0){
     contactEmployeesPageNo = contactEmployeesPageNo - 10;
     GetContacts(contactEmployeesPageNo)
   }
});

// this is the button for pagination right
$('#contacts-employees-page-right').click(function(){
  var rowCount = $('#contacts-employees tr').length;
  if(rowCount > 1){
    contactEmployeesPageNo =  contactEmployeesPageNo + 10;
    GetContacts(contactEmployeesPageNo);
  }
});


//getting contact from php.
GetContacts = (emppage) => {
  $.ajax({
  type: 'POST',
  dataType: "json", 
  url: 'getcontact.php',
  data: {mode:27,emppage:emppage},
  success: function(result){
      $('tbody[id=contacts-employees-list]').empty();
      $('tbody[id=contacts-employees-list]').append(result[1]);
      contactEmployees = result;
  error: function (xhr, ajaxOptions, thrownError) {

   },
});

//php code
 public function GetContacts($offsetemployee,$offsetsection,$offsetdepartment){

 $stmt3 = $this->db->prepare('SELECT empno, empname, concat(department," - ", position) as `position` from employees ORDER by empname ASC LIMIT 30 OFFSET :page ');

 $stmt3->bindParam(':page', $offsetemployee, PDO::PARAM_INT);
 $stmt3->execute();

 $result3 = $stmt3->fetchAll(PDO::FETCH_ASSOC);

 $contacts = array();
 $html3 = '';


 if($stmt3->rowCount() > 0){
    $ctr = 0;
    $listofemployee = array();
    foreach($result3 as $row) {
       $empno = $row['empno'];
       $listofemployee[$ctr] = $empno;
       $empname = $row['empname'];
       $position = $row['position'];
       $empctr = $ctr  + 1;


       $html3 .= '<tr>
                        <td>
                          <div class="custom-control custom-checkbox">
                              <input type="checkbox" class="custom-control-input" id="emp'.$ctr.'">
                              <label class="custom-control-label" for="emp'.$ctr.'">'. $empctr .'</label>
                              <span style="display: none;" name="tbl-ctr">'. $ctr .'</span>
                          </div>
                        </td>
                        <td>'. $empname .'</td>
                        <td>'. $position .'</td>
                      </tr>';
            $ctr++;
          }
          $contacts[0] = $html3
        }else{
          $contacts[0] = "";
        }

        echo json_encode($contacts);
}

when i did not click the button for next or back page, the for loop shows the checked rows but when i click the next page or the back page, it doesnt show any checked rows even if i checked everthing on the row

1 个答案:

答案 0 :(得分:0)

//I tried this bbut doesnt work  

  GetContacts = (emppage) => {
      $.ajax({
      type: 'POST',
      dataType: "json", 
      url: 'getcontact.php',
      data: {mode:27,emppage:emppage},
      success: function(result){
          $('tbody[id=contacts-employees-list]').empty();
          $('tbody[id=contacts-employees-list]').append(result[1]);
          contactEmployees = result;
          //this is the code for send
    $('button[id=btn-save-emp]').click(function(){
          let employees = new Array();
          let tableControl = $('table[id=contacts-employees]');
          let sendtoctr = 0;

          $('input:checkbox:checked', tableControl).each(function() {
            let index = $(this).closest('tr').find('span[name=tbl-ctr]').text();
            if(index != ""){
              alert(index );
            }
          }).get();
        }
      });`enter code here`
      error: function (xhr, ajaxOptions, thrownError) {

       },
    });