• vue 封装Table组件


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

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

    <template>
        <section class="wrap">
            <el-table
                ref="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-column
                    v-if="selection"
                    type="selection"
                    width="56"
                    align="center"
                    :fixed="fixed"
                >
                </el-table-column>
                // 序号列 type="index"
                <el-table-column
                    v-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 = val
        loading.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 formatter
            formatter: (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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165

    在具体的父组件中使用:

    <template>
        <section class="container">
            <section class="table-wrap">
                <Table 
                    :data="tableData" 
                    ref="table" 
                    border
                    stripe 
                    @selectionChange="handleSelectionChange" 
                    @editItem="handleEdit"
                    @addItem="handleAdd"
                    @deleteItem="handleDelete">
                </Table>
            </section>
            // {{ this will not be compiled }}
        </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) return
        formEl.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 = false
            tableData.value = json
        })
    }
    
    </script>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
  • 相关阅读:
    算法金 | 读者问了个关于深度学习卷积神经网络(CNN)核心概念的问题
    栈的压入、弹出序列
    spring与spring boot升级官方指导文档
    在VirtualLab Fusion的示例中演示连接场解算器
    芯洲升压电源,降压电源,DC-DC,汇总
    Codeforces 348B 思维
    【计网】计算机网络学习笔记 目录
    ICG-PEG-NHS,吲哚菁绿-聚乙二醇-活性酯,ICG-PEG2000-NHS
    git使用
    2023最新SSM计算机毕业设计选题大全(附源码+LW)之java学生出国境学习交流管理87153
  • 原文地址:https://blog.csdn.net/qyl_0316/article/details/132812734