Jump to content

Pagination with Ajax driven table


rushy
 Share

Recommended Posts

Has anyone used pagination with ajax driven content? I'm using the MarkupPagerNav module to give me pagination, but my content is updated by an ajax post to refresh the body content of a table. The pagination links show a myproject/http404/?page=n for each link generated so they give a 404 page not found. I believe this is because the file containing my ajax code resides in the root of my project as a "web service" and does not have the access it needs, but I'm unsure really.

If anyone can throw any light on this I would be very grateful. I include the code with my ajax below.

<?php namespace ProcessWire;

require_once ('./index.php'); // bootstrap ProcessWire

if($config->ajax) 
{
    $pg = $pages->get("/single-use-carrier-bags/");
 
  	 $selector = wire('input')->post('selector');
  	 
    $mytableContent = ''; // hold the table markup
 
 	// $retailer = $pg->children($selector); 
	// for info. only $selector looks like this ->
 	$retailer = $pg->children("sort=title, include=all, limit=10");

	$pageinate = $retailer->renderPager();
 	echo $pageinate;
 	
 	foreach ($retailer as $r) 
    {
        $mytableContent .= "<tr>
                        	<td><a href= '$r->url' target='_blank'>$r->title</a></td>
  				        	<td>$r->category</td>
       					  	<td>".numFormat($r->no_bags_issued)."</td>
      					  	<td>".numFormat($r->gross_proceeds)."</td>
                          	<td>".numFormat($r->net_proceeds)."</td>
                          	<td>$r->other_use_net_proceeds</td>
                          	<td>".numFormat($r->amount_donated)."</td>		                                           						 
                          	<td>$r->good_causes_in_receipt</td>
                          	<td>".numFormat($r->no_paper_bags_issued)."</td>
                          	<td>".numFormat($r->no_bags_for_life)."</td>
                          </tr>";
    }

   echo $mytableContent;

}
?>

 

Edited by rushy
show $selector useage
Link to comment
Share on other sites

You might find it easier to debug this and get it working by using a PW page to respond to your AJAX requests.

So you create a template named "web-service" (for example) and put the code above into the template file (minus the bootstrapping line). Then create a page named "web-service" that uses this template and use this page's URL in your AJAX request.

You might be able to get pagination working simply by enabling pagination for the template, or you might need to get the page number using the API and use it in conjunction with the "limit" to get the "start" for your selector. I haven't tried AJAX pagination myself.

Link to comment
Share on other sites

Ok thanks for the ideas. Eventually I got it working by using my own pagination buttons - just next & prev. as the PW MarkupPagerNav module did not suit me in my scenario. I just work out what page should be displayed and send a param in my Ajax call so that the body rendering loop in my "webservice" file outputs the correct rows from the table. PW pagination uses the page numbering in the template and the renderPager() renders links that call a template instance with a page no. not what I wanted in this case.... maybe it could work nicely with delayed output - I'm still learning this, but this worked for me - so a big thank you to the above folks who gave me ideas!

Paul

Edited by rushy
A solution
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...