Posts Tagged ‘listmodel’

To load data in a select control you must use the object ADListModel. See the next little example:

<?php
$form = new ADForm();
...
$sel = $form->add(new ADSelectField('continents'));
$sel->setListModel(new ContinentsListModel());
...
echo $form->render();

class ContinentsListModel extends ADListModel
{
    function __construct()
    {
        $this->addValue('AF', 'Africa');
        $this->addValue('AM', 'America');
        $this->addValue('AS', 'Asia');
        $this->addValue('EU', 'Europa');
        $this->addValue('OC', 'Oceania');
    }
}
?>

There are some other ways to make this. I think the above code is more clear, but you can do this:

<?php
$form = new ADForm();
...
$sel = $form->add(new ADSelectField('continents'));
$list = new ADListModel();
$list->addValue('AF', 'Africa');
$list->addValue('AM', 'America');
$list->addValue('AS', 'Asia');
$list->addValue('EU', 'Europa');
$list->addValue('OC', 'Oceania');
$sel->setListModel($list);

echo $form->render();
?>

In this example, we are loading the data programmatically, but to get data from a data base and load them in the select control has the same difficult. RedFruits has a objects to access to data base. We’ll look later.

You can see that with RedFruits there are differents ways to solve the code needs. You only have to choose the near one to your programming needs.

That’s all!!