tags:

views:

21

answers:

1

So I've got some pagination working, but there's a variable I need access to (total pages). When I get the data to display in the table from changing the page (or otherwise updating the table), what's a good way to get the variable in the same request?

+1  A: 

If your data is being transfered in some well defined form like XML or JSON, you can simply add an extra parameter to your output. $.post can be configured to decode the server's response as XML or JSON via the last parameter.

Using JSON, your client code might look something like this:

// Request page 3 from the server
$.post(url, {page:3}, function(data, textStatus, XMLHttpRequest) {
   // data.page contains the HTML for page 3
   // data.num_pages contains the number of pages
}, 'json');

And (assuming PHP on the server-side) you'd output your JSON data thusly:

<?php
  // instead of dumping the HTML for page 3 directly to the browser,
  // send it as part of a JSON-encoded response
  $page = '<div class="page">Page 3 of 10</div>';

  echo json_encode(array(
    'page' => $page,
    'num_pages' => 37, // substitute with your calculated number of pages
  ));
?>
meagar