How to apply search filters in cakephp ajax pagination

Sometimes we need to filter records via custom query while using ajax pagination in a cakephp application. An easy way to achieve this is to store query in a session varibale and to check it whenever an ajax call is made through “previous”, “next” or pagination links like 1,2,3 etc.

Here is the controller function code to fetch results from a table:

$this->paginate = array('limit' => 20,"order"=>" ASC");
$this->set('charities', $this->paginate('Charity',$this->_filterCharitySearch()));

In the above code, line one sets the pagination parameters ie limit, order etc., while the second line fetches the records from the Charity table and sets them for the view. Our main concern here is the second parameter ($this->_filterCharitySearch()) passed to the “paginate” function of controller class. The function _filterCharitySearch() performs the desired action to make search filters work. Below is the source code for _filterCharitySearch():

function _filterCharitySearch()	{

	if(!$this->RequestHandler->isAjax()) {

	if(!empty($this->data) && strlen(trim($this->data['Charity']['keyword']))>=2)
		$search = $this->data['Charity']['keyword'];
		$search = $this->Session->read($this->name.'.keyword');

	$filters = array();
	if(isset($search)) {
		$search = low($search);
		$filters = array("lower( like '%".$search."%' or lower(Charity.initials) like '%".$search."%' 
or lower(Charity.short_description) like '%".$search."%' or lower(Charity.full_description) like '%".$search."%' 
or lower(Charity.individual_description) like '%".$search."%' or lower(Charity.contact_dump) like '%".$search."%' 
or lower(Charity.contact_email) like '%".$search."%' or lower(Charity.contact_website_url) like '%".$search."%'");
		$this->Session->write($this->name.'.keyword', $search);

	return $filters;

Before going into the explaination of code above let’s have a look at the view section page result. Below is the result page which is supposed to have search form and ajax pagination on a web page.

Charity Pagination Ajax
Ajax Pagination listing Screenshot

As per the figure we have got a search form at the top and charity records.

Ok , now line by line explanation of the code snippet (_filterCharitySearch() function). In the line 3 of the function we check whether this is a direct page call (and not a ajax call). If it is not a ajax call we need to reset search filters which is done in line 4 by deleting previously stored session containing previous searched term. Line 7 to 11 checks whether there is valid form submission occured, if so, it stores posted value ($this->data[‘Charity’][‘keyword’]) in a “$search” variable otherwise(form is not submitted) it checks for the session value and stores it in the “$search” variable if found.

Now if we have “$search” variable already set we need to create desired filters for our db query and store our “$search” value in a session so we can use that in the next ajax call like we did just now. One can store “$filters” variable into the session, do whatever you like.

Now as a process in $this->set(‘charities’, $this->paginate(‘Charity’,$this->_filterCharitySearch())); “_filterCharitySearch()” returns a set of conditions which filters the results from the model based on search performed by the user.

It was a simple example of ajax pagination filters in cakephp application. Hope that helps someone!

2 thoughts on “How to apply search filters in cakephp ajax pagination

Leave a Reply