vue+Element UI table表格动态渲染(包括操作按钮列)、按钮动态绑定点击事件

涉及内容:

  1. Element UI的文档与使用;
  2. vue如何动态循环渲染Element UI中table内容;
  3. Element UI中table最后一列为操作按钮,如何动态渲染并绑定点击事件。

一、Element UI的文档与使用
Element UI文档
element-ui的安装使用步骤

二、vue如何动态循环渲染Element UI中table内容;Element UI中table最后一列为操作按钮,如何动态渲染并绑定点击事件

  1. Table.vue
<template>
  <div>
    <el-table :data="tableData" class="table_bsm">
      <el-table-column
        v-for="(col, index) in cols"
        :key="index"
        :prop="col.prop"
        :label="col.label"
      >
        <template slot-scope="scope">
          <i-cell :prop="col.prop" :row="scope.row"></i-cell>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import iCell from "./iCell";
export default {
  name: "Table",
  components: {
    iCell,
  },
  data() {
    return {
      cols: [
        { prop: "name", label: "姓名" },
        { prop: "sex", label: "性别" },
        { prop: "age", label: "年龄" },
        { prop: "operate", label: "操作" },
      ],
      tableData: [
        {
          name: "阿熙",
          sex: "男",
          age: "18",
          operate: [
            { name: "新增", type: "primary", size: "mini", click: "add" },
            { name: "编辑", type: "danger", size: "mini", click: "edit" },
            { name: "删除", type: "primary", size: "mini", click: "delete" },
          ],
        },
        {
          name: "小美",
          sex: "女",
          age: "16",
          operate: [
            { name: "新增", type: "primary", size: "mini", click: "add" },
            { name: "编辑", type: "danger", size: "mini", click: "edit" },
            { name: "删除", type: "primary", size: "mini", click: "delete" },
          ],
        },
        {
          name: "囡囡",
          sex: "女",
          age: "20",
          operate: [
            { name: "新增", type: "primary", size: "mini", click: "add" },
            { name: "编辑", type: "danger", size: "mini", click: "edit" },
            { name: "删除", type: "primary", size: "mini", click: "delete" },
          ],
        },
      ],
    };
  },
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.table_bsm {
  width: 98%;
  margin: auto;
}
</style>

  1. iCell.vue
<template>
  <div>
    <span v-if="prop != 'operate'">{{ row[prop] }}</span>
    <div v-if="prop == 'operate'">
      <el-button
        v-for="(item, i) in row.operate"
        :key="i"
        v-on:[eventName]="handleClick(item.click)"
        :type="item.type"
        :size="item.size"
        >{{ item.name }}</el-button
      >
    </div>
  </div>
</template>

<script>
export default {
  name: "iCell",
  props: {
    prop: String,
    row: Object,
  },
  data() {
    return {
      eventName: "click",
    };
  },
  methods: {
    //动态绑定操作按钮的点击事件
    handleClick(i) {
      let onClick = i;
      this[onClick]();
    },

    //新增
    add() {
      alert("新增~");
    },
    //编辑
    edit() {
      alert("编辑~");
    },
    //删除
    delete() {
      alert("删除~");
    },
  },
};
</script>

<style scoped></style>

  1. 效果图
    在这里插入图片描述
  • 8
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要实现这个功能,你需要在 Vue 中使用 Element UI 组件库,并且需要在你的组件中添加一个回收站按钮点击事件,同需要在你的数据中定义一个是否显示回收站的状态。具体步骤如下: 1. 在你的组件模板中添加一个回收站按钮,并为该按钮绑定一个点击事件: ``` <template> <div> <el-button type="primary" @click="showRecycleBin">回收站</el-button> <!-- 其他内容 --> </div> </template> ``` 2. 在你的组件中添加一个 `showRecycleBin` 方法,并在该方法中切换回收站的显示状态: ``` <script> export default { data() { return { isRecycleBinShow: false, // 其他数据 } }, methods: { showRecycleBin() { this.isRecycleBinShow = !this.isRecycleBinShow; }, // 其他方法 } } </script> ``` 3. 在你的组件模板中添加回收站组件,并使用 `v-if` 指令控制其显示: ``` <template> <div> <el-button type="primary" @click="showRecycleBin">回收站</el-button> <el-table v-if="isRecycleBinShow" :data="recycleBinData"> <!-- 表格定义 --> </el-table> <!-- 其他内容 --> </div> </template> ``` 4. 在你的组件中定义回收站数据,并在需要的候更新该数据: ``` <script> export default { data() { return { isRecycleBinShow: false, recycleBinData: [], // 定义回收站数据 // 其他数据 } }, methods: { showRecycleBin() { this.isRecycleBinShow = !this.isRecycleBinShow; if (this.isRecycleBinShow) { // 当回收站显示,从后端获取回收站数据 this.getRecycleBinData(); } }, getRecycleBinData() { // 向后端请求回收站数据 // 更新回收站数据 this.recycleBinData = [...]; }, // 其他方法 } } </script> ``` 这样,当你点击回收站按钮,回收站就会显示出来,并从后端获取回收站数据。你还需要添加删除数据的功能,以及恢复数据的功能,这些功能可以在 `recycleBinData` 数据中添加一些标记字段,然后在表格渲染出来,并在表格添加一些操作按钮来实现。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值