0


vue+Element UI Table表格动态渲染表头内容及操作按钮

循环表格头信息数组

   <el-table
      v-loading="loading"
      style="width: 100%"
      :data="tableData"
      @selection-change="selectionChange"
      @sort-change="sortChange"
    >
      <el-table-column
        v-for="(item, index) in tableHeaders"
        header-align="center"
        :label="item.label"
        :prop="item.prop"
        :align="item.align"
        :sortable="item.sortable"
        :key="index"
      >
        <template slot-scope="scope">
          <Render
            v-if="item.label == '操作'"
            :scope="scope"
            :render="item.render"
          />
          <span v-else>{{ scope.row[scope.column.property] }}</span>
        </template>
      </el-table-column>
    </el-table>

封装操作组件并引入表格文件内

<script>
export default {
  name: "LbRender",
  functional: true,
  props: {
    scope: Object,
    render: Function,
  },
  render: (h, ctx) => {
    return ctx.props.render ? ctx.props.render(h, ctx.props.scope) : "";
  },
};
</script>

配置表头信息数组及添加操作事件

 data() {
    return {
      tableHeaders: [
        { label: "商品名称", prop: "name", width: "380", align: "center" },
        {
          label: "价格(元)",
          prop: "a",
          width: "160",
          align: "center",
          sortable: true,
        },
        {
          label: "总库存",
          prop: "b",
          width: "260",
          align: "center",
          sortable: true,
        },
        {
          label: "销量",
          prop: "c",
          width: "160",
          align: "center",
          sortable: true,
        },
        {
          label: "是否上架",
          prop: "d",
          width: "160",
          align: "center",
        },
        { label: "操作结果", prop: "e", width: "160", align: "center" },
        { label: "审核", prop: "f", width: "160", align: "center" },
        {
          label: "操作",
          prop: "address",
          width: "160",
          align: "center",
          sortable: true,
          render: (h, scope) => {
            return (
              <div>
                <el-button
                  type="text"
                  onClick={() => {
                    this.edit(scope.row);
                  }}
                >
                  编辑
                </el-button>
                <el-button
                  type="text"
                  style="margin-left:10px"
                  onClick={() => {
                    this.delete(scope.row);
                  }}
                >
                  删除
                </el-button>
                <el-button
                  type="text"
                  style={scope.row.state ? "" : "margin-left:10px;display:none"}
                  onClick={() => {
                    this.delete(scope.row);
                  }}
                >
                  下架
                </el-button>
              </div>
            );
          },
        },
      ],
    };
  },
  methods: {
    edit(row) {
      console.log("edit");
    },
    delete(row) {
      console.log("delete");
    },
  },
标签: 前端 vue.js

本文转载自: https://blog.csdn.net/HTE_YOUNG_MAN/article/details/128843750
版权归原作者 傍晚的月亮照着花开的田野 所有, 如有侵权,请联系我们删除。

“vue+Element UI Table表格动态渲染表头内容及操作按钮”的评论:

还没有评论