Welcome Guest, Not a member yet? Register   Sign In
Pagination with AJAX using jquery
#1

[eluser]HarishKumar[/eluser]
I have read many topics related to Pagination with Jquery.I have got the good ideas from different topics.From that learned topics i have used very simple code to do pagination with Jquery.. Following is my View and Controller. Try this code. By using this you will get the idea how Jquery do pagination..



View pagination.php

<html>
<head>
[removed][removed]
[removed]
$(document).ready(function(){
myfun('');
function myfun(limit)
{
pathArray = [removed].pathname.split( '/' );
var url=pathArray[0]+"/"+pathArray[1];
$.post(url+"/index.php/urcontroller/urfunction/"+limit, {}, function (table) {

$('body').html('');
$('body').append(table);
$("body strong").attr('id',limit);
$("body a").attr('href','[removed]void(0);');
$("body a").bind("click", function(e)
{
limit=$(this).attr('id');
myfun(limit);
});
});
}
});
[removed]
</head>
<body>

</body>
</html>


In this view code i have written nothing in the body section. In this example i am displaying data when the page is loaded.An ajax request sent to the controller's function and the output is loaded into the body tag in the script code.In the script i have used a variable named url ,this variable will help u to send request to your controller...
If you have http://your ip/projectname/controller/function name this
then the variable url contains value your ip/projectname..




Controller Urcontroller.php


<?php

class Urcontroller extends Controller {

function Urcontroller()
{
parent::Controller();
$this->load->model('login');
$this->load->library('pagination');
}
function index()
{
$this->load->view('pagination');
}
function urfunction()
{
$config['base_url'] = base_url().'index.php/urcontroller/ajax_index';
$total_rows=$this->login->get_total_members()->num_rows;
$config['total_rows'] = $total_rows;
$limit=$this->uri->segment(3);
if($limit=="")
$limit=0;
$offset=$config['per_page'] = 2;
$config['first_link'] = 'First';
$config['last_link'] = 'Last';
$this->pagination->initialize($config);
$data['page_links']= $this->pagination->create_links();
$data['members']=$this->login->get_members($limit,$offset);
echo $data['page_links'];

echo "<div id='pagination'>";
echo "<table width='70%' border='1' cellpadding='1' cellspacing='0'>
<tr bgcolor='red'>
<th align='left' width='20%'>ID</th>
<th width='50%' align='left'>First Name</th>
</tr>";
foreach($data['members']->result() as $row):
echo "<tr>

<td width='20%'> $row->id</td>
<td width='50%'>$row->v_name</td>
</tr>";
endforeach;
echo "</table></div>";

}
}


In this controller i have used pagination library.
Login is just my database class to show id and first name attribute on the page.
To perform pagination i have do a little bit changes in my pagination library.
I have just added an attribute id in all the anchor tags of pagination library..
This id contains the location of your next page to visit other pages.Following is the change for digit links in library.

$output .= $this->num_tag_open.'<a >base_url.$n.'" id="'.$n.'" >'.$loop.'</a>'.$this->num_tag_close;


you can also use pagination file from the attachment. From this file just replace your create_link() of pagination file with create_links() of this attachment.

This code is working fine on all browser perfectly. Try to use this code to perform pagination.
I hope its really helpful.
Tell me if you have any query regarding this code..




Theme © iAndrew 2016 - Forum software by © MyBB