Ver Fonte

Merge pull request #2326 from caponica/autocomplete_refactor_init

Refactored how the autocomplete initialises its values
Andrej Hudec há 11 anos atrás
pai
commit
36cb9d20b8
1 ficheiros alterados com 16 adições e 19 exclusões
  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 %}