|
@@ -24,13 +24,14 @@
|
|
|
import { defineComponent, onMounted, reactive, toRefs } from 'vue';
|
|
|
import { Input } from 'ant-design-vue';
|
|
|
import Sortable from 'sortablejs';
|
|
|
+ // import draggable from 'vuedraggable'
|
|
|
|
|
|
interface Data {
|
|
|
name: string;
|
|
|
value: string;
|
|
|
}
|
|
|
const props = {
|
|
|
- value: { type: Object, default: {} },
|
|
|
+ value: { type: Array, default: [] },
|
|
|
tip: { type: String, default: '' },
|
|
|
};
|
|
|
|
|
@@ -39,15 +40,22 @@
|
|
|
props,
|
|
|
emits: ['change'],
|
|
|
setup(props, { emit }) {
|
|
|
+ console.log(`props`, props.value);
|
|
|
const state = reactive({
|
|
|
list: [] as Data[],
|
|
|
});
|
|
|
|
|
|
// 初始化 sortable 实现拖动
|
|
|
function initSortable() {
|
|
|
- for (let k in props.value) {
|
|
|
- state.list.push({ name: k, value: props.value[k] });
|
|
|
- }
|
|
|
+ // for (let k in props.value) {
|
|
|
+ // state.list.push({ name: k, value: props.value[k] });
|
|
|
+ // }
|
|
|
+ state.list = [];
|
|
|
+ props.value.map((item: Object) => {
|
|
|
+ for (let k in item) {
|
|
|
+ state.list.push({ name: k, value: item[k] });
|
|
|
+ }
|
|
|
+ });
|
|
|
// state.list = props.value as object[];
|
|
|
const el = document.querySelector('.array-item') as any;
|
|
|
Sortable.create(el, {
|
|
@@ -70,19 +78,16 @@
|
|
|
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];
|
|
|
- // const list: object[] = [];
|
|
|
- // state.list.map((item, index) => {
|
|
|
- // if (index != oldIndex) {
|
|
|
- // list.push(item);
|
|
|
- // }
|
|
|
- // });
|
|
|
- // list.splice(newIndex, 0, current);
|
|
|
- // console.log(`list`, list);
|
|
|
- // state.list = list;
|
|
|
- // console.log(`list`, state.list);
|
|
|
+ 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();
|
|
|
},
|
|
|
});
|
|
|
}
|
|
@@ -90,16 +95,18 @@
|
|
|
initSortable(); // 开启拖拽功能
|
|
|
});
|
|
|
|
|
|
- function formateList() {
|
|
|
- const data = {};
|
|
|
+ function formatList() {
|
|
|
+ const data: object[] = [];
|
|
|
state.list.map((item) => {
|
|
|
- data[item.name] = item.value;
|
|
|
+ const obj = {};
|
|
|
+ obj[item.name] = item.value;
|
|
|
+ data.push(obj);
|
|
|
});
|
|
|
return data;
|
|
|
}
|
|
|
|
|
|
function onChange() {
|
|
|
- const data = formateList();
|
|
|
+ const data = formatList();
|
|
|
emit('change', data);
|
|
|
}
|
|
|
function create() {
|
|
@@ -107,7 +114,7 @@
|
|
|
}
|
|
|
function remove(i) {
|
|
|
state.list.splice(i, 1);
|
|
|
- const data = formateList();
|
|
|
+ const data = formatList();
|
|
|
emit('change', data);
|
|
|
}
|
|
|
|