简体   繁体   English

Select2 initSelection PreLoaded数据未显示

[英]Select2 initSelection PreLoaded Data doesn't show

I'm using select2 to loading remote data into a select, the input tag has a value attribute preloaded that points to a preselected option, when I load the page, it show the select with the Clear option (x) at the right but the data doesn't show. 我正在使用select2将远程数据加载到一个select中,输入标签有一个预加载的value属性,指向一个预先选择的选项,当我加载页面时,它会在右边显示带有Clear选项(x)的select但是数据没有显示。

This is my code: 这是我的代码:

    function FormatResult(Consig) {
    return Consig.NomCon;
}

function FormatSelection(Consig) {
    $('#strConNom').val(Consig.NomCon);
    return Consig.NomCon;
}

$("#strCon").select2({
placeholder: "Search",
minimumInputLength: 5,
allowClear: true,
ajax: {
    url: "LoadData.asp",
    dataType: 'jsonp',
    data: function (term, page) {
        return {
            q: term,
            CodCas: $('#strCas').val(),
        };
    },
    results: function (data, page) {
        return {results: data.ConsigNom};
    }
},
initSelection: function(element, callback) {
    var id=$(element).val();
    if (id!=="") {
        $.ajax("LoadData.asp", {
        dataType: 'jsonp',
            data: {
                CodCon: id,
                CodCas: $('#strCas').val(),
            },

        }).done(function(data) { 
        callback(data);
        });
    }
},
formatResult: FormatResult,
formatSelection: FormatSelection,
dropdownCssClass: "bigdrop",
escapeMarkup: function (m) { return m; }
});

How can I solved this? 我该怎么解决这个问题?

Make sure that, in your initSelection function, your LoadData.asp passes a single object and NOT an array containing a single object. 确保在initSelection函数中,LoadData.asp传递单个对象而不是包含单个对象的数组。

Wrong: 错误:

[{"id": 5, "text":"myValue"}]

Correct: 正确:

{"id": 5, "text":"myValue"}

Here's your function modified. 这是你的功能修改。

initSelection: function(element, callback) {
    var id=$(element).val();
    if (id!=="") {
        $.ajax("LoadData.asp", {
        dataType: 'jsonp',
            data: {
                CodCon: id,
                CodCas: $('#strCas').val(),
            },

        }).done(function(data) { 
            callback(data[0]);
        });
    }
},

I had a similar problem and I solved it by including a value attribute in the html of the input element and of course appropriate formatSelection and initSelection. 我遇到了类似的问题,我通过在input元素的html中包含一个value属性来解决它,当然还有适当的formatSelection和initSelection。

The json passed into the select2 has two fields id and text. 传入select2的json有两个字段id和text。

Here's my js code: 这是我的js代码:

$( "#contributorNameSearchId" ).select2({
        placeholder: "Select a contributor",
        containerCssClass: "dropDown",
        minimumInputLength: 3,
        ajax: { 
            url: "getList?t=contributor",
            dataType: 'json',
            data: function (term, page) {
                return {
                    term: term, // search term
                    page_limit: 10,
                };
            },
            results: function (data, page) { 
                return {results: data};
            }
        },
        allowClear: true,
         formatSelection: function(data) { 
            return data.text; 
        },
        initSelection : function (element, callback) {
            var obj= {id:1,text:'whatever value'};
            callback(obj);
        } 
    }); 

and the input element itself: 和输入元素本身:

<input value="0000" name="contributorNameSearch" id="contributorNameSearchId"/>

Notice the dummy value of the value attribute. 注意value属性的虚拟值。


There's also a chance that your 还有机会你的

 callback(data); 

needs a data.something instead (although your FormatSelection seems to be ok). 需要一个data.something(虽然你的FormatSelection似乎没问题)。 Check the data that are returned from your LoadData.asp. 检查从LoadData.asp返回的数据。

I hope that this helps. 我希望这个对你有用。

When select2 is complete loading write following code 当select2完成加载时写下面的代码

$(".ajax_select").select2("data", {"id": 5, "text": "Test"});

it will set default selection of select2. 它将设置select2的默认选择。

对我来说,我必须从初始设置中删除占位符选项 - 可能对其他人有用

After struggling a lot, the solution is the following 在经历了很多努力之后,解决方案如下

Note, data object should have same key as your repoFormatSelection var data = {id: 1, cardname: "Abolish"}; 注意,数据对象应该与repoFormatSelection var data = {id:1,cardname:“Abolish”}具有相同的密钥;

You will see "text" in all answers, but it should be same as your json variables 您将在所有答案中看到“text”,但它应与您的json变量相同

and one more important thing, make sure you put value="0" or some dummy value in your select2 hidden input 还有一件更重要的事情,请确保在select2隐藏输入中放置value =“0”或一些虚拟值

$("#myselect").select2({
    allowClear: true,
    placeholder: "Search for a card",
    minimumInputLength: 3,
    ajax: {
            url: "search.php",
            dataType: 'json',
            quietMillis: 250,
            data: function (term, page) {
              return {
                 q: term, // search term
            };
        },
        results: function (data, page) { 
            return {results: data.items};
        },
        cache: true
    },
    initSelection: function (element, callback) {              
        var data = {id: 1, cardname: "Abolish"};
        callback(data);
    },
    formatResult: repoFormatResult,  
    formatSelection: repoFormatSelection, 
    escapeMarkup: function (m) {
        return m;
    } // we do not want to escape markup since we are displaying html in results
});

    function repoFormatResult(card) {
        var markup = '<div class="row-fluid">';

        if (card.cardimage != '')
        {
           markup += '<div class="span2" style="float:left; margin-right:10px"><a class="qtooltip" href="#" rel="' + card.cardimage + '" ><img src="' + card.cardimage + '" width="50px" /></a></div>';
        }

        markup += '<strong>' + card.cardname + '</strong> <small>' + card.editionname + '</small>'
        markup += '<div style="clear:both;"></div></div>';

        return markup;
    }

    function repoFormatSelection(card) {
        return card.cardname;
    }   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM