1

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...

ATL
  • 25
  • 5
  • please add lines where you define `$form->addField('packaging_id',...)` too – DarkSide May 16 '14 at 15:27
  • isn't `... array($this->api->url(), array('color' => $dd1->js()->val(), 'size' => $dd2->js()->val())) ...` working for you? – DarkSide May 16 '14 at 15:29
  • Hi, I've added the origin of the form. It's a CRUD form, so, the `packaging_id`Field is created by the CRUD it self. I've also tried your proposal with the `array('color' => $dd1->js()->val(), 'size' => $dd2->js()->val())`, but it didn't give me the `$_GET['size']`or `$_GET['color']` values. – ATL May 18 '14 at 19:26

1 Answers1

2

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(),
   )
)
DarkSide
  • 3,670
  • 1
  • 26
  • 34
  • 1
    Thanks, DarkSide. **Your second suggestion works perfectly!** I've tried different combinations (also the first one). But this only delivers the `[color] => $('#packagelist_page_form_color').val() [size] => $('#packagelist_page_form_size').val()` as values within the `$_GET` array. The combination of PHP, Ajax and JavaScript with ATK4 is very interesting, but I'm not so comfortable with this at the moment. Especially the JavaScript part is, where I need more experience. – ATL May 19 '14 at 11:41
  • Yeah it takes some time while you'll get used to it. Also looking in atk4 source code is daily routine. By investigating atk4 sourcecode daily you'll understand how it's build more and more and life will be easier :) – DarkSide May 19 '14 at 13:06
  • Yes, this is what I do. But sometimes I'm not to see the wood for the trees. :) – ATL May 20 '14 at 14:01