Saturday, August 29, 2009

Re: How may I 'trick' $ajax->observeField into firing with linked combo boxes?

You have to add an empty item to the beginning of the array before
rendering the options. And I suggest you put the "Please select a ..."
text in a label or elsewhere and just leave the 1st option of each
select list empty.

On Sat, Aug 29, 2009 at 10:08 AM,
FrederickD<manzanillo.english@gmail.com> wrote:
>
> After hours of research and trial and error, I must turn to the group
> for help with this. I have a series of three linked combo boxes for
> state, county, and city from the tutorial here:
> http://forum.phpsitesolutions.com/php-frameworks/cakephp/ajax-cakephp-dynamically-populate-html-select-dropdown-box-t29.html.
>
> They work great... mostly. I have added to the code in the add.ctp
> file to include an 'empty' element of 'Please select a [whatever]'. So
> all three combo boxes start out with the first value being 'Please
> select a [whatever]'.
>
> The user selects a state. The $ajax->observeField on the state combo
> box fires and loads the county combo box. This loses the initial
> 'Please select a [whatever] and displays the first entry in the list
> of counties for the selected state.
>
> Even if the user opens the county combo box and selects the first
> entry, the $ajax->observeField does not fire to load the next combo
> box of cities, probably because there is no actual change. Only if the
> user selects a DIFFERENT element than the first one does the event
> fire.
>
> A possible way around this is to still have the 'Please select a
> [whatever]' as the first element in the county combo box, forcing the
> user to make another selection and thus firing the next observeField
> trigger to load the cities applicable for a selected county.
>
> How do I do that? How do I force another 'Please select a [whatever]
> element into the list that is returned from the controller function to
> retrieve the counties for the selected state?
>
> Here are some code snippets:
>
> /app/controllers/examples_controller.php
>
> function getCounties() {
>        $this->set('options',
>                $this->Example->County->find('list',
>                        array(
>                                'conditions' => array(
>                                        'County.state_id' => $this->data['Example']['state_id']
>                                ),
>                                'group' => array('County.name')
>                        )
>                )
>        );
>        $this->render('/examples/ajax_dropdown');
> }
>
> function add() {
>        if (!empty($this->data)) {
>                $this->Example->create();
>                if ($this->Example->save($this->data)) {
>                $this->Session->setFlash(__('The Example has been saved', true));
>                        $this->redirect(array('action'=>'index'));
>                } else {
>                        $this->Session->setFlash(__('The Example could not be saved.
> Please, try again.', true));
>                }
>        }
>        $cities = array();
>        $counties = array();
>        $states = $this->Example->State->find('list');
>        $this->set(compact('cities', 'counties', 'states'));
> }
>
> /app/views/examples/add.ctp
>
> <?php
>        echo $form->input(
>                'state_id',
>                array(
>                        'options' => $states,
>                        'empty' => 'Please select a state'
>                ),
>                null,
>                array(
>                        'id' => 'states',
>                        'label' => 'State'
>                )
>        );
>        echo $form->input(
>                'county_id',
>                array(
>                        'options' => $counties,
>                        'empty' => 'Please select a county'
>                ),
>                null,
>                array(
>                        'id' => 'counties',
>                        'label' => 'County'
>                )
>        );
>        $options = array('url' => 'getcounties', 'update' =>
> 'ExampleCountyId');
>        echo $ajax->observeField('ExampleStateId', $options);
>
> Thank you in advance for your kind assistance.
> >
>

--~--~---------~--~----~------------~-------~--~----~
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: