我正在使用基于SortableJS库的Vue.Draggable库。
在我的代码中,我有 2 个可拖动列表,但我有第三个没有与简单元素合并的列表。
第三个列表有一个对象数组,每个对象都有一个端点条目,当我放下我的元素时,我想做一个从端点获取对象数组并将其与目标列表合并的函数。
var vm = new Vue({
el: "#main",
data: {
list: [{
name: "John"
}, {
name: "Joao"
}, {
name: "Jean"
}],
list2: [{
name: "Juan"
}, {
name: "Edgard"
}, {
name: "Johnson"
}],
list3: [{
name: "First",
endpoint: 'http://api.com/first'
}, {
name: "Second",
endpoint: 'http://api.com/second'
}, {
name: "Third",
endpoint: 'http://api.com/third'
}]
},
methods: {
getAllTheFirstNames (evt) {
// i want to do something like this
axios.get(evt.elementDragged.endpoint)
.then(res => {
evt.dropList.add(res.data /* res.data is an array of names */)
})
},
add: function() {
this.list.push({
name: 'Juan'
});
},
replace: function() {
this.list = [{
name: 'Edgard'
}]
}
}
});
.normal {
background-color: grey;
}
.drag {
background-color: green;
}
.dragArea {
min-height: 10px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/sortablejs@1.7.0/Sortable.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Vue.Draggable/2.17.0/vuedraggable.min.js"></script>
<div id="main">
<h1>Vue Draggable</h1>
<div class="drag">
<h2>List 1 Draggable</h2>
<draggable v-model="list" class="dragArea" :options="{group:'people'}">
<div v-for="element in list">{{element.name}}</div>
</draggable>
<h2>List 2 Draggable</h2>
<draggable v-model="list2" class="dragArea" :options="{group:'people'}">
<div v-for="element in list2">{{element.name}}</div>
</draggable>
<h2>List 3 Draggable</h2>
<draggable v-model="list3" class="dragArea" :options="{group:'people'}" @end="getAllTheFirstNames">
<div v-for="element in list3">{{element.name}}</div>
</draggable>
</div>
</div>
这是小提琴