I want to show you a new best practice example: Cascading FormSelect
Sometimes you want, that the choice of the first selectbox adapt the possible options of the second selectbox.
After playing around a little bit I decided to implement
https://github.com/geekonjava/FilterSelectI created a new Formselect class (see
https://github.com/XoopsModules25x/wgsimpleacc/blob/master/class/Form/FormSelectCascading.php )
The call is similar to call of XoopsFormSelect:
le="color: #000000"><?php // Get Theme Form \xoops_load('XoopsFormLoader'); $form = new \XoopsThemeForm('My form for testing cascading select', 'formTest', $_SERVER['REQUEST_URI'], 'post', true); $form->setExtra('enctype="multipart/form-data"'); $myExampleTray1 = new XoopsFormElementTray('Example Tray 1'); $mySelect1 = new XoopsModules\Wgsimpleacc\Form\FormSelectCascading('Caption Select 1', 'select1', '2', 15); $mySelect1->setType(1); $arrSelect1 = [ ['id' => '1', 'text'=>'Sourceelement 1', 'rel'=> '0', 'init'=> '0'], ['id' => '2', 'text'=>'Sourceelement 2', 'rel'=> '0', 'init'=> '0'], ['id' => '3', 'text'=>'Sourceelement 3', 'rel'=> '0', 'init'=> '0'], ]; $mySelect1->setCustomOptions($arrSelect1); $myExampleTray1->addElement($mySelect1); $mySelect2 = new XoopsModules\Wgsimpleacc\Form\FormSelectCascading('Caption Select 2', 'select2', '4', 15); $mySelect2->setType(2); $arrSelect2 = [ ['id' => '1', 'text'=>'Targetelement 1, linked to Sourceelement 1', 'rel'=> '1', 'init'=> '2'], ['id' => '1', 'text'=>'Targetelement 1, linked to Sourceelement 2', 'rel'=> '2', 'init'=> '2'], ['id' => '1', 'text'=>'Targetelement 1, linked to Sourceelement 3', 'rel'=> '3', 'init'=> '2'], ['id' => '2', 'text'=>'Targetelement 2, linked to Sourceelement 1', 'rel'=> '1', 'init'=> '2'], ['id' => '3', 'text'=>'Targetelement 3, linked to Sourceelement 1', 'rel'=> '1', 'init'=> '2'], ['id' => '3', 'text'=>'Targetelement 3, linked to Sourceelement 3', 'rel'=> '3', 'init'=> '2'], ['id' => '4', 'text'=>'Targetelement 4, linked to Sourceelement 2', 'rel'=> '2', 'init'=> '2'], ['id' => '5', 'text'=>'Targetelement 5, linked to Sourceelement 2', 'rel'=> '2', 'init'=> '2'], ]; $mySelect2->setCustomOptions($arrSelect2); $myExampleTray1->addElement($mySelect2); $form->addElement($myExampleTray1); $form->addElement(new \XoopsFormHidden('op', 'save')); $form->addElement(new \XoopsFormButtonTray('', _SUBMIT, 'submit', '', false)); $GLOBALS['xoopsTpl']->assign('form', $form->render());
if you now click on 'Sourceelement 1' in first select then you see only
- 'Targetelement 1, linked to Sourceelement 1'
- 'Targetelement 2, linked to Sourceelement 1'
- 'Targetelement 3, linked to Sourceelement 1'
in second select
have fun
Goffy