Select Combo Box

Help
gerry
2011-12-04
2013-05-15
  • gerry
    gerry
    2011-12-04

    Am trying to implement combo boxes of select drop down lists. 

    When one item is selected in the first select dorp-down list,  the option values shown in the second select drop-dow list should change

    Following examples shown in this forum in various discussions,

    This is the code I have:

            $afood = array( "id" => "F", "desc" => "Fruits" );
    $afood = array( "id" => "N", "desc" => "Nuts" );
    $afood = array( "id" => "V", "desc" => "Vegetables" );
    $this->build("p4a_array_source","category")
    ->setPk( "id" )
    ->load($afood);

    $afruits = array( "id" => "FA", "desc" => "Apple" );
    $afruits = array( "id" => "FP", "desc" => "Peaches" );
    $afruits = array( "id" => "FG", "desc" => "Grapes" );
    $afruits = array( "id" => "FO", "desc" => "Oranges" );
    $this->build("p4a_array_source","fruits")
    ->setPk( "id" )
    ->load($afruits);

    $anuts = array( "id" => "NA", "desc" => "Almonds" );
    $anuts = array( "id" => "NC", "desc" => "Cashew" );
    $anuts = array( "id" => "NP", "desc" => "Peanuts" );
    $anuts = array( "id" => "NW", "desc" => "Walnut" );
    $this->build("p4a_array_source","nuts")
    ->setPk( "id" )
    ->load($anuts);

    ………………
    ……………….

    $this->foods
    ->setType("select") 
                            ->addAction("onChange")
                    ->intercept($this->foods,"onChange","getCat")
    ->setWidth(110)
            ->setSource($this->category)
       ->setSourceDescriptionField("desc");

    $this->foodcats
    ->setType("select")
                  ->setWidth(150)
        ->setSource($this->fruits)
        ->setSourceDescriptionField("desc");

    ……
    ……….

    public function getCat()
    {

              $foodtype = $this->foods->getValue();

                   if ( $foodtype == "N"  ) {

    $this->foodcats
        ->setSource($this->nuts);

                    }

                  $this->food_form->redesign();

    }

    This does not work. When an item is selected on the first drop-down, the onChange event does not seem to go to the getCat subroutine.  The option values i the second select list are left unchanged.

    Have tried putting message() statements in the subroutine and I do not see those messages popping up.

    What am I doing worng? Please help…

    gerry

     
  • croaker___
    croaker___
    2011-12-04

    If you're not seeing your message popups, maybe getCat isn't being called? When I have such trouble, I sprinkle around a few:

    /*DEBUG*/ echo "<pre>"; print_r($cols); echo "</pre>";
    

    Looking at your code…

    You might have more luck with

    $foodtype = $this->foods->get[b]New[/b]Value();
    

    rather than chaining…
    ->addAction("onChange")->intercept($this->foods,"onChange","getCat")

    …assuming "$this->foods" is of type P4A_field, I have more success with:

    $this->intercept($this->foods, "onChange", "getCat");
    

    Rather than redesigning the whole form ($this->food_form->redesign();), try redesigning just the field:

    $this->foods->redesign();
    

    Martin

     
  • gerry
    gerry
    2011-12-05

    Excellent !  It works now like a charm!

    Thanks Martin for the very helpful and detailed feedback. getCat was not being called in my code.

    What did the trick was:

    $this->intercept($this->foods, "onChange", "getCat");

    Will document this now. Hopefully some time I will be able to put up the documentation of what am learning here.

    P4A is a great application and the developers are doing a great job!

    Gerry