【发布时间】:2018-01-03 05:09:25
【问题描述】:
我的问题:
我已经创建了我的问题的完整场景。
我的 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 => { } )
}
}
})
【问题讨论】:
标签: javascript vuejs2 lodash jquery-select2