Articles: 843 | Categories: 148   
   
   
Home Articles Contact Us
 
 
 
 
Pager Tutorials (0 Comments)
Admin: Posted Date: April 4, 2010

First, let's see a simple example on how to harness Pager to create javascript links. In this rather semplicistic example, we fetch all the data into paginated chunks, store each page into a <div> and use some DOM scripting.

Example #1 - Pager and Javascript

First, let's see a simple example on how to harness Pager to create javascript links. In this rather semplicistic example, we fetch all the data into paginated chunks, store each page into a <div> and use some DOM scripting to hide all the layers but the current page.

01.<?php
02.require_once 'Pager/Pager.php';
03.$data = range(1, 100); //an array of data to paginate
04.$pager_params = array(
05.    'mode'     => 'Sliding',
06.    'append'   => false,  //don't append the GET parameters to the url
07.    'path'     => '',
08.    'fileName' => 'javascript:revealDiv(%d)',  //Pager replaces "%d" with the page number...
09.    'perPage'  => 10, //show 10 items per page
10.    'delta'    => 5,
11.    'itemData' => $data,
12.);
13.$pager = & Pager::factory($pager_params);
14.$n_pages = $pager->numPages();
15.$links = $pager->getLinks();
16.?>
17.<html>
18.<head>
19.    <script type="text/javascript">
20.    var n_pages = <?php echo $n_pages ?>;
21.    function revealDiv(n)
22.    {
23.        for (var count = 1; count <= n_pages; count++) {
24.          document.getElementById("page"+count).style.display = 'none';
25.        }
26.        document.getElementById("page"+n).style.display = 'block';
27.    }
28.    </script>
29.    <style type="text/css">
30.    div.page {
31.      background: #FFFF99;
32.      border-top: 1px solid #FFBF99;
33.      border-bottom: 1px solid #FFBF99;
34.    }
35.    </style>
36.</head>
37.<body>
38.<h1>PEAR::Pager example with JavaScript</h1>
39.<?php echo $links['pages']; ?>
40.<hr />
41.<?php
42.for ($i=1; $i <= $n_pages; ++$i) {
43.    echo '<div class="page" id="page'.$i.'">';
44.    echo '<h2>Page '.$i.'</h2>';
45.    foreach ($pager->getPageData($i) as $item) {
46.        echo 'Item '.$item.'<br />';
47.    }
48.    echo '</div>';
49.}
50.?>
51.<hr />
52.<script type="text/javascript">
53.revealDiv(1);
54.</script>
55.</body>
56.</html>

As you could see, the trick was setting the path parameter to an empty string and the fileName parameter to a javascript link, with the usual "%s" placeholder for the pageID.

Example #2: We want AJAX We want

Ok, now that you've seen the basics, you should have all the elements to go further. But if you're lazy and want to see it anyway, here's an example on how to do the same thing we've seen before.

1) The html file: we include the dynamic js file (server.php) to handle the AJAX request, and call "HTML_AJAX.replace('target', 'testdata.php')", which will replace the contents of the target div with the output of the testdata.php script using an AJAX call.

01.<html>
02.<body>
03.<h1>PEAR::Pager example with AJAX</h1>
04. 
05.<script type="text/javascript" src="server.php?client=all"></script>
06. 
07.<div id="target">I'm the target</div>
08. 
09.<script type="text/javascript">
10.    HTML_AJAX.replace('target', 'testdata.php');
11.</script>
12. 
13.</body>
14.</html>

2) The testdata.php file: it's a simple php script where you fetch the data you want to paginate (in this example, 100 integers) and pass it to Pager. The output of this script will replace the contents of the target div in the first html file. We also print the current datetime to prove the data is "fresh" and built at the time of each call (i.e. every time you click on a navigation link).

01.<?php
02.require_once 'Pager.php';
03.$data = range(1, 100); //an array of data to paginate
04.$pager_params = array(
05.    'mode'     => 'Sliding',
06.    'append'   => false,  //don't append the GET parameters to the url
07.    'path'     => '',
08.    'fileName' => 'javascript:HTML_AJAX.replace(\'target\',\'testdata.php?pageID=%d\');',  //Pager replaces "%d" with the page number...
09.    'perPage'  => 10, //show 10 items per page
10.    'delta'    => 1,
11.    'itemData' => $data,
12.);
13.$pager = & Pager::factory($pager_params);
14.$n_pages = $pager->numPages();
15.$links = $pager->getLinks();
16.echo '<p>This container is loaded with an AJAX call</p>';
17.echo '<p><span class="datetime">DateTime: '. date('Y-m-d H:i:s') .'</span></p>';
18.echo '<h3>Page '. $pager->getCurrentPageId() .'</h3>';
19.foreach ($pager->getPageData() as $item) {
20.    echo 'Item '. $item .'<br />';
21.}
22.echo '<hr />'.$pager->links;
23.?>


3) Finally, the server.php file: we create an instance of HTML_AJAX_Server to deliver both the javascript libraries and to handle AJAX requests from browsers.

1.<?php
2.include 'HTML/AJAX/Server.php';
3. 
4.$server = new HTML_AJAX_Server();
5.$server->handleRequest();
6.?>

 

 
 
Add a Comment:
 
(You must be signed in to comment on an article. Not a member? Click here to register)
   
Title:

Comments: