Re: How may I 'trick' $ajax->observeField into firing with linked combo boxes?
Been a while since I did this but as CakePHP uses the id of each record as the key I think I did $array[0] => 'value'; ksort($array); Should work as you can't have a primary key of 0 so it should always sort to first, actually this would mess with an array sorted by a-z values. Ok, how about ... $this->set('variableName', Set::merge(array(0=>'emptyValue'), $listArrayFromCake)); Paul. --~--~-~--~~~---~--~~ 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+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: How may I 'trick' $ajax->observeField into firing with linked combo boxes?
I think I solved my problem referenced on this thread. The solution proposed with the array_unshift scrambled the data inside the dropdown. The names were still sorted properly, but the id number with that name got resequenced. Apparently that is what array_unshift does for you, according to the docs. So what I did, and I think it will work for me is I added this code to the .ctp file that creates the dropdown list. Mine is called ajax_dropdown.ctp because that is how it was in the tutorial I have been using. Here is the code: $v) : ?> This successfully adds an element at the beginning of the dropdown list 'Pick me!' (a reference to Donkey in Shrek...) and preserves the elements beneath it. I think I can move on to the next issue now... --~--~-~--~~~---~--~~ 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+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: How may I 'trick' $ajax->observeField into firing with linked combo boxes?
In case you miss the other thread: you need to call set() before rendering the view because the view's copy of the variable is not changed otherwise. On Sun, Aug 30, 2009 at 9:33 AM, FrederickD wrote: > > I understand. That is what I would like to do; have the first row be > 'Please select...' so that the observeField 'sees' a change in the > field. > > I am just not sure how to push an entry into element 0 of an array > that has already been loaded by a find('List'), and thereby pushing > all the other elements down by one. > > This was an interesting thread: > http://groups.google.com/group/cake-php/browse_thread/thread/a342e152400d0d88?hl=en > > The suggestion there is to use array_unshift($isps, array > ("none"=>"None of the above"));. > > I tried that and placed the array_unshift just before the render > statement, but to no avail. Is my code, and or placement, incorrect? > > Thank you! > > On Aug 30, 3:48 am, WebbedIT wrote: >> As brian states you add the blank entry to the arrays that you pass >> from the controller to the view. >> >> So the $states, $counties and $cities arrays need to include the blank >> 'Please select ...' options as their first row. > > > --~--~-~--~~~---~--~~ 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+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: How may I 'trick' $ajax->observeField into firing with linked combo boxes?
I understand. That is what I would like to do; have the first row be 'Please select...' so that the observeField 'sees' a change in the field. I am just not sure how to push an entry into element 0 of an array that has already been loaded by a find('List'), and thereby pushing all the other elements down by one. This was an interesting thread: http://groups.google.com/group/cake-php/browse_thread/thread/a342e152400d0d88?hl=en The suggestion there is to use array_unshift($isps, array ("none"=>"None of the above"));. I tried that and placed the array_unshift just before the render statement, but to no avail. Is my code, and or placement, incorrect? Thank you! On Aug 30, 3:48 am, WebbedIT wrote: > As brian states you add the blank entry to the arrays that you pass > from the controller to the view. > > So the $states, $counties and $cities arrays need to include the blank > 'Please select ...' options as their first row. --~--~-~--~~~---~--~~ 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+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: How may I 'trick' $ajax->observeField into firing with linked combo boxes?
As brian states you add the blank entry to the arrays that you pass from the controller to the view. So the $states, $counties and $cities arrays need to include the blank 'Please select ...' options as their first row. --~--~-~--~~~---~--~~ 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+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: How may I 'trick' $ajax->observeField into firing with linked combo boxes?
Thank you Brian. Yes, that is what I would like to do. What would that piece of code look like before the $this->render('/examples/ ajax_dropdown');? Is it as simple as $counties = "Please select a ..." . $counties;? I'm not sure how to insert the empty item at the beginning of the array before rendering. Would you be so kind as to demonstrate for me please? Thanks! On Aug 29, 11:14 am, brian wrote: > 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 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 > > > 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 > > > > 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+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com
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 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 > > 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+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---