views:

907

answers:

1

Hi, I have an auto complete box which is populated with the list of users of the application. It is working fine with the box listing the users.

But I am able to select only one user. How to select multiple users from the list ? And also how to save the selected user's names in a variable or an array?

EDIT

I am using the built-in auto complete feature of the CakePHP framework. This is the action in the controller which generates the auto complete text box.

function autoComplete()
{
  $this->set('users',$this->User->find('all',array(
                                  'fields'=>array('User.id','User.name'),
    'conditions'=>array('User.name LIKE' => $this->data['User']['name'].'%'))));

    $this->layout = "ajax";
}

This is the auto_complete.ctp file

<ul>
    <?php foreach($users as $user): ?>
         <li><?php echo $user['User']['name']; ?></li>
    <?php endforeach; ?>
</ul>

And this is the view where I have the auto complete box:

<?php echo $form->create('User', array('url' => '/forms/share')); ?>

     <?php  echo $ajax->autoComplete('User.name', '/forms/autoComplete');?>

 <?php echo $form->end('Share');?>

In the auto complete box, I am able to select only one user name. how can I select multiple users with a comma or space separator?

+1  A: 

I don't think the AjaxHelper can produce a multi-selection auto-complete box, it's not what it's designed to do. I'm afraid you'll have to roll your own solution. Since you're already getting a nice list via Ajax that shouldn't be too much trouble.

If you want something like the Stack Overflow tag box you can probably get by by placing a few Javascript callbacks in the Helper, if you're looking for a checkbox based list you'll need to do your own.

deceze
I need something like the Stack overflow tag box. Could you guide me more on how to get that done?
Angeline Aarthi
Check out the Callback Options: http://book.cakephp.org/view/211/Callback-Options A quick suggestion would involve a 'before' and 'complete' callback. If possible, alter the request that is sent to the server to only include the last "tag", or have the server only regard the last part of the request. Save the contents of the text field before they are send in a variable and stitch them back together with the new result at the end. Something along those lines, but I don't know if it's feasible or if rolling your own script might be faster and cleaner.
deceze
ok, could you tell me how to save the selected entry from the auto suggested list in a variable, so that it could be passed to other functions?
Angeline Aarthi