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
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
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 |
The code
<!–
<a href="?playGame=” class=”magic-links”>Play |
–>
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
Please upload them online and give me the link.
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
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
Hi muki,
Dont place the links inside the container div
Place the PLAY links before the “container DIV”, it will work fine.
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?
Yes, we have to write custom code.
Let me try later.
Thanks.
ok sir
I will appreciate
hello, hope you have not forgotten this script 🙂
muki, 🙂
hi. Thank you
tnx . very helpfull
Pingback: 2010 review – Pagination and anchor scroll are most wanted | Beschi's Works
i haven’t tried this script. but this is the concept that everyone wants to know after learning the basic programming. you had written as if a kilipillai can understand. kudos man! keep going. We need more from you.
Thanks natraj.
This year I haven’t write much, some changes in my life and career keeping me busy. Now I am a Drupal developer. Will write about Drupal and Jquery in the following posts.
-Beschi A.
Muki,
take a look:
var targetUrl = url + " #content";
Must be
var targetUrl = url + " #container";
I have tried this examle as well as your other paging ajax example both ones only show the results on the first page. the only thing I changed was the db info and the column names
I have one array which can contains all of data now , i want to display it by paging and also want to do data manipulation in it such like ajax. have you any idea ?
“It works just once and when you click again, the whole page is reloaded”
Use the ‘live’ function. Try this:
$(“a.magic-links”).live(“click”,function(e){