|
@@ -6,7 +6,12 @@
|
|
|
<div class="value-head">键值</div>
|
|
|
</div>
|
|
|
<div class="array-item">
|
|
|
- <div class="content-item" v-for="(item, index) in list" :key="index">
|
|
|
+ <div
|
|
|
+ class="content-item"
|
|
|
+ v-for="(item, index) in list"
|
|
|
+ :key="index"
|
|
|
+ :data-no="JSON.stringify(item)"
|
|
|
+ >
|
|
|
<div class="name-content">
|
|
|
<Input v-model:value="item.name" @change="onChange" />
|
|
|
</div>
|
|
@@ -43,8 +48,11 @@
|
|
|
console.log(`props`, props.value);
|
|
|
const state = reactive({
|
|
|
list: [] as Data[],
|
|
|
+ sortList: [] as Data[],
|
|
|
});
|
|
|
|
|
|
+ var sortCom: any = null;
|
|
|
+
|
|
|
// 初始化 sortable 实现拖动
|
|
|
function initSortable() {
|
|
|
// for (let k in props.value) {
|
|
@@ -58,7 +66,7 @@
|
|
|
});
|
|
|
// state.list = props.value as object[];
|
|
|
const el = document.querySelector('.array-item') as any;
|
|
|
- Sortable.create(el, {
|
|
|
+ sortCom = Sortable.create(el, {
|
|
|
handle: '.drag-btn',
|
|
|
sort: true, // boolean 定义是否列表单元是否可以在列表容器内进行拖拽排序
|
|
|
delay: 0, // number 定义鼠标选中列表单元可以开始拖动的延迟时间;
|
|
@@ -71,6 +79,7 @@
|
|
|
chosenClass: 'sortable-chosen', // 被选中项的css 类名
|
|
|
dragClass: 'sortable-drag', // 正在被拖拽中的css类名
|
|
|
group: { name: 'name', pull: true, put: true },
|
|
|
+ dataIdAttr: 'data-no',
|
|
|
|
|
|
// 拖拽完成
|
|
|
onUpdate: function (evt) {
|
|
@@ -78,16 +87,18 @@
|
|
|
console.log(`移动前的位置索引`, evt.oldIndex);
|
|
|
console.log(`移动后的位置索引`, evt.newIndex);
|
|
|
// // const newIndex = evt.newIndex as number;
|
|
|
- const oldIndex = evt.oldIndex as number;
|
|
|
- const newIndex = evt.newIndex as number;
|
|
|
- const current = state.list[oldIndex];
|
|
|
- state.list.splice(oldIndex, 1);
|
|
|
- state.list.splice(newIndex, 0, current);
|
|
|
- console.log(`state.list`, state.list);
|
|
|
- const data = formatList();
|
|
|
- console.log(`data`, data);
|
|
|
- emit('change', data);
|
|
|
- initSortable();
|
|
|
+ // const oldIndex = evt.oldIndex as number;
|
|
|
+ // const newIndex = evt.newIndex as number;
|
|
|
+ // const current = state.list[oldIndex];
|
|
|
+ // console.log(`state.list.length === before`, state.list.length);
|
|
|
+ // state.list.splice(oldIndex, 1);
|
|
|
+ // console.log(`state.list.length`, state.list.length);
|
|
|
+ // state.list.splice(newIndex, 0, current);
|
|
|
+ // console.log(`state.list`, state.list);
|
|
|
+ const res = sortCom.toArray();
|
|
|
+ state.sortList = res.map((item) => JSON.parse(item));
|
|
|
+ state.list = state.sortList;
|
|
|
+ console.log(`拖动后的数据`, state.sortList);
|
|
|
},
|
|
|
});
|
|
|
}
|