Pagination Search Result in Cakephp 1.1

In previous lesson, i talked about pagination in cakephp 1.1.  In this lesson, i'm going to talk about pagination search result in cakephp 1.1.(like previous method).
With PaginationHelper and PaginationComponent in previous lesson, they only help conventional pagination. But, when we add search condition into request, pagination has been error. The reason is that when we implement paging of pages the variables will be transmitted via a URL in the GET method. So that the filter conditions in the form will not be passed on, and when intializing the page, it will not understand the conditions transmitted from the GET method, so paging no longer accurate.




There are two way to solve this problem :
  • Transmit all conditiona and parameters via a URL in the GET method
  • Transmit filter condition to the POST method and pagination parameter to the URL in the GET method.
The first way has a big disadvatage that is if we transmit all condition via a URL, URL will very long and ugly. Simultaneous, URL only have maximum 256 character, so when we search with big data condition, the first way is impossible.

So, we choose the second way : transmit filter condition to the POST method and pagination parameter to the URL in the GET method.

How to perform:

In previous lession, pagination will be perform through GET method that means links will include paging parameters. Pagination will completly base on that parameters.

Now, when clicking on the link, instead of activating the link which we will call a javascript function through the onclick function. We will submit data to the POST method instead of GET method simultaneous transmit paging parameters to URL. After submit, we will have data in POST methed and paging parameters in GET method.

The step are:

First, we add 'onclick' event in the paging link. Open '/app/views/helpers/pagination.php' file, go to 'function _generateLink ()', move to the end of function and change :
return $this->Html->link($title,$url,NULL,NULL,$escapeTitle)
to :
$options['onclick'] = "return paginator_number('form', '{$this->Html->url($url)}')";
return $this->Html->link($title,$url,$option,NULL,$escapeTitle)
Above, we called 'pagination_number' function with 2 parameters:

  • Name of form : 'form'
  • Url with paging parameters (this value will be auto generate, we needn't change)
Note : You must set name to form is 'form', otherwise, function can't run.
After that, we will add function : 
function paginator_number(element, url ) {
 $(element).attr("action",url); 
 $(element).submit(); 
 return false; 
}

This function will be add to layout. It use some function of jquery. If you don't import JQuery, import it now. Good luck.

Share this

Related Posts

Previous
Next Post »

1 comments:

comments