weixin_33691817 2019-10-24 06:48 采纳率: 0%
浏览 8

分页活动链接

I am doing pagination with ajax for table which has some records for which i have to show active link for each click.Please do help me with this.

      $(document).ready(function()
      {  
             ready_data();  
             function ready_data(page)  
       {  
         $.ajax({  
            url:"connection.php",  
            method:"POST",  
            data:{page:page},  
            success:function(data){  
                 $('#paginate_record').html(data); 
                //  $('.page_links').removeClass('active');
                //  $(this).addClass('active');                  
            }  
       });   }  

     $(document).on('click', '.page_links ', function(){   
         var page = $(this).attr("id");  
         ready_data(page);  
     });  
    }); 

Here is My pagination page

    $records_per_page = 2;  
    $page = '';  

    if(isset($_POST["page"]))  
    {  
         $page = $_POST["page"];  
    }               
    else  
    {  
         $page = 1;  
    }  
    $start_from = ($page - 1)*$records_per_page; 
    $sql = "SELECT * FROM students  LIMIT $start_from, 
    $records_per_page"; 
    $result = mysqli_query($con,$sql);

    $data .= '</table><br /><div align="center">';  
    $page_sql = "SELECT * FROM students ";  
    $page_result = mysqli_query($con, $page_sql);  
    $total_records = mysqli_num_rows($page_result);  
    $total_pages = ceil($total_records/$records_per_page);

    for($i=1; $i<=$total_pages; $i++)  
    {    

      $data.= "<span class='page_links active' 
      style='cursor:pointer;margin:10px;padding:1px;border:1px 
       solid 
      ;'id='".$i."'>".$i."</span>";  

    }

    echo $data;

Now in which i want to show active link for each click.i tried by adding class on ajax succes function also add the active class on span tag but it is applying to all links.so please do help me.

  • 写回答

1条回答 默认 最新

  • weixin_33744854 2019-10-24 06:58
    关注

    In the last for() loop, you are applying the active class to all the output, you want to apply this only to the current page...

       for($i=1; $i<=$total_pages; $i++)  
        {    
    
          $data.= "<span class='page_links";
          if ( $i == $page ) {
              $data .=" active";
          }
          $data.= "' style='cursor:pointer;margin:10px;padding:1px;border:1px solid;'id='"
                   .$i."'>".$i."</span>";  
    
        }
    
    评论

报告相同问题?