atk4agiletoolkit

DropDown contents depending on multiple other DropDown values


I'm again. I've seen many threads which asking, how to create Dropdowns with content depending of a other Dropdown value. These logic works also for me. But now I have the problem, that the content of a Dropdown depends of the selection/value of 2 other Dropdowns. The code for depending on one Dropdown would looks like this:

$form = $crud->form;

$dd1 = $form->addField('dropdown', 'color', 'Color');
$dd1->setValueList(array('1' => 'white', '2' => 'black'));
$dd1->setEmptyText('all');

$dd2 = $form->addField('dropdown', 'size', 'Size');
$dd2->setValueList(array('1' => 'small', '2' => 'normal', '3' => 'large'));
$dd2->setModel('Size');

$dd3 = $form->getElement('packaging_id');

if ($_GET['color']) {
    $dd3->model->addCondition('color', $_GET['color']);
}

if ($_GET['size']) {
    $dd3->model->addCondition('size', $_GET['size']);
}

$dd1->js('change',
    $form->js()->atk4_form('reloadField', 'packaging_id',
        array($this->api->url(), 'color' => $dd1->js()->val())
    )
);

$dd2->js('change',
    $form->js()->atk4_form('reloadField', 'packaging_id',
        array($this->api->url(), 'size' => $dd2->js()->val())
    )
);

With these code the Dropdown dd3 will be filled with the packages, which matching the 'size' OR the 'color' option. But I need, that the Dropdown dd3 will be filled with packages, which matching both, the 'size' AND the 'color' options (for example packages that are 'small' and 'black'). I think, I need a way to achieve the values from both Dropdowns dd1 and dd2 and put it into the 'reloadField' $_GET argument. Then extract it from the $_GET and apply 2 conditions. But I haven't found a way yet. Can anyone help me? Thanks.

ByE...


Solution

  • Description

    Parameters for atk4_form are the following:

    ->atk4_form($js_method, $param1, $param2, $param3, ...)
    

    As result JS method $js_method from ui.atk4_form.js will be called like this:

    ->$js_method($param1, $param2, $param3, ...)
    

    If method you use is $js_method = 'reloadField', then you can have following parameters:

    reloadField: function(field_name, url, fn, notrigger, arg)
    

    So you can pass URL arguments in one (or both) of two ways - using url parameter or using arg parameter.


    Solution

    So one of these solutions should work for you.

    Pass URL parameters already included in URL (using PHP, wrong approach):

    ->atk4_form(
        'reloadField',
        'packaging_id',
        $this->api->url(null, array( /* base URL + additional parameters, formatted by PHP */
            'color' => $dd1->js()->val(),
            'size'  => $dd2->js()->val(),
        ))
    )
    

    or generate URL dynamically using JS (this is correct way):

    ->atk4_form(
        'reloadField',
        'packaging_id',
        $this->api->url(), /* base URL */
        null,
        null,
        array( /* additional parameters formatted by JS */
            'color' => $dd1->js()->val(),
            'size'  => $dd2->js()->val(),
       )
    )