vue3表格编辑(数据回显)和删除功能实现

基于Vue3 + Element plus创建的表格

实现效果如下

<template>
    <div class="table">
      <div class="tableTop">现有数据集</div>
      <el-table
        :data="filterTableData"
        style="width: 100%"
        class-name="elTable"
      >
        <template #empty>
          <el-empty description="暂无数据" :image-size="200" />
        </template>
        <el-table-column label="模型创建日期" prop="date" />
        <el-table-column label="模型名称" prop="name" />
        <el-table-column label="镜像地址" prop="address" />
        <el-table-column align="right">
          <template #header>
            <el-input
              v-model="search"
              size="small"
              placeholder="请输入您的模型名称"
            />
          </template>
          <template #default="scope">
            <el-button size="small" @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-button
              size="small"
              type="danger"
              @click="handleDelete(scope.$index)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-dialog v-model="dialogVisible">
        <el-form :model="editingRowData">
          <el-form-item label="模型名称">
            <el-input v-model="editingRowData.name" />
          </el-form-item>
        </el-form>
        <div slot="footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleUpdate">保存</el-button>
        </div>
      </el-dialog>
    </div>
</template>

<script setup>
import {
  ElInput,
  ElTable,
  ElTableColumn,
  ElIcon,
  ElButton,
  ElEmpty,
  ElMessageBox,
  ElMessage,
  ElDialog,
  ElForm,
  ElFormItem
} from 'element-plus'

import { computed, ref, reactive } from 'vue'

const search = ref('')
const editingRowData = ref({})
const dialogVisible = ref(false) // 编辑回显时的弹窗
let editIndex = -1 // 表示没有编辑的行
const tableData = reactive([
  {
    date: '2023-05-03',
    name: 'Tom',
    address: 'https://mirrors.aliyun.com/'
  },
  {
    date: '2023-05-02',
    name: 'John',
    address: 'http://mirrors.163.com/'
  },
  {
    date: '2023-05-04',
    name: 'Morgan',
    address: 'https://mirrors.hust.edu.cn/'
  },
  {
    date: '2023-05-01',
    name: 'Jessy',
    address: 'https://npm.taobao.org/'
  }
])

const handleEdit = (index, row) => {
  editIndex = index
  editingRowData.value = { ...row }
  dialogVisible.value = true
}

const handleUpdate = () => {
  if (editIndex !== -1) {
    tableData.splice(editIndex, 1, { ...editingRowData })
    dialogVisible.value = false
    // tableData.value = editingRowData.value  直接赋值会失败,因为直接赋值只是赋值,而这里需要一个对象
    tableData[editIndex] = Object.assign({}, editingRowData.value)

    ElMessage({
      type: 'success',
      message: '修改成功'
    })
  } else {
    ElMessage({
      type: 'error',
      message: '修改失败!'
    })
  }
}

const filterTableData = computed(function () {
  return tableData.filter(function (data) {
    return (
      !search.value ||
      data.name.toLowerCase().includes(search.value.toLowerCase())
    )
  })
})

// 删除数据集
const handleDelete = index => {
  ElMessageBox.confirm('您确定要删除该数据吗?', '警告', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    tableData.splice(index, 1)
    ElMessage({
      type: 'success',
      message: '删除成功'
    })
  })
}

</script>

  • 9
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue3 中实现表格拖拽排序可以使用以下步骤: 1. 在表格中定义一个可拖拽的列头,例如使用`<th>`元素,并添加`draggable`属性以启用拖拽功能。 2. 监听`dragstart`事件并将拖拽的列的索引存储在数据中。 3. 监听`dragover`事件,并阻止默认行为以允许放置目标。 4. 监听`drop`事件,并获取当前的目标列的索引,然后通过交换数据中的列来实现拖拽排序。 5. 更新表格的渲染。 以下是一个示例代码: ```html <template> <table> <thead> <tr> <th v-for="(column, index) in columns" :key="index" :draggable="true" @dragstart="onDragStart(index)" @dragover.prevent @drop="onDrop(index)" > {{ column }} </th> </tr> </thead> <tbody> <tr v-for="(row, index) in data" :key="index"> <td v-for="(value, index) in row" :key="index">{{ value }}</td> </tr> </tbody> </table> </template> <script> import { ref } from 'vue'; export default { setup() { const columns = ref(['Name', 'Age', 'Gender']); const data = ref([ { Name: 'Alice', Age: 25, Gender: 'Female' }, { Name: 'Bob', Age: 30, Gender: 'Male' }, { Name: 'Charlie', Age: 20, Gender: 'Male' }, ]); let dragIndex = null; const onDragStart = (index) => { dragIndex = index; }; const onDrop = (index) => { const temp = columns.value[dragIndex]; columns.value.splice(dragIndex, 1); columns.value.splice(index, 0, temp); data.value.forEach((row) => { const temp = row[dragIndex]; row.splice(dragIndex, 1); row.splice(index, 0, temp); }); dragIndex = null; }; return { columns, data, onDragStart, onDrop, }; }, }; </script> ``` 在这个示例中,我们将表格的列头设置为可拖拽,并使用`dragstart`事件将拖拽的列的索引存储在`dragIndex`变量中。在`drop`事件中,我们获取当前目标列的索引,并使用数据中的列进行交换,然后更新表格的渲染。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值