简体   繁体   English

Select2是否允许将“text”键的名称更改为其他内容?

[英]Does Select2 allow for changing name of “text” key to something else?

I have the following Select2 configuration. 我有以下Select2配置。

$scope.select2Options = {
    simple_tags: false,
    placeholder : "Search for a language",
    multiple : true,
    contentType: "application/json; charset=utf-8",
    minimumInputLength : 3,
    ajax : {
        url : "/bignibou/utils/findLanguagesByLanguageStartingWith.json",
        dataType : 'json',
        data : function(term) {
            return  {
                language : term
            };
        },
        results : function(data, page) {
            return {
                results :
                    data.map(function(item) {
                        return {
                            id : item.id,
                            text : item.description
                        };
                    }
            )};
        }
    }
};

This allows me to populate the select2 control properly. 这允许我正确填充select2控件。

However, an issue occurs when I use Ajax in order to post the whole form containing the tags (amongst other): the json array sent to the server contains objects with two properties named id and text whereas the server would require id and description . 但是,当我使用Ajax发布包含标记的整个表单(以及其他)时会出现问题:发送到服务器的json数组包含具有两个名为idtext属性的对象,而服务器需要iddescription

see snippet from my json: 从我的json看到片段:

"languages":[{"id":46,"text":"Français"},{"id":1,"text":"Anglais"}]

Does select2 allow for changing the name of the text to something else? select2是否允许将text名称更改为其他名称?

Changing my js to the following sorted the issue: 将我的js更改为以下排序问题:

function format(item) { return item.description; };

$scope.select2Options = {   
    simple_tags: false,
    placeholder : "Search for a language",
    multiple : true,
    contentType: "application/json; charset=utf-8",
    minimumInputLength : 3,
    data:{ text: "description" },
    formatSelection: format,
    formatResult: format,
    ajax : {
        url : "/bignibou/utils/findLanguagesByLanguageStartingWith.json",
        dataType : 'json',
        data : function(term) {
            return  {
                language : term
            };
        },
        results : function(data, page) {
            return {
                results :
                    data.map(function(item) {
                        return {
                            id : item.id,
                            description : item.description
                        };
                    }
            )};
        }
    }
};

Notice: one has to use the Select2 top level attribute data . 注意:必须使用Select2顶级属性data

Here's the bare minium of the configuration neccesary to use a custom id and text properties on ui-select2 这是在ui-select2上使用自定义id和文本属性所需配置的最低要求

$scope.clients: {
  data: [{ ClientId: 1, ClientName: "ClientA" }, { ClientId: 2, ClientName: "ClientB" }],
  id: 'ClientId',
  formatSelection: function (item) { return item.ClientName; },
  formatResult: function (item) { return item.ClientName; }
}

Select2 requires that the text that should be displayed for an option is stored in the text property. Select2要求为选项显示的文本存储在text属性中。 You can map this property from any existing property using the following JavaScript: 您可以使用以下JavaScript从任何现有属性映射此属性:

var data = $.map(yourArrayData, function (obj) {
  obj.text = obj.text || obj.name; // replace name with the property used for the text
  obj.id = obj.id || obj.pk; // replace pk with your identifier
  return obj;
});

Documentation 文档

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

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