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 –
什麼完整的代碼是我的完整代碼。 –