0

I have this contact form which also relates to a few other tables with a HABTM relationship. They are displayed in the form either as a list or as checkboxes. What I want is to be able to search through them while still in the form to select those that are applicable to the contact I am adding with the form. I'm not sure how to do this- was thinking maybe Javascript?

An example of what I am talking about is like Facebook when you're searching through your friends list.

Below are the HABTM relationships for the Contact Model:

    public $hasAndBelongsToMany = array(
    'Company' => array(
        'className' => 'Company',
        'joinTable' => 'companies_contacts',
        'foreignKey' => 'contact_id',
        'associationForeignKey' => 'company_id',
        'unique' => 'keepExisting',
        'dependent' => true,
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'finderQuery' => '',
        'deleteQuery' => '',
        'insertQuery' => ''
    ),
    'Event' => array(
        'className' => 'Event',
        'joinTable' => 'contacts_events',
        'foreignKey' => 'contact_id',
        'associationForeignKey' => 'event_id',
        'unique' => 'keepExisting',
        'dependent' => true,
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'finderQuery' => '',
        'deleteQuery' => '',
        'insertQuery' => ''
    ),
    'Screenoccupation' => array(
        'className' => 'Screenoccupation',
        'joinTable' => 'contacts_screenoccupations',
        'foreignKey' => 'contact_id',
        'associationForeignKey' => 'screenoccupation_id',
        'unique' => 'keepExisting',
        'dependent' => true,
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'finderQuery' => '',
        'deleteQuery' => '',
        'insertQuery' => ''
    ),
    'Tapearchive' => array(
        'className' => 'Tapearchive',
        'joinTable' => 'contacts_tapearchives',
        'foreignKey' => 'contact_id',
        'associationForeignKey' => 'tapearchive_id',
        'unique' => 'keepExisting',
        'dependent' => true,
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'finderQuery' => '',
        'deleteQuery' => '',
        'insertQuery' => ''
    ),
    'Relation' => array(
        'className' => 'Contact',
        'joinTable' => 'contacts_contacts',
        'foreignKey' => 'contact_id',
        'associationForeignKey' => 'related_id',
        'unique' => true,
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'finderQuery' => '',
        'deleteQuery' => '',
        'insertQuery' => ''
    )
);

Not sure what other codes I need to show you guys, so please do let me know what codes are needed and I will add them to this question.

Thanks!

jase89
  • 75
  • 2
  • 7

1 Answers1

0

You could use a jquery plugin to do the job for you.

It seems that this one corresponds to your criteria. Chosen is nice too.

Anyway, do a google search to see all the jquery plugin available, and see if one fits your need.

L. Sanna
  • 6,482
  • 7
  • 33
  • 47
  • Thanks for that. The first one seems to suit my needs but I am having some trouble getting it to work. Can you please help me? This is the HABTM selection that I have and I want it to use that jquery plugin. Form->input('Event', array('type'=>'select','multiple'=>'select')); ?> Any ideas? – jase89 Aug 05 '12 at 10:37
  • Simply create a standart multiselect in php, then call the javascript which will transform it. – L. Sanna Aug 05 '12 at 10:57
  • Sorry ALS, Im not sure how to do that... This code: Form->input('Event', array('type'=>'select','multiple'=>'select')); ?> automatically creates a multiselect. Not sure how to do it otherwise... – jase89 Aug 05 '12 at 11:15
  • Yes, that's what have to do. Then simply put $("select").multiselect().multiselectfilter(); in – L. Sanna Aug 05 '12 at 11:22
  • Form->input('Event', array('type'=>'select','multiple'=>'select','class'=>'select')); ?> – L. Sanna Aug 05 '12 at 11:24
  • You'll have to call the javascript library for it to work of course. – L. Sanna Aug 05 '12 at 11:25
  • It is working now BUT I dont know why it is appearing for every single select box. Eg. echo $this->Form->input('Contact.title', array('type'=>'select','options' => $title, 'default' => 'Mr.')); – jase89 Aug 05 '12 at 11:47
  • ah. $("select").multiselect().multiselectfilter(); calls all select box. Put $(".select").multiselect().multiselectfilter(); with a dot, it will work only on the input with the select class. – L. Sanna Aug 05 '12 at 11:50
  • Great!! It's working now! But it automatically selects an option by default. Any way to remove that default selection? I cant find anyting about it from the website. Also, the check all and uncheck all box is somehow missing for me. How do I add them to it? Cheers – jase89 Aug 05 '12 at 12:03
  • The default comes from cake: you wrote: echo $this->Form->input('Contact.title', array('type'=>'select','options' => $title, 'default' => 'Mr.')); just change default as you want. – L. Sanna Aug 05 '12 at 12:07
  • This is the one which has a default value: echo $this->Form->input('Event',array('type'=>'select','multiple'=>'select','class'=>'multi')); – jase89 Aug 05 '12 at 12:13
  • And dont worry about the check and uncheck box, I've just fixed it. Thanks – jase89 Aug 05 '12 at 12:13
  • That's cakephp doing the default. Go read the docs to get exactly what you want. – L. Sanna Aug 05 '12 at 12:19
  • Ah I see. Thank you very much for your help! You've been great!! And sorry that I've been asking so many questions. I'm quite new to Cake and Jquery. – jase89 Aug 05 '12 at 12:33