mirror of
https://github.com/wintercms/winter.git
synced 2024-06-28 05:33:29 +02:00
Fix support for custom Select2 options via the AJAX framework (#4414)
Fixes #4413. Credit to @DanHarrin
This commit is contained in:
parent
3eac21219f
commit
7f29cbe13c
@ -68,11 +68,28 @@ Use the `data-handler` attribute to source the select options from an AJAX handl
|
||||
data-minimum-input-length="2"
|
||||
data-ajax--delay="300"
|
||||
data-request-data="foo: 'bar'"
|
||||
></select>
|
||||
></select>
|
||||
```
|
||||
|
||||
The AJAX handler should return results as an array.
|
||||
|
||||
```php
|
||||
public function onGetOptions()
|
||||
{
|
||||
$results = [
|
||||
[
|
||||
'id' => 1,
|
||||
'text' => 'Foobar',
|
||||
],
|
||||
...
|
||||
];
|
||||
|
||||
return ['result' => $results];
|
||||
}
|
||||
```
|
||||
|
||||
Due to the fact that JavaScript reorders numeric keys when interpreting the JSON data received by the AJAX handler, we suggest the method above for defining `results`. Support for the original `results` array format is however retained to ensure backwards compatibility.
|
||||
|
||||
```php
|
||||
public function onGetOptions()
|
||||
{
|
||||
|
@ -86,7 +86,25 @@
|
||||
|
||||
return $request
|
||||
},
|
||||
processResults: function (data, params) {
|
||||
var results = data.result;
|
||||
var options = [];
|
||||
|
||||
for (var i in results) {
|
||||
if (results.hasOwnProperty(i)) {
|
||||
var isObject = i != null && i.constructor.name === 'Object';
|
||||
|
||||
options.push({
|
||||
id: isObject ? results[i].id : i,
|
||||
text: isObject ? results[i].text : results[i],
|
||||
});
|
||||
};
|
||||
};
|
||||
|
||||
return {
|
||||
results: options,
|
||||
};
|
||||
},
|
||||
dataType: 'json'
|
||||
}
|
||||
}
|
||||
|
2
modules/system/assets/ui/storm-min.js
vendored
2
modules/system/assets/ui/storm-min.js
vendored
@ -3493,7 +3493,7 @@ if($element.hasClass('select-hide-selected')){extraOptions.dropdownCssClass+=' s
|
||||
var source=$element.data('handler');if(source){extraOptions.ajax={transport:function(params,success,failure){var $request=$element.request(source,{data:params.data})
|
||||
$request.done(success)
|
||||
$request.fail(failure)
|
||||
return $request},dataType:'json'}}
|
||||
return $request},processResults:function(data,params){var results=data.result;var options=[];for(var i in results){if(results.hasOwnProperty(i)){var isObject=i!=null&&i.constructor.name==='Object';options.push({id:isObject?results[i].id:i,text:isObject?results[i].text:results[i],});};};return{results:options,};},dataType:'json'}}
|
||||
var separators=$element.data('token-separators')
|
||||
if(separators){extraOptions.tags=true
|
||||
extraOptions.tokenSeparators=separators.split('|')
|
||||
|
Loading…
x
Reference in New Issue
Block a user