Skip to content

Commit 09c616d

Browse files
author
Aleš
authored
Merge pull request #23 from aleswita/master
Bug fix of passing array parameter to signal link
2 parents 17dced8 + 6ef7033 commit 09c616d

19 files changed

Lines changed: 1609 additions & 1560 deletions

client-side/dependentSelectBox.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,35 @@
3333
var signalLink = element.data(dsb.settings.dataLinkName);
3434
var parents = element.data(dsb.settings.dataParentsName);
3535

36-
if (signalLink == undefined) {
36+
if (signalLink === undefined) {
3737
return false;
3838
}
3939

4040
$.each(parents, function (name, id) {
4141
var parentElement = $('#' + id);
42+
4243
if (parentElement.length > 0) {
4344
var val;
45+
4446
if (parentElement.prop('type') === 'checkbox') {
4547
val = parentElement.prop('checked') ? 1 : 0;
48+
4649
} else {
4750
val = $(parentElement).val();
4851
if (!val) {
4952
return;
5053
}
5154
}
52-
signalLink = signalLink + '&' + name + '=' + val;
55+
56+
57+
if (val instanceof Array) {
58+
$.each(val, function (key, value) {
59+
signalLink += '&' + name + '[]=' + value;
60+
});
61+
62+
} else {
63+
signalLink += '&' + name + '=' + val;
64+
}
5365
}
5466
});
5567

Lines changed: 124 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -1,123 +1,124 @@
1-
<?php
2-
3-
/**
4-
* This file is part of the NasExt extensions of Nette Framework
5-
*
6-
* Copyright (c) 2013 Dusan Hudak (http://dusan-hudak.com)
7-
*
8-
* For the full copyright and license information, please view
9-
* the file license.md that was distributed with this source code.
10-
*/
11-
12-
namespace NasExt\Forms\Controls;
13-
14-
use NasExt;
15-
use Nette;
16-
17-
18-
/**
19-
* @author Jáchym Toušek
20-
* @author Dusan Hudak
21-
* @author Ales Wita
22-
* @license MIT
23-
*/
24-
class DependentMultiSelectBox extends Nette\Forms\Controls\MultiSelectBox implements Nette\Application\UI\ISignalReceiver
25-
{
26-
use NasExt\Forms\DependentTrait;
27-
28-
/** @var string */
29-
const SIGNAL_NAME = DependentSelectBox::SIGNAL_NAME;
30-
31-
32-
/**
33-
* @param string
34-
* @param array[Nette\Forms\IControl]
35-
*/
36-
public function __construct($label, array $parents)
37-
{
38-
$this->parents = $parents;
39-
parent::__construct($label);
40-
}
41-
42-
43-
/**
44-
* @throws
45-
* @param bool
46-
* @return self
47-
*/
48-
public function setDisabled($value = true)
49-
{
50-
if (is_array($value)) {
51-
throw new Nette\InvalidArgumentException('NasExt\\Forms\\Controls\\DependentMultiSelectBox not supported disabled items!');
52-
}
53-
return parent::setDisabled($value);
54-
}
55-
56-
57-
/**
58-
* @param string
59-
* @return void
60-
*/
61-
public function signalReceived($signal)
62-
{
63-
$presenter = $this->lookup('Nette\\Application\\UI\\Presenter');
64-
65-
if ($presenter->isAjax() && $signal === self::SIGNAL_NAME && !$this->isDisabled()) {
66-
$parentsNames = [];
67-
foreach ($this->parents as $parent) {
68-
$value = $presenter->getParameter($this->getNormalizeName($parent));
69-
$parent->setValue($value);
70-
71-
$parentsNames[$parent->getName()] = $parent->getValue();
72-
}
73-
74-
$data = $this->getDependentData([$parentsNames]);
75-
$presenter->payload->dependentselectbox = [
76-
'id' => $this->getHtmlId(),
77-
'items' => $data->getPreparedItems(!is_array($this->disabled) ?: $this->disabled),
78-
'value' => $data->getValue(),
79-
'prompt' => false,
80-
'disabledWhenEmpty' => $this->disabledWhenEmpty,
81-
];
82-
$presenter->sendPayload();
83-
}
84-
}
85-
86-
87-
/**
88-
* @return void
89-
*/
90-
private function tryLoadItems()
91-
{
92-
if ($this->parents === array_filter($this->parents, function ($p) {return !$p->hasErrors();})) {
93-
$parentsValues = [];
94-
foreach ($this->parents as $parent) {
95-
$parentsValues[$parent->getName()] = $parent->getValue();
96-
}
97-
98-
$data = $this->getDependentData([$parentsValues]);
99-
$items = $data->getItems();
100-
101-
102-
if ($this->getForm()->isSubmitted()) {
103-
$this->setValue($this->value);
104-
105-
} elseif ($this->tempValue !== null) {
106-
$this->setValue($this->tempValue);
107-
108-
} else {
109-
$this->setValue($data->getValue());
110-
}
111-
112-
113-
$this->loadHttpData();
114-
$this->setItems($items);
115-
116-
if (count($items) === 0) {
117-
if ($this->disabledWhenEmpty === true && !$this->isDisabled()) {
118-
$this->setDisabled();
119-
}
120-
}
121-
}
122-
}
123-
}
1+
<?php
2+
3+
/**
4+
* This file is part of the NasExt extensions of Nette Framework
5+
*
6+
* Copyright (c) 2013 Dusan Hudak (http://dusan-hudak.com)
7+
*
8+
* For the full copyright and license information, please view
9+
* the file license.md that was distributed with this source code.
10+
*/
11+
12+
namespace NasExt\Forms\Controls;
13+
14+
use NasExt;
15+
use Nette;
16+
17+
18+
/**
19+
* @author Jáchym Toušek
20+
* @author Dusan Hudak
21+
* @author Ales Wita
22+
* @license MIT
23+
*/
24+
class DependentMultiSelectBox extends Nette\Forms\Controls\MultiSelectBox implements Nette\Application\UI\ISignalReceiver
25+
{
26+
use NasExt\Forms\DependentTrait;
27+
28+
/** @var string */
29+
const SIGNAL_NAME = DependentSelectBox::SIGNAL_NAME;
30+
31+
32+
/**
33+
* @param string $label
34+
* @param array<int, Nette\Forms\IControl> $parents
35+
*/
36+
public function __construct($label, array $parents)
37+
{
38+
$this->parents = $parents;
39+
parent::__construct($label);
40+
}
41+
42+
43+
/**
44+
* @throws $value
45+
* @param bool
46+
* @return self
47+
*/
48+
public function setDisabled($value = true)
49+
{
50+
if (is_array($value)) {
51+
throw new Nette\InvalidArgumentException('NasExt\\Forms\\Controls\\DependentMultiSelectBox not supported disabled items!');
52+
}
53+
54+
return parent::setDisabled($value);
55+
}
56+
57+
58+
/**
59+
* @param string $signal
60+
* @return void
61+
*/
62+
public function signalReceived($signal)
63+
{
64+
$presenter = $this->lookup('Nette\\Application\\UI\\Presenter');
65+
66+
if ($presenter->isAjax() && $signal === self::SIGNAL_NAME && !$this->isDisabled()) {
67+
$parentsNames = [];
68+
foreach ($this->parents as $parent) {bdump($presenter->getParameters());
69+
$value = $presenter->getParameter($this->getNormalizeName($parent));
70+
$parent->setValue($value);
71+
72+
$parentsNames[$parent->getName()] = $parent->getValue();
73+
}
74+
75+
$data = $this->getDependentData([$parentsNames]);
76+
$presenter->payload->dependentselectbox = [
77+
'id' => $this->getHtmlId(),
78+
'items' => $data->getPreparedItems(!is_array($this->disabled) ?: $this->disabled),
79+
'value' => $data->getValue(),
80+
'prompt' => false,
81+
'disabledWhenEmpty' => $this->disabledWhenEmpty,
82+
];
83+
84+
$presenter->sendPayload();
85+
}
86+
}
87+
88+
89+
/**
90+
* @return void
91+
*/
92+
private function tryLoadItems()
93+
{
94+
if ($this->parents === array_filter($this->parents, function ($p) {return !$p->hasErrors();})) {
95+
$parentsValues = [];
96+
foreach ($this->parents as $parent) {
97+
$parentsValues[$parent->getName()] = $parent->getValue();
98+
}
99+
100+
$data = $this->getDependentData([$parentsValues]);
101+
$items = $data->getItems();
102+
103+
if ($this->getForm()->isSubmitted()) {
104+
$this->setValue($this->value);
105+
106+
} elseif ($this->tempValue !== null) {
107+
$this->setValue($this->tempValue);
108+
109+
} else {
110+
$this->setValue($data->getValue());
111+
}
112+
113+
114+
$this->loadHttpData();
115+
$this->setItems($items);
116+
117+
if (count($items) === 0) {
118+
if ($this->disabledWhenEmpty === true && !$this->isDisabled()) {
119+
$this->setDisabled();
120+
}
121+
}
122+
}
123+
}
124+
}

src/Controls/DependentSelectBox.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ class DependentSelectBox extends Nette\Forms\Controls\SelectBox implements Nette
3030

3131

3232
/**
33-
* @param string
34-
* @param array[Nette\Forms\IControl]
33+
* @param string $label
34+
* @param array<int, Nette\Forms\IControl> $parents
3535
*/
3636
public function __construct($label, array $parents)
3737
{
@@ -41,7 +41,7 @@ public function __construct($label, array $parents)
4141

4242

4343
/**
44-
* @param string
44+
* @param string $signal
4545
* @return void
4646
*/
4747
public function signalReceived($signal)

0 commit comments

Comments
 (0)