el-table的二次封装

子组件

<template>
  <keep-alive>
    <el-table :data="tableData" @cell-dblclick="copyText" tooltip-effect="light">
      <slot name="selection"></slot>
      <el-table-column v-for="item in tableLabel" :key="item.value" :prop="item.value" :label="item.label"
        :width="item.width ? item.width : '' " :formatter="item.formatter" :show-overflow-tooltip="true"
        :custom="item.copyable">
      </el-table-column>
      <slot name="operation"></slot>
    </el-table>
  </keep-alive>
</template>

<script>
export default {
  props: {
    tableLabel: {
      type: Array,
      required: true,
      default () {
        return []
      },
    },
    id: {
      type: String,
      required: true,
      default () {
        return String
      },
    },
    tableData: {
      type: Array,
      required: true,
      default () {
        return []
      },
    },
    copyText: {
      type: Function,
      default: () => {
        return Function;
      },
    }
  },
  methods: {

  },
}
</script>

<style scoped>
::v-deep .cell .el-tag {
  margin-right: 5px !important;
}
::v-deep .el-button {
  padding: 5px 0;
}
::v-deep .el-table__cell {
  padding: 10px 0 !important;
}
</style>

在父组件里使用

<template>
  <div>
    <gem-tables :id="'accountSettingTabsFour'" :tableLabel="accountSettingFourTableLabel"
      :tableData="accountSettingTabsFourData" :copyText="homeCopyText">
      <el-table-column label="操作" slot="operation">
        <template slot-scope="scope">
          <el-button ondblclick="false" type="text">明细</el-button>
        </template>
      </el-table-column>
    </gem-tables>
  </div>
</template>

<script>
import { mapState, mapActions } from 'vuex'
export default {
  data () {
    return {
      copyable: { copyText: '复制', copiedText: '已复制', timeout: 2000 },
      accountSettingFourTableLabel: [
        { label: '名称', value: 'name' },
        { label: '配置', value: 'configInfo', formatter: this.formatAge },
        { label: '描述', value: 'description' }
      ],
      accountSettingTabsFourData: [
        {
          name: 1,
          configInfo: 1,
          description: '是法国岁的法国岁的法国岁的法国是'
        },
        {
          name: 1,
          configInfo: 1,
          description: '是法国岁的法国岁的法国岁的法国是'
        },
        {
          name: 1,
          configInfo: 100,
          description: '是法国岁的法国岁的法国岁的法国是'
        },
        {
          name: 1,
          configInfo: 1,
          description: '是法国岁的法国岁的法国岁的法国是'
        },
        {
          name: 1,
          configInfo: 1,
          description: '是法国岁的法国岁的法国岁的法国是是法国岁的法国是是法国岁的法国岁的法国是是法国岁的法国岁的法国岁的法国是是法国岁的法国岁的法国岁的法国是是法国是是法国岁的法国岁的法国岁的法国是是法国岁的法国岁的法国岁的法国是是法国是是法国岁的法国岁的法国岁的法国是是法国岁的法国岁的法国岁的法国是是法国岁的法国是是法国岁的法国岁的法国岁的法国是是法国是是法国岁的法国岁的法国岁的法国是是法国岁的法国岁的法国岁的法国是是法国是是法国岁的法国岁的法国岁的法国是是法国岁的法国岁的法国岁的法国是是法国岁的法国岁的法国是是法国岁的法国岁的法国岁的法国是是法国岁的是是法国岁的法国岁的法国岁的法国是是法国岁的法国岁的法国岁的法国是是法国是是法国岁的法国岁的法国岁的法国是是法国岁的法国岁的法国岁的法国是是法国法国岁的法国岁的法国是是法国岁的法国岁的法国岁的法国是'
        },
      ]
    }
  },
  methods: {
    formatAge (row) {
      let type = '';
      if (row.configInfo >= 18) {
        type = 'success';
      } else {
        type = 'warning';
      }
      return [this.$createElement('el-tag', { props: { type: type, size: "mini" } }, `${row.configInfo} 岁`), this.$createElement('el-tag', { props: { type: type, size: "mini" } }, `${row.configInfo} 岁`)];
    },
    homeCopyText (row, column, cell, event) {
      if (column.label == '描述') {
        this.$copyText(row, column, cell, event)
      }
    }
  },
}
</script>

<style lang="scss" scoped>
</style>

双击复制

function copyText (row, column, cell, event) {
  // 双击复制
  let save = function (e) {
    e.clipboardData.setData('text/plain', event.target.innerText);
    e.preventDefault();  //阻止默认行为
  }
  document.addEventListener('copy', save);//添加一个copy事件
  document.execCommand("copy");//执行copy方法
  this.$message({ message: '复制成功', type: 'success' })//提示
}

App.vue(不要添加scoped)

<style lang="scss">
.el-tooltip__popper {
  max-width: 35%;
}
</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mars空港

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值