Jeroen Baert
Jeroen Baert

Reputation: 1314

CakePHP 2.0 Livesearch

I'm trying to create this: http://www.justkez.com/cakephp-livesearch/ in CakePHP 2.0. The problem is that the AjaxHelper is not available anymore in CakePHP 2.0, and so

echo $this->Ajax->observeField('query', $options);

Does not work anymore.

Any suggestions?

Upvotes: 0

Views: 1949

Answers (2)

Jeroen Baert
Jeroen Baert

Reputation: 1314

I ended up implementing it using the JSHelper, like suggested above. I will be marking this as an answer, since it contains the actual code example on how to do it.

<h3>Search Reservations</h3>
<?php
echo $this->Html->css('screen');
// we need some javascripts for this
echo $this->Html->script('jquery');
// create the form
echo $this->Form->create(false, array('type' => 'get', 'default' => false));
echo $this->Form->input('query', array('type' => 'text','id' => 'query', 'name' => 'query', 'label' => false))?>
<div id="loading" style="display: none; ">
<?php
echo $this->Html->image('ajax_clock.gif');
?>
</div>
<?php
$this->Js->get('#query')->event('keyup', $this->Js->request( 
array('controller' => 'sales','action' => 'searchReservations', $event['Event']['id']), 
array( 
'update' => '#view', 
'async' => true,
'dataExpression' => true, 
'method' => 'post', 
'data' => $this->Js->serializeForm(array('isForm' => false, 'inline' => true))) 
));
?>

Upvotes: 1

Elte Hupkes
Elte Hupkes

Reputation: 2978

The AjaxHelper calls were all just convenience methods for JavaScript functions; most of which can be easily achieved in Javascript itself, which is the way I prefer to do it myself. I suppose that observeField would simply add an "onchange" event listener, but I've never used it so I can't be sure.

There is a replacement in Cake 2.0 (and 1.3, by the way) for the AjaxHelper though, it's called the JsHelper, and it uses various engines to adapt to the multiple JavaScript frameworks that exist. It probably doesn't have the same methods the AjaxHelper used to have, but it's much more flexible and I'm pretty sure it'll fit your needs.

Upvotes: 1

Related Questions