vue中element-ui表格组件el-table封装,在table表格中插入图片

        这次写的项目是写后台管理系统这部分,对于后台管理使用vue写,用组件的话,table组件用得次数比较多,可以封装一个table组件。

        1.如封装的table组件:

<template>
  <el-table
    :data="tableData"
    height="400px"
    max-height="400px"
    size="small"
    row-class-name="row"
    cell-class-name="column"
    :highlight-current-row="true"
    fit
  >
    <el-table-column type="selection" width="55"></el-table-column>
    <el-table-column
      v-for="(item, index) in tableLabel"
      :key="index"
      :fixed="item.fixed"
      :prop="item.prop"
      :width="item.width"
      :label="item.label"
    >
      <template slot-scope="scope">
        <template v-if="item.arr">
          <el-button
            size="mini"
            :type="item2.type == 'delete' ? 'danger' : ''"
            v-for="item2 in item.arr"
            :key="item2.type"
            @click="handleClick(item2.type,scope.row,scope.$index)"
            >{{ item2.name }}</el-button
          >
        </template>
        <div v-else class="boxTa">{{ scope.row[item.prop] }}</div>
      </template>
    </el-table-column>
  </el-table>
</template>

<script>
export default {
  name: "UserTableCenter",
  props: {
    tableData: {
      type: Array,
      default: () => [],
    },
    tableLabel: {
      type: Array,
      default: () => {
        return [];
      },
    },
  },
};
</script>

 :prop="item.prop"  :label="item.label"是必须要有的,其他的可以根据自己需要写

2.封装之后是就是使用了:

首先先引用这个封装好的组件

import UserTableCenter from "../../../components/backendPages/contentCenter/userTableCenter.vue";

然后再使用这个组件在内容部分

<UserTableCenter :tableData="tableData" :tableLabel="tableLabel"  />

3.传入数据

data() {
    return {
      tableLabel: [
        { label: "订单号", width: "", prop: "orderNumber"},
        { label: "订单总价", width: "", prop: "orderPrice" },
        { label: "订单状态", width: "", prop: "orderState" },
        { label: "支付方式", width: "", prop: "orderType" },
        { label: "创建时间", width: "", prop: "orderTime" },
        {
          prop: "action",
          label: "操作",
          arr: [{ name: "查看" }, { type: "delete", name: "删除" }],
        },
      ],
      tableData: [
        {
          orderNumber: "中国男",
          orderPrice: "阳光超市",
          orderState: "男",
          orderType: 18383929383,
          orderTime: "华北",
        },
      ],
    };
  },
};

如下图就是完成的结果图 

 还有如果是商品图片的话,还要在表格中插入图片,插入图片的方式如下

 <el-table-column prop="image" label="商品图片" width="120">
        <template slot-scope="scope">
          <el-image :src="scope.row.image" style="width:80px;height:50px" :preview-src-list="[scope.row.image]">
            <div slot="error" class="image-slot">
               
            </div>
          </el-image>
        </template>
    </el-table-column>

:src="scope.row.image" :preview-src-list="[scope.row.image]"row后面参数是返回的数据的类型

    tableData: [
        {
          date: "2016-05-03",
          introduction: "王小虎上海市普陀区金沙江路 1518 弄",
          image: require('../../../../public/imgaes/10861.jpg'),//这里要用require包含图片的地址
          number: "900",
          tag: "已上架",
          price: "800",
          time: "上海市普陀区金沙江路 1518 弄",
        },
]
  • 5
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Vue基于Element UI Table的二次封装可以通过创建一个自定义的组件来实现。以下是一个简单的示例,演示了如何封装一个基于Element UI Table组件: ```vue <template> <el-table :data="tableData" :row-key="rowKey" :height="height"> <!-- 渲染表头 --> <el-table-column v-for="column in columns" :key="column.prop" :prop="column.prop" :label="column.label"> <!-- 自定义插槽 --> <template slot-scope="scope"> <slot :column="column" :scope="scope"></slot> </template> </el-table-column> </el-table> </template> <script> export default { name: 'CustomTable', props: { tableData: { type: Array, required: true }, columns: { type: Array, required: true }, rowKey: { type: String, required: true }, height: { type: String, default: 'auto' } } } </script> ``` 在这个示例,我们创建了一个名为`CustomTable`的组件。它接受`tableData`、`columns`、`rowKey`和`height`作为props,分别表示表格数据、表格列配置、行数据的唯一标识以及表格的高度。 在模板,我们使用`el-table`和`el-table-column`来渲染Element UI表格。我们使用了`v-for`指令来循环渲染表格列,并通过`slot-scope`来传递数据给插槽。插槽可以在父组件定义,并在插槽使用自定义的组件来渲染表格单元格内容。 通过这种方式,我们可以在父组件使用这个封装的自定义表格组件,并通过插槽来定制表格的内容和样式。 希望这个简单的示例能帮助到你进行Vue基于Element UI Table的二次封装。如果有任何问题,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值