Skip to content

Instantly share code, notes, and snippets.

@swthate
Created October 18, 2014 19:01
Show Gist options
  • Save swthate/09d0ce770320fd5d5ecf to your computer and use it in GitHub Desktop.
Save swthate/09d0ce770320fd5d5ecf to your computer and use it in GitHub Desktop.
Another attempt at auto-complete
<?php
# app/Model/Animal.php
class Animal extends AppModel {
}
<?php
# File: app/controllers/AnimalsController.php
class AnimalsController extends AppController {
public $components = array('RequestHandler');
public $helpers = array('Js' => array('Jquery'));
/* index action */
public function index() {
if ($this->request->is(array('ajax'))) {
$this->loadModel('Animal');
$items = $this->Animal->find('list', array(
'conditions' => array(
'name LIKE' => '%' . $this->request->query('term') . '%'
)
));
$this->set('items', $items);
$this->set('_serialize', array('items'));
}
}
}
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package app.View.Layouts
* @since CakePHP(tm) v 0.10.0.1076
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
$cakeDescription = __d('cake_dev', 'CakePHP: the rapid development php framework');
$cakeVersion = __d('cake_dev', 'CakePHP %s', Configure::version())
?>
<!DOCTYPE html>
<html>
<head>
<?php echo $this->Html->charset(); ?>
<title>
<?php echo $cakeDescription ?>:
<?php echo $this->fetch('title'); ?>
</title>
<?php
echo $this->Html->meta('icon');
echo $this->Html->css('cake.generic');
echo $this->Html->script('jquery');
echo $this->fetch('meta');
echo $this->fetch('css');
echo $this->fetch('script');
?>
</head>
<body>
<div id="container">
<div id="content">
<?php echo $this->Session->flash(); ?>
<?php echo $this->fetch('content'); ?>
</div> <!-- / content -->
<div id="footer">
<?php echo $this->Html->link(
$this->Html->image('cake.power.gif', array('alt' => $cakeDescription, 'border' => '0')),
'http://www.cakephp.org/',
array('target' => '_blank', 'escape' => false, 'id' => 'cake-powered')
);
?>
<p><?php echo $cakeVersion; ?></p>
</div> <!-- / footer -->
</div> <!-- / container -->
<?php echo $this->element('sql_dump'); ?>
<?php echo $this->Js->writeBuffer();?>
</body>
</html>
<!-- File: app/View/Animals/index.ctp -->
<h1><?php echo __('Animal Auto-Complete');?></h1>
<?php
echo $this->Form->create('Animal');
echo $this->Form->input('term', ['label' => __('Search for an animal')]);
echo $this->Form->submit(__('Search'));
echo $this->Form->end();
?>
<?php $this->append('css');?>
<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.1/themes/smoothness/jquery-ui.css" />
<?php $this->end();?>
<?php $this->append('script');?>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.1/jquery-ui.min.js"></script>
<script>
$(function () {
$('#AnimalTerm').autocomplete({
source: function (request, response) {
$.ajax({
url: $(#AnimalIndexForm).attr('action') + '.json',
data: {
'term': $('#AnimalTerm').val()
},
success: function (data) {
response(data.items);
},
minLength: 1,
select: function (event, ui) {
// If you want to do something when a user clicks an item
},
open: function() {
$(this).removeClass('ui-corner-all').addClass('ui-corner-top');
},
close: function() {
$(this).removeClass('ui-corner-top').addClass('ui-corner-all');
}
});
}
});
});
</script>
<?php $this->end();?>
<?php
/**
* Routes configuration
*
* In this file, you set up routes to your controllers and their actions.
* Routes are very important mechanism that allows you to freely connect
* different URLs to chosen controllers and their actions (functions).
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package app.Config
* @since CakePHP(tm) v 0.2.9
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
/**
* Here, we are connecting '/' (base path) to controller called 'Pages',
* its action called 'display', and we pass a param to select the view file
* to use (in this case, /app/View/Pages/home.ctp)...
*/
Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
/**
* ...and connect the rest of 'Pages' controller's URLs.
*/
Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
/**
* Load all plugin routes. See the CakePlugin documentation on
* how to customize the loading of plugin routes.
*/
CakePlugin::routes();
/**
* Load the CakePHP default routes. Only remove this if you do not want to use
* the built-in default routes.
*/
require CAKE . 'Config' . DS . 'routes.php';
/**
* Parsing
*/
Router::parseExtensions();
Router::setExtensions(array('json'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment