|
@@ -5,35 +5,87 @@
|
|
|
<div class="name-head">键名</div>
|
|
|
<div class="value-head">键值</div>
|
|
|
</div>
|
|
|
- <div class="content" v-for="(item, i) in list" :key="i">
|
|
|
- <div class="name-content">
|
|
|
- <Input />
|
|
|
+ <div class="array-item">
|
|
|
+ <div class="content-item" v-for="(item, index) in list" :key="index">
|
|
|
+ <div class="name-content">
|
|
|
+ <Input v-model:value="item.name" />
|
|
|
+ </div>
|
|
|
+ <div class="value-content">
|
|
|
+ <Input v-model:value="item.value" />
|
|
|
+ </div>
|
|
|
+ <a-button class="mr-2" preIcon="mdi:close-thick" type="danger" @click="remove(index)" />
|
|
|
+ <a-button class="mr-2 drag-btn" preIcon="ri:drag-move-2-fill" type="primary" />
|
|
|
</div>
|
|
|
- <div class="value-content">
|
|
|
- <Input :value="item.value" />
|
|
|
- </div>
|
|
|
- <a-button preIcon="mdi:close-thick" type="danger" class="mr-2" />
|
|
|
</div>
|
|
|
- <a-button @click="create" class="add-btn mr-2" type="success"> 追加 </a-button>
|
|
|
+ <a-button class="add-btn mr-2" size="small" type="success" @click="create"> 追加 </a-button>
|
|
|
</div>
|
|
|
</template>
|
|
|
<script lang="ts">
|
|
|
- import { defineComponent, reactive, toRefs } from 'vue';
|
|
|
+ import { defineComponent, onMounted, reactive, toRefs } from 'vue';
|
|
|
import { Input } from 'ant-design-vue';
|
|
|
+ import Sortable from 'sortablejs';
|
|
|
|
|
|
export default defineComponent({
|
|
|
components: { Input },
|
|
|
setup() {
|
|
|
const state = reactive({
|
|
|
- list: [{ name: '' }],
|
|
|
+ list: [
|
|
|
+ { name: '12df', value: 'hello' },
|
|
|
+ { name: 'sdfa33', value: 'dfds33' },
|
|
|
+ ] as object[],
|
|
|
+ });
|
|
|
+
|
|
|
+ // 初始化 sortable 实现拖动
|
|
|
+ function initSortable() {
|
|
|
+ console.log('===========init----dfsadf=======');
|
|
|
+ console.log(`document`, document);
|
|
|
+ const el = document.querySelector('.array-item') as any;
|
|
|
+ console.log(el);
|
|
|
+ Sortable.create(el, {
|
|
|
+ handle: '.drag-btn',
|
|
|
+ sort: true, // boolean 定义是否列表单元是否可以在列表容器内进行拖拽排序
|
|
|
+ delay: 0, // number 定义鼠标选中列表单元可以开始拖动的延迟时间;
|
|
|
+ touchStartThreshold: 0, // px, how many pixels the point should move before cancelling a delayed drag event
|
|
|
+ disabled: false, // boolean 定义是否此sortable对象是否可用,为true时sortable对象不能拖放排序等功能,为false时为可以进行排序,相当于一个开关;
|
|
|
+ animation: 150, // ms, number 单位:ms,定义排序动画的时间
|
|
|
+ easing: 'cubic-bezier(1, 0, 0, 1)', // Easing for animation. Defaults to null. See https://easings.net/ for examples.
|
|
|
+ ghostClass: 'drag-ghost', // drop placeholder的css类名
|
|
|
+ draggable: '.content-item', // 允许拖拽的项目类名
|
|
|
+ chosenClass: 'sortable-chosen', // 被选中项的css 类名
|
|
|
+ dragClass: 'sortable-drag', // 正在被拖拽中的css类名
|
|
|
+ group: { name: 'name', pull: true, put: true },
|
|
|
+
|
|
|
+ // 拖拽完成
|
|
|
+ onUpdate: function (evt) {
|
|
|
+ console.log(evt);
|
|
|
+ console.log(`移动前的位置索引`, evt.oldIndex);
|
|
|
+ console.log(`移动后的位置索引`, evt.newIndex);
|
|
|
+ const newIndex = evt.newIndex as number;
|
|
|
+ const oldIndex = evt.oldIndex as number;
|
|
|
+ const temp = state.list[oldIndex];
|
|
|
+ state.list[oldIndex] = state.list[newIndex];
|
|
|
+ state.list[newIndex] = temp;
|
|
|
+ console.log(`list`, state.list);
|
|
|
+ },
|
|
|
+ });
|
|
|
+ }
|
|
|
+ onMounted(() => {
|
|
|
+ initSortable(); // 开启拖拽功能
|
|
|
+ // console.log(`fn`, fn)
|
|
|
});
|
|
|
|
|
|
function create() {
|
|
|
+ console.log(`state.list`, state.list);
|
|
|
state.list.push({ name: '' });
|
|
|
}
|
|
|
+ function remove(i) {
|
|
|
+ console.log(`i`, i);
|
|
|
+ state.list.splice(i, 1);
|
|
|
+ }
|
|
|
|
|
|
return {
|
|
|
create,
|
|
|
+ remove,
|
|
|
...toRefs(state),
|
|
|
};
|
|
|
},
|
|
@@ -41,7 +93,7 @@
|
|
|
</script>
|
|
|
<style scoped>
|
|
|
.wrap {
|
|
|
- width: 60%;
|
|
|
+ width: 55%;
|
|
|
}
|
|
|
|
|
|
.head {
|
|
@@ -60,7 +112,7 @@
|
|
|
font-weight: bold;
|
|
|
}
|
|
|
|
|
|
- .content {
|
|
|
+ .content-item {
|
|
|
display: flex;
|
|
|
margin: 5px 0;
|
|
|
}
|
|
@@ -76,6 +128,7 @@
|
|
|
}
|
|
|
|
|
|
.add-btn {
|
|
|
+ margin-top: 2px;
|
|
|
color: #fff;
|
|
|
}
|
|
|
</style>
|