Selaa lähdekoodia

Autocomplete changes:
Removed initSelection method in favour of directly setting the initial value
Added extra (not null) check autocompleteInput onChange function (prevents error when first called)

caponica 11 vuotta sitten
vanhempi
commit
95d4667bb9
1 muutettua tiedostoa jossa 16 lisäystä ja 19 poistoa
  1. 16 19
      Resources/views/Form/form_admin_fields.html.twig

+ 16 - 19
Resources/views/Form/form_admin_fields.html.twig

@@ -307,24 +307,6 @@ file that was distributed with this source code.
                         return {results: data.items, more: data.more};
                     }
                 },
-                initSelection: function(element, callback) {
-                    //remove initial text from input
-                    $(element).val("");
-
-                    var data = [];
-                    {% if multiple -%}
-                        data = [
-                        {%- for key, label_text in value.labels -%}
-                            {id:{{ value.identifiers[key]|e('js') }}, label:'{{ label_text|e('js') }}'}
-                            {%- if not loop.last -%}, {% endif -%}
-                        {%- endfor -%}
-                        ];
-                    {%- elseif value.labels[0] is defined -%}
-                        data = {id: {{ value.identifiers[0]|e('js') }}, label:'{{ value.labels[0]|e('js') }}'};
-                    {%- endif  %}
-
-                    callback(data);
-                },
                 formatResult: function (item) {
                     return {% block sonata_type_model_autocomplete_dropdown_item_format %}'<div class="sonata-autocomplete-dropdown-item">'+item.label+'</div>'{% endblock %};// format of one dropdown item
                 },
@@ -357,7 +339,7 @@ file that was distributed with this source code.
                 }
 
                 // remove input
-                if (undefined !== e.removed) {
+                if (undefined !== e.removed && null !== e.removed) {
                     var removedItems = e.removed;
 
                     if(!$.isArray(removedItems)) {
@@ -372,6 +354,21 @@ file that was distributed with this source code.
                 }
             });
 
+            // Initialise the autocomplete
+            var data = [];
+            {% if multiple -%}
+                data = [
+                {%- for key, label_text in value.labels -%}
+                    {id:{{ value.identifiers[key]|e('js') }}, label:'{{ label_text|e('js') }}'}
+                    {%- if not loop.last -%}, {% endif -%}
+                {%- endfor -%}
+                ];
+            {%- elseif value.labels[0] is defined -%}
+                data = {id: {{ value.identifiers[0]|e('js') }}, label:'{{ value.labels[0]|e('js') }}'};
+            {%- endif  %}
+            if (undefined==data.length || 0<data.length) { // Leave placeholder if no data set
+                autocompleteInput.select2('data', data);
+            }
         })(jQuery);
     </script>
 {% endspaceless %}