当前位置: 首页 > news >正文

vue 封装Table组件

基于element-plus UI 框架封装一个table组件

在项目目录下的components新建一个Table.vue

<template><section class="wrap"><el-tableref="table":data="tableData" v-loading="loading" style="width: 100%":stripe="stripe":border="border":row-key="(row) => { return row.id }"@selection-change="handleSelectionChange":height="tableHeight">// 复选框列 type="selection"<el-table-columnv-if="selection"type="selection"width="56"align="center":fixed="fixed"></el-table-column>// 序号列 type="index"<el-table-columnv-if="index"type="index"width="56"label="序号":fixed="fixed"></el-table-column><template v-for="(item, index) in tableProps" :key="index"><el-table-column :prop="item.prop" :label="item.label" :width="item.width" :type="item.type":show-overflow-tooltip="item.showOverflowTooltip" :align="item.align":fixed="item.fixed":sortable="item.sort":formatter="item.formatter"><template v-if="item.prop === 'operation'" #default="scope"><el-button size="small" type="success" @click="handleEdit(scope.row)">编辑</el-button><el-button size="small" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button><el-button size="small" type="warning" @click="handleAdd(scope.$index, scope.row)">新增</el-button></template></el-table-column></template></el-table></section>
</template><script setup>
import { ref, watch } from 'vue'const tableData = ref([])
const loading = ref(true)// 监听父组件传递过来的数据
watch(() => props.data, (val) => {tableData.value = valloading.value = false
})// 声明要触发的emits
const emits = defineEmits(['selectionChange','deleteItem', 'editItem', 'addItem'])// 父组件传递过来的props
const props = defineProps({data: {type: Array,default: () => [],},tableProps: {type: Array,},tableHeight: {type: String,default: '500'},selection: Boolean,index: Boolean,border: Boolean,stripe: Boolean,
})// 定义表格的列属性
const tableProps = ref([{type: 'selection',label: '',width: '55',align: 'center'},{type: 'index',label: 'No.',width: '55',align: 'center'},{prop: 'name',label: '姓名',width: '',showOverflowTooltip: true,align: 'center'},{prop: 'username',label: '用户名',width: 120,showOverflowTooltip: true,align: 'center'},{prop: 'email',label: '邮箱',width: '',showOverflowTooltip: true,align: 'center'},{prop: 'phone',label: '联系电话',width: '',showOverflowTooltip: true,align: 'center',// 单元格格式化函数,参考element-plus formatterformatter: (row) => {return `+86 ${ row.phone }`;},},{prop: 'website',label: '网址',width: '',showOverflowTooltip: true,align: 'center',},{prop: 'operation',label: '操作',align: 'center',formatter: (row,column) => {}}
])const handleAdd = (row) => {emits('addItem', row)
}const handleEdit = (rowData) => {emits('editItem', rowData)
}const handleDelete = (index, rowData) => {emits('deleteItem', index)
}const handleSelectionChange = (row) => {console.log('row data are: ', row[0].name)
}</script>

在具体的父组件中使用:

<template><section class="container"><section class="table-wrap"><Table :data="tableData" ref="table" borderstripe @selectionChange="handleSelectionChange" @editItem="handleEdit"@addItem="handleAdd"@deleteItem="handleDelete"></Table></section>// <span v-pre>{{ this will not be compiled }}</span></section>
</template><script setup>
import { ref, reactive, toRefs, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import Table from '@/components/Table.vue'onMounted(() => {queryData()
})
const table = ref(null)const data = reactive({/*  materialForm: {materailName: '',materialCode: '',snCode: '',productCode: '',productName: '',result: '',workOrder: [],productLine: [],},loading: false,lineList: [],orderList: [],*/tableData: [],
})const { materialForm, lineList, orderList, tableData, loading } = toRefs(data)const title = ref("")const addData = (param) => {param.id = Math.random()// const data = Object.assign({}, param) // 拷贝一个对象const data = { ...param }// const data = JSON.parse(JSON.stringify(param)) // 深拷贝tableData.value.push(data)
}const handleAdd = () => {editRef.value.openDialog()title.value = '新增'formData.value = {}
}
const handleDelete = (index, row) => {ElMessageBox.confirm('此操作将会删除本条数据,请确定是否继续执行?','提示',{confirmButtonText: '确定',cancelButtonText: '取消',closeOnClickModal: false,type: 'warning',}).then(() => {ElMessage({type: 'success',message: '删除成功',})tableData.value.splice(index, 1)}).catch(() => {ElMessage({type: 'info',message: '取消操作',})})
}const handleSelectionChange = () => {table.value.handleSelectionChange()
}provide('formObj', { title, formData, addData })const handleEdit = (rowData) => {title.value = '编辑'const param = {...rowData}formData.value = param// formData.value = Object.assign({}, rowData)// 避免引用传递,做一次数据拷贝// formData.value = JSON.parse(JSON.stringify(row))editRef.value.openDialog()
}const query = () => {queryData()
}const reset = (formEl) => {if (!formEl) returnformEl.resetFields()queryData()
}const queryData = () => {loading.value = true// const param = { ...materialForm.value, ...{ name: 'zhangsan', age: 20 } }fetch("https://jsonplaceholder.typicode.com/users").then((resposne) => resposne.json()).then((json) => {loading.value = falsetableData.value = json})
}</script>
http://www.lryc.cn/news/169363.html

相关文章:

  • MySQL主从复制错误
  • Redis群集
  • Spring AOP以及统一处理
  • vue2markdown转思维导图
  • docker下redis备份文件dump.rdb获取
  • 二十一、MySQL(多表)内连接、外连接、自连接实现
  • Zookeeper运维
  • uniapp 点击事件-防重复点击
  • 推进“数智+数治”,中期科技智慧公厕驱动城市公厕更新升级发展
  • 4、模板(二叉树,红黑树,STL的实现)
  • 了解JVM
  • vue2实现组织机构树
  • JS中BigInt的使用
  • 你的第1个 Unity 游戏!!!
  • 炫云云渲染3ds max效果图渲染教程
  • Java中数组array和列表list相互转换
  • 【JavaSE笔记】数据类型与变量
  • VR全景智慧文旅解决方案,助力文旅产业转型升级
  • 采用cv2和默认的人脸识别分类器实现人脸检测功能
  • C# 实现迷宫游戏
  • chales 重写/断点/映射/手机代理/其他主机代理
  • django添加数据库字段进行数据迁移
  • flink1.15.0消费kafka 报错 The coordinator is not available.
  • 2023华为杯研究生数学建模F题思路模型代码(9.22早第一时间更新)
  • [k8s] pod的创建过程
  • [网鼎杯 2020 朱雀组]phpweb call_user_func()
  • 电脑怎么取消磁盘分区?
  • Cascade-MVSNet CVPR-2020 学习笔记总结 译文 深度学习三维重建
  • 【JVM】Java类的加载机制!
  • Postman使用_接口导入导出