jQuery Autocomplete widget for CakePHP 2.2

A JavaScript autocomplete element that links through to your database isn’t built into CakePHP any more – here is a simple solution to add to your application using a jQuery plugin.
Why a plugin? jQuery doesn’t include this functionality, it is provided by jQuery UI but including that library may be a heavyweight solution for your application.

It should end up looking like the image below – as you type, relevant options from your database will appear in the list beneath.

CakePHP autocomplete widget

You can download the jQuery autocomplete plugin from https://github.com/dyve/jquery-autocomplete
Extract the files from the archive and move the .css and .js files from the src folder into the css and js folders of the CakePHP webroot directory.

Link to the CSS and JavaScript in your default layout:

echo $this->Html->css('jquery.autocomplete');
echo $this->Html->script('jquery.autocomplete.min.js');

Add a small form to your View, in this example there is a model called company and we want to use autocomplete on the name field.

<?php
echo $this->Form->create('Company', array('type' => 'post', 'action' => 'find'));
echo $this->Form->input('name');
echo $this->Form->submit();
echo $this->Form->end();
?>

Add this script block to the bottom of the document which activates the plugin and tells it where to submit AJAX requests to:

<script>
  $(document).ready(function(){  
    $("#CompanyName").autocomplete("/Companies/find.json", {
    minChars: 3
    });
  });
</script>

Modify jQuery.autocomplete.css to look nice with CakePHP default CSS:

.acResults ul li {
	margin: 0px;
	padding: 2px 5px;
	cursor: pointer;
	display: block;
	font: menu;
	overflow: hidden;
	color: #333;
}

Now back to some CakePHP – Your Controller needs a new action which the AJAX requests will be sending data to:

public function find() {
  $this->Company->recursive = -1;
  if ($this->request->is('ajax')) {
    $this->autoRender = false;
    $results = $this->Company->find('all', array(
      'fields' => array('Company.name'),
      //remove the leading '%' if you want to restrict the matches more
      'conditions' => array('Company.name LIKE ' => '%' . $this->request->query['q'] . '%')
    ));
    foreach($results as $result) {
      echo $result['Company']['name'] . "\n";
    }
 
  } else {
  	//if the form wasn't submitted with JavaScript
    //set a session variable with the search term in and redirect to index page
    $this->Session->write('companyName',$this->request->data['Company']['name']);
    $this->redirect(array('action' => 'index'));
  }
}

And finally add some code to the index action in the index action, which handles what happens if the form was submitted via a button press rather than AJAX.

//if there's a session with some data in, add a filter to the search conditions
if ($this->Session->check('companyName')) {
  $name = $this->Session->read('companyName');
  if ($name) {
    $this->paginate['conditions'][] = array('Company.name LIKE' => '%' . $name . '%');
    $this->request->data['Company']['name'] = $name;
  }
}

That’s it.

To to make submit buttons appear alongside form elements
here is a simple bit of jQuery to improve the appearance

$(document).ready(function(){
  $('div.input, div.submit').css({
    'float' : 'left',
    width : '200px',
    clear : 'none'
  });
  $('div.submit').css('margin-top', '14px');
  $('div.input.text').css('margin-top', '-4px');
});

Thanks to the answer on this question from StackOverflow for some inspiration
http://stackoverflow.com/questions/6071828/autocomplete-search-form-cakephp

This entry was posted in JavaScript, PHP. Bookmark the permalink.

12 Responses to jQuery Autocomplete widget for CakePHP 2.2

  1. Pingback: jQuery Autocomplete widget for CakePHP 2.2 | Richard Willis-Owen - Web 2.0 BLOG | Web 2.0 BLOG

  2. jon says:

    I could not get this to work on Cake 2.2.4. A few questions:
    1.Where is code converted to json?
    2.I could not find any access to the find action being called in my logs either.

    • Richard says:

      There is no JSON conversion taking place – the controller action is just echoing out plain text separated by new lines – the autocomplete plugin can deal with this just fine.
      Try using Firebug and look in the ‘Net’ tab, you should see any GET requests plus the responses in there.

  3. Kelly says:

    $(document).ready(function(){
    $("#CompanyName").autocomplete("/Companies/find.json", {
    minChars: 3
    });
    });

    I’m running 2.1.1 atm, but had to change this. I needed to drop the .json off the end (otherwise the controller couldn’t find the function).

    Also, as I was using admin routing, and doing this in an admin page, the url was just the name of the function. ‘/Companies/find’ took it to the root of the application, losing the admin, ‘Companies/find’ meant it was duplicated like /admin/companies/Companies/find. Eventually I just changed it to ‘find’ and it worked!

    • Kelly says:

      ooh, no. The trailing slash is dependent on if your url finishes with a /

      domain/admin/controller/ = domain/admin/controller/controller/action
      domain/admin/controller = domain/admin/controller/action

      any ideas? I suspect basically admin routing is ballsing it up and I’m not sure how to handle it properly.

  4. Daniel says:

    hi,
    I’m getting this error in console:
    “Uncaught Error: cannot call methods on autocomplete prior to initialization; attempted to call method ‘/Venues/find.json’ ”

    Any ideas?

  5. Please, I’m a lammer that need finish my job with this kind of autocomplete plugin. Like you know, cakephp launch in dropdowns a array with `id` and `name`( for example) , but just shows to us the `name`.
    There is a way to use this plugin ( autocomplete widget cakephp) saving the `id` in the field but searching by the `name` ?
    Thanks to save my job

  6. Aguiran says:

    Just activate the .json extension in routes.php and the output view will automatically convert your data in json

    Router::parseExtensions(‘json’);

  7. mks0491 says:

    Great tutorial. You have shared good knowledge. To know How To: Create auto-complete using jquery and css check this http://www.webdeveloperjuice.com/2010/04/20/create-auto-complete-using-jquery-and-css/

  8. Jari Kuokka says:

    When I add more fields to the “‘fields’ => array(‘Company.name’)” -clause like “‘fields’ => array(‘Company.name’, Company.address, Company.postalcode)”, how can I use those fields in form? After I select the company by it’s name, three more input fields would be populated due to selected company id? How to use more information field at the same time?

Leave a Reply

Your email address will not be published. Required fields are marked *


× 4 = 36

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>