Ajax Jquery Paging with Magic Links
What Is This?
Already we have seen the Magic Link post – a tricky ajax links for making it works with the javascript disabled browsers.
Here, we are going to see how to make a pagination ajax links in the above mentioned trick.
By this way, one can easily change a normal paging into ajax paging by adding a javascript and making some changes.
Understanding The Layout
Imagine already we have a normal page with a pagination (say index.php).
First time, the default page number is 1 and the first page will load, for paging we have the links like
<a class="magic-links" href="index.php?page=1">1</a> <a class="magic-links" href="index.php?page=2">2</a> <a class="magic-links" href="index.php?page=3">3</a> . . .
The content(table) loads inside a content div, this content div is placed inside container div.
<div id="container" style="height: 200px;"> <div id="content"> <table border="0"> . . .</table> </div> </div>
When we click on a page link the whole page will reload again with new content in the table.
The Trick
On clicking the page link do the following,
1. stop the default action
2. get the href value (index.php?page=x)
3. send request to server via ajax
4. get the result
5. load content div into container div
Can’t understand? see the code,
$("a.magic-links").click(function(e){
e.preventDefault();
//define the target and get content then load it to container
var url = $(this).attr("href");
var targetUrl = url + " #content";
container.load(targetUrl, hideLoading);
});
Thats all, now the normal paging will work as ajax paging. Also it will work though there is no javascript on the browser.
Demo and Download
After adding some loading and page number highlight effects the complete code:
DEMO
DOWNLOAD
hello,
this is a wonderful script. However, I modified it and I’m having problems.
As per my game script, I need to put the link in the content DIV like this
<a href="?gmeid=2" rel="nofollow">Play</a>
In this case, the games will be shown except the game just clicked.
But… It works just one time. The next time I click, it reloads the whole page (meaning the effect is not applied) Please help correct this
Thanks
muki
October 22, 2009
Thanks muki.
Please go to
http://yensdesign.com/2009/06/safe-ajax-links-using-jquery/
and understand the magic link concept, this is just a implementation of that concept.
Still getting problem, send me the sample code.
-Beschi
beski
October 23, 2009
Hello,
thanks for the reply.
I’m still having problems. Please I’m not used to jquery and javascript.
Here is the code
<a href="?playGame=” class=”magic-links”>Play |
muki
October 23, 2009
The code
<!–
<a href="?playGame=” class=”magic-links”>Play |
–>
muki
October 23, 2009
I’m having problems pasting the code here. However, I place the PHP code with the
It works just once and when you click again, the whole page is reloaded
muki
October 23, 2009
Please upload them online and give me the link.
beski
October 23, 2009
I’m having problems pasting the code here. However, I place the PHP code with the DIV id=”content” block
It works just once and when you click again, the whole page is reloaded
muki
October 23, 2009
Oh sorry. I have had a series of connection issues.
Here is the link http://camarues.com/jquery.zip
Run it and click ‘play’ This works the first time. When you click ‘Play’ again, it reloads the whole page
muki
October 23, 2009
Hi muki,
Dont place the links inside the container div
Place the PLAY links before the “container DIV”, it will work fine.
beski
October 23, 2009
Hello,
how do I refresh the games listed? I want to do it that when the user clicks ‘play’ The game in question should disappear from the list. Is there any way out?
muki
October 23, 2009
Yes, we have to write custom code.
Let me try later.
Thanks.
beski
October 23, 2009
ok sir
I will appreciate
muki
October 23, 2009
hello, hope you have not forgotten this script
muki
October 24, 2009
muki,
beski
October 29, 2009