@@ -28,6 +28,9 @@ trait TAjaxSelect
2828 /** @var \Nette\Caching\Cache */
2929 private $ storage ;
3030
31+ /** @var array */
32+ private $ optionsRaw ;
33+
3134 public function setCallback (callable $ callback ): self
3235 {
3336 $ this ->callback = $ callback ;
@@ -86,17 +89,7 @@ public function signalReceived(string $signal): void
8689
8790 switch ($ signal ) {
8891 case self ::CALLBACK_SIGNAL_NAME :
89- $ data = $ this ->getData ($ presenter ->getParameter ('q ' ));
90- $ toJson = [];
91-
92- foreach ($ data as $ key => $ value ) {
93- $ toJson [] = [
94- 'id ' => $ key ,
95- 'text ' => $ value ,
96- ];
97- }
98-
99- $ presenter ->sendJson ($ toJson );
92+ $ presenter ->sendJson ($ this ->getData ($ presenter ->getParameter ('q ' )));
10093 break ;
10194 case self ::ONCHANGE_SIGNAL_NAME :
10295 $ this ->fireOnchange ($ presenter ->getParameter ('s ' ));
@@ -107,9 +100,9 @@ public function signalReceived(string $signal): void
107100 /**
108101 * @param string $query
109102 * @param array|int|null $default
110- * @return array
103+ * @return \Nepttune\Form\ResultObjectSet
111104 */
112- private function getData (string $ query = '' , $ default = null ): array
105+ private function getData (string $ query = '' , $ default = null ) : \ Nepttune \ Form \ ResultObjectSet
113106 {
114107 if ($ this ->callback === null ) {
115108 throw new \Nette \InvalidStateException ('Callback for " ' . $ this ->getHtmlId () . '" is not set. ' );
@@ -126,7 +119,11 @@ private function getData(string $query = '', $default = null): array
126119
127120 $ data = \call_user_func ($ this ->callback , $ query , $ default );
128121
129- if (!\is_array ($ data )) {
122+ if (\is_array ($ data )) {
123+ $ data = \Nepttune \Form \ResultObjectSet::fromArray ($ data );
124+ }
125+
126+ if (!$ data instanceof \Nepttune \Form \ResultObjectSet) {
130127 throw new \Nette \InvalidStateException ('Callback for " ' . $ this ->getHtmlId () . '" must return array. ' );
131128 }
132129
@@ -140,16 +137,19 @@ private function getData(string $query = '', $default = null): array
140137 private function initiateItems ($ value = null ): void
141138 {
142139 $ value = $ value ?? $ this ->value ;
140+ $ data = [];
143141
144142 if (\in_array ($ value , [null , '' , []], true )) {
145143 if (\count ($ this ->items ) > 0 ) {
146144 return ;
147145 }
148146
149- parent :: setItems ( $ this ->getData () );
147+ $ data = $ this ->getData ();
150148 } else {
151- parent :: setItems ( $ this ->getData ('' , $ value) );
149+ $ data = $ this ->getData ('' , $ value );
152150 }
151+
152+ parent ::setItems ($ data ->getRawData ());
153153 }
154154
155155 private function fireOnchange ($ selected = null ) : void
0 commit comments