当前位置:网站首页>工作经验-组件封装(拖拽排序组件)
工作经验-组件封装(拖拽排序组件)
2022-08-09 21:54:00 【杨旬】
子组件``
<div id="box" :style="boxStyle">
<div v-for="item in modelValue" :key="item.id">
<slot :msg="item"></slot>
</div>
</div>
</template>
<script setup lang="ts">
import Sortable from "sortablejs";
import {
defineEmits,
defineProps,
nextTick,
onMounted,
defineExpose,
withDefaults,
computed,
} from "vue";
//内部interface type类型定义
interface IModelValue {
id: string;
index: number;
name: string;
}
type ILayout = "x" | "y" | "X" | "Y";
interface IProps {
modelValue: IModelValue[];
layout?: ILayout;
}
// props 定义:对外选项
const props = withDefaults(defineProps<IProps>(), {
layout: "x",
});
// emit 定义:自定义事件
const emits = defineEmits(["update:modelValue"]);
//computed定义:计算属性
const boxStyle = computed(() => {
if (props.layout == "y" || props.layout == "Y") {
return "flex-direction:column;height:100%;width:fit-content";
} else {
return "flex-direction:row;width:100%;height:fit-content";
}
});
//生命周期函数
onMounted(() => {
initDragAble();
});
//自定义函数
//初始化拖拽元素
function initDragAble() {
nextTick(() => {
const el = document.querySelector("#box");
const sortable = Sortable.create(el as HTMLElement, {
// 在拖拽过程中会有动画产生
animation: 150,
easing: "cubic-bezier(1, 0, 0, 1)",
// 是否可以进行拖拽排序,为 true 时,不能进行排序,函数不会被触发,为 false 时, 可以进行排序
disabled: false,
// 为拖拽时产生的副本添加一个class
ghostClass: "newclass",
// 为选中的元素添加一个class
chosenClass: "addclass",
// 禁用html5原有的样式
forceFallback: true,
// 排序发生变化时调用的函数
onUpdate: function (event) {
const newVal = props.modelValue;
const oldItem = newVal[event.oldIndex as number];
newVal[event.oldIndex as number] = newVal[event.newIndex as number];
newVal[event.newIndex as number] = oldItem;
//将数据传给父组件
emits("update:modelValue", newVal);
},
});
});
}
defineExpose({
boxStyle,
});
</script>
<style scoped>
#box {
display: flex;
/* width: 100%; */
/* height: 100%; */
overflow: scroll;
overflow-y: none;
}
</style>
父组件
```javascript
在这里插入代码片
<template>
<div class="container">
<TableComponent v-model="data.tabList" layout="x">
<template v-slot="scope">
<div class="items">{
{ scope.msg.name }}</div>
</template>
</TableComponent>
</div>
</template>
<script setup lang="ts">
import TableComponent from "@/components/TabComponent.vue";
import { reactive, watch } from "vue";
interface ITabItem {
id: string;
index: number;
name: string;
}
interface IData {
tabList: ITabItem[];
}
const data = reactive<IData>({
tabList: [
{
id: "ie",
index: 4,
name: "e",
},
{
id: "id",
index: 3,
name: "d",
},
{
id: "ic",
index: 2,
name: "c",
},
{
id: "ib",
index: 1,
name: "b",
},
{
id: "ia",
index: 0,
name: "a",
},
],
});
watch(
() => data.tabList,
(val) => {
console.log("更新了", val);
},
{
deep: true,
}
);
defineExpose({
data,
});
</script>
<style scoped>
.container {
width: 300px;
height: 300px;
background-color: green;
}
.items {
width: 100px;
height: 100px;
background-color: red;
border: 1px solid black;
}
</style>
注意点:引入第三方库的时候需要自定义生命类型 可以安装类型声明文件, npm i @types/第三方库包名 --save-dev
边栏推荐
- Deceptive Dice
- 一本通2074:【21CSPJ普及组】分糖果(candy)
- Let's talk about what DDL, DML, DQL and DCL are in SQL statements
- 聊天尬死名场面,你遇到过吗?教你一键获取斗图表情包,晋升聊天达人
- mysql 找不到或无法加载已注册的 .Net Framework Data Provider。
- 【EF】 更新条目时出错。有关详细信息,请参见内部异常。[通俗易懂]
- openGauss数据库基本操作(超详细)
- 聊聊SQL语句中 DDL 、DML 、DQL 、DCL 分别是什么
- Kubernetes Service对象
- 18-GuliMall 压力测试与性能监控
猜你喜欢

How to Make Your Company Content GDPR Compliant

SQLi-LABS Page-2 (Adv Injections)

Domestic mobile phone manufacturers once fought for it, but now it is the first to collapse...

AI+Medical: Using Neural Networks for Medical Image Recognition and Analysis

AI Knows Everything: Building and Deploying a Sign Language Recognition System from Zero

每日一R「02」所有权与 Move 语义

从源码方面来分析Fragment管理中 Add() 方法

Xiaohei leetcode's refreshing rainy day trip, just finished eating Yufei Beef Noodles, Mala Tang and Beer: 112. Path Sum

深度剖析 Apache EventMesh 云原生分布式事件驱动架构

Js fifteen interview questions (with answers)
随机推荐
基于ABP的AppUser对象扩展
Technology Sharing | How to use the JSON Schema mode of interface automation testing?
STC8H Development (15): GPIO Drives Ci24R1 Wireless Module
random.normal() and random.truncated_normal() in TF
BulkInsert方法实现批量导入
Use convert_to_tensor in Tensorflow to specify the type of data
How to Make Your Company Content GDPR Compliant
nvm下node安装;node环境变量配置
TRUNCATE表之后空间未释放
README_Albumentations
金山云地震,震源在字节?
十步以内,用小程序快速生成App!
Quotefancy ,提供鼓舞人心语录的壁纸网站 - 倾城之链
js数组对象去重
shell学习
Xiaohei leetcode's refreshing rainy day trip, just finished eating Yufei Beef Noodles, Mala Tang and Beer: 112. Path Sum
简单问题窥见数学
SQLi-LABS Page-2 (Adv Injections)
[Microservice~Nacos] Configuration Center of Nacos
【GORM】模型关系-HasMany关系