0
我的问题:如何使用select2加载对象数据数组
我已经创建了我的问题的完整场景。
我的HTML:
<select data-placeholder = "Sending" id = "sender" data-allow-clear = true >
<option ></option >
</select >
<select data-placeholder = "Receiving" id = "receiving" data-allow-clear = true >
<option ></option >
</select >
Corridor url
回报这个下面集合(我使用laravel作为后端):
[ {
"id" : 1, "source_country_id": 1, "destination_country_id": 2,
"source_country" : { "id": 1, "name": "United Kingdom", "flag_icon": "flag-icon-gb" },
"destination_country": { "id": 2, "name": "Pakistan", "flag_icon": "flag-icon-pk" }
}, {
"id" : 2, "source_country_id": 1, "destination_country_id": 3,
"source_country" : { "id": 1, "name": "United Kingdom", "flag_icon": "flag-icon-gb" },
"destination_country": { "id": 3, "name": "India", "flag_icon": "flag-icon-in" }
}, {
"id" : 7, "source_country_id": 1, "destination_country_id": 4,
"source_country" : { "id": 1, "name": "United Kingdom", "flag_icon": "flag-icon-gb" },
"destination_country": { "id": 4, "name": "Bangladesh", "flag_icon": "flag-icon-bd" }
} ]
我Vue公司代码:在使用Axios公司,我检索数据corridors
方法(如上所示),然后我为sendigCountries
和recevingCountries
创建两个阵列。这两个数组成功填充其数据(sendingCountries
与重复数据创建)之后,我想将该数据传递给select2但select2未填充该数组。请帮我弄清楚我犯的错误。
var app = new Vue({
methods: {
corridors : function() {
axios.post ('/corridors')
.then (response => {
let sendingCountries = [];
let receivingCountries = [];
_.forEach (response.data, function (value, key) {
sendingCountries.push ({
id: value.source_country.id,
text: value.source_country.name,
flag: value.source_country.flag_icon
});
receivingCountries.push ({
id : value.destination_country.id,
text: value.destination_country.name,
flag: value.destination_country.flag_icon
});
});
$ ("#sender").select2 ({
width: '100%',
data : sendingCountries,
});
$ ("#receiver").select2 ({
width: '100%',
data : receivingCountries,
});
})
.catch (error => { })
}
}
})
'sendingCountries'和'在AJAX成功响应无线receivingCountries'创建如果我从两个数组中删除了'flag'而不是相同的情况数据没有填充... –
@WaqasMehmood请分享完整的代码与小提琴或jsbin –
什么完整的代码是我的完整代码。 –