1

在我的表单的选择元素中使用 Select2 4.0,那些打算在 Ajax 模式下动态填充的元素在 IE9 中不起作用。它显示一个没有占位符的空选择。

经典模式(预填充的选择列表)中的那些正在正确显示和工作(select2 search..)。

在其他页面使用旧版本3.5.2,问题不会出现...!

这是一个已知的问题?可修复的?

谢谢

这是JS:

$(".select2Ajax").not(".select2-container").each(function(){
        var min = $(this).data('min'); // 3

        var configs = {
            "width":"100%",
            language: $locale, // 'fr'

            ajax: {

                    dataType: 'json',
                    delay: 250,
                    data: function (params) {
                        return {
                            q: params.term
                        };
                    },
                    processResults: function (data, page) {
                        return {
                            results: data
                        };
                    },
                    cache: true
                },
            escapeMarkup: function (markup) { return markup; }, 
            minimumInputLength: min

        };
        $(this).select2(configs);
    })//.each

的HTML:

<select id="contrat_kam" name="contrat[kam]" class="select2Ajax form-control" data-min="3" data-ajax--url="/My/Ajax/Source/url-returning-json-formatted-list/from-search-term">
    <option value="">Here is my intitial placeholder</option>
</select>

以及在普通客户端(Chrome)中输入 select2 字段时从服务器返回的示例:

[{"id":"75484567","text":"Bestnameever Ronald"},{"id":"12344568","text":"Nameofdude C\u00e9dric"},{"id":"01202795","text":"SecondDudesName John"},{"id":"00709297","text":"Doe John"}]

谢谢

4

1 回答 1

0

Ok I solved my problem, but it wasn't a JS/IE problem.

I am working under Symfony 2. The problem appeared after the form submit. Having a validation error, Symfony displays the form page again with the errors on the fields.

I was using Select2 in AJAX mode because the choices list was way too heavy to handle, nearly causing the browser to crash with more than 7000 options to display.. So I created the select (sf2 entity choice list) with an empty array for choices list.

BUT If you try to save your form with the new value, which was NOT in the (empty) original choice list, SF doesn't validate it, and throws a validation error.

SO you have to add a form PRE_SUBMIT event listener, to add the select with your selected value in it, to your formType. Which is complete other subject :)

The problem was that the new select that I defined in the form event listener did'nt have the correct parameters (class, data attributes..), preventing the select2 to work.

My bad!

So the subject would rather find his place in a Symfony 2 thread..

于 2015-04-08T12:33:55.550 回答