Tuesday, November 23, 2010

Re: CakePHP 1.3, JSHelper and Autocomplete, Missing method?

I ran into this issue a while ago. I decided to add the autocomplete
method to the JsHelper / jQuery engine myself. Here's what you need to
do.

In jquery_engine.php, you need to create an autocomplete method. You
can copy/paste from a similar method e.g. the 'sortable' method. You
might want to make some modifications regarding the callback functions
that you pass in the options argument. This is what I had in the end:

/**
* Create an autocomplete element.
*
* @param array $options Array of options for the autocomplete.
* @return string Completed autocomplete script.
* @access public
* @see JsBaseEngineHelper::autocomplete() for options list.
*/
function autocomplete($options = array()) {
$template = '%s.autocomplete({%s});';
// don't escape the 'select' or 'search' functions
$callbacks = array('select', 'search');
return $this->_methodTemplate('autocomplete', $template, $options,
$callbacks);
}

Note that if you want to use Autocomplete's custom rendering (http://
jqueryui.com/demos/autocomplete/#custom-data), you'll have to change
the template above and allow for additional arguments that specify the
rendering function.

If you want to make the rendered JS for the autocomplete appear in the
JS buffer, you need to add your 'autocomplete' method to the list of
bufferedMethods in the JsBaseEngineHelper class (js.php).

That should be it! You can then use your autocomplete in your view
like this:

$this->Js->get('#employee_autocomplete')->autocomplete(
array( 'source' => $this->Html-
>url(array('controller'=>'employees',
'action'=>'get_employee_names')),
'minLength' => 3,
'search' => 'function (event, ui) { ... }',
'select' => 'function (event, ui) { ... }'));

One last hint - FireBug (or similar network debugging tool) is your
friend when trying to figure out why your AJAX posts don't return any
results. You can check the "Response" tab for Cake/PHP generated
errors.

Hope this helps!

On Nov 19, 7:06 am, nurvzy <nur...@gmail.com> wrote:
> That cookbook link you reference is for the Ajax Helper, not the
> JsHelper.  To my knowledge autocomplete is not supported out of the
> box using the JsHelper.  But it's simple enough to do on your own.
>
> http://docs.jquery.com/UI/Autocomplete
>
> Nick
>
> On Nov 18, 1:09 am, Louie Miranda <lmira...@gmail.com> wrote:
>
>
>
>
>
>
>
> > I am trying to migrate my autocomplete to JSHelper using JQuery (As the
> > default). And I think there are no documentation or examples yet that has
> > been publicized as of this writing.
>
> > Anyway, I did what the manual has told...
>
> > *Controller:*
> > var $helpers = array('Html', 'Form', 'Time', 'Js' => array('Jquery'));
>
> > *Default.ctp*
> > echo $html->script('http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js');
> > $this->Js->JqueryEngine->jQueryObject = '$j';
> > print $this->Html->scriptBlock('var $j = jQuery.noConflict();',
> >     array('inline' => false)); //Tell jQuery to go into noconflict mode
>
> > *Now, from autocomplete (under view).*
>
> > *The old code:*
> > <?php echo $ajax->autoComplete('q', '/publications/autocompletetitle',
> > array('minChars' => 4, 'class' => 'search')); ?>
>
> > *Was switched to the following:*
> > <?php echo $js->autoComplete('q', '/publications/autocompletetitle',
> > array('minChars' => 4, 'class' => 'search')); ?>
>
> > And the usual, it did not worked! :(. I turned-on debug and found this error
> > message.
> > *JsHelper:: Missing Method autoComplete is undefined
> > [CORE/cake/libs/view/helpers/js.php, line 154]*
>
> > So, I searched the API for JSHelper.http://api13.cakephp.org/view_source/js-helper/
>
> > And I did not find the code. The odd thing was actually following the
> > example of the cakephp book for 1.3 for autocomplete.http://book.cakephp.org/view/1370/autoComplete, and it turned out wrong.
>
> > Any ideas, how can I make autocomplete work on the new JSHelper? But, if the
> > method does not exist, then it won't make any sense. I think I am missing
> > something here? :(
>
> > Please help.
> > --
> > Louie Miranda
> >  - Email: lmira...@gmail.com
> >  - Web:http://www.louiemiranda.com

Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.

You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en

No comments: