select2: “text is undefined” when getting json using ajax

后端 未结 2 1581
轮回少年
轮回少年 2021-01-22 09:13

I\'m having an issue when getting json results back to select2. My json does not return a result that has a \"text\" field so need to format the result so that select2 accepts \

2条回答
  •  孤独总比滥情好
    2021-01-22 09:26

    note that select2 is always in {id,text} pair so you need to specify both

    results: function (data, page) {
                var newData = [];
                _.each(data, function (item) {
                    newData.push({
                        id: item.Id  //id part present in data 
                      , text: item.DisplayString  //string to be displayed
                    });
                });
                return { results: newData };
            }
        },
    

提交回复
热议问题