element-ui table表格封装应用

1、可直接复制粘贴引用

<template>
  <div>
    <el-table
      ref="elTable"
      class="lk-table-offsetTop"
      :class="['lk-table',{'isShowScrollbar':isShowScrollbar}]"
      :height="screenTableHeight"
      v-bind="elTableProps"
      @mouseover.native="mouseover($event)"
      @mouseout.native="mouseout($event)"
      @row-click="rowClick"
      v-on="$listeners">
      <el-table-column
        v-if="rowSelection"
        type="selection"
        width="60"
        align="center"
        :selectable="selectable"
        disabled
        :show-overflow-tooltip="showOverflowTooltip"
        fixed
      ></el-table-column>
      <el-table-column
        v-if="showIndex"
        type="index"
        label="序号"
        width="50"
        align="center"
        fixed
      ></el-table-column>
      <el-table-column
        v-for="(column,colI) in columns"
        :key="colI"
        v-bind="column"
        :show-overflow-tooltip="showOverflowTooltip"
        :sort-orders="!column.sortOrders?['ascending', 'descending', null]:column.sortOrders"
        :fixed="column.fixed">
        <template
          slot="header"
          slot-scope="scope">
          <slot
            v-if="column['slotHeader']"
            :name="column['slotHeader']"
            v-bind="scope"
            :column="column"></slot>
          <span v-else>{{ column['label'] ? column['label'] : '-' }}</span>
        </template>
        <template slot-scope="scope">
          <slot
            v-if="column['slotName']"
            :name="column['slotName']"
            v-bind="scope"></slot>
          <span v-else>{{ $lkcommon.isEmpty(scope.row[column['prop']]) ? '-':scope.row[column['prop']] }}</span>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { Table } from 'element-ui';

export default {
  name: 'LkTable',
  components: {},
  props: Object.assign({}, Table.props, {
    columns: {
      type: Array,
      default: () => []
    },
    rowSelection: {
      type: Boolean,
      default: false
    },
    showIndex: {
      type: Boolean,
      default: false
    },
    rowStyle: {
      type: Object,
      default: { height: '48px' }
    },
    stripe: {
      type: Boolean,
      default: true
    },
    selectable: {
      type: Function,
      default: (row, index) => true
    },
    height: {
      type: [Number, String],
      default: () => ''
    },
    // 动态配置表格高度时表格距离底部的高度
    bottomHeight: {
      type: Number,
      default: 86
    },
    // 是否动态设置表格高度
    autoHeight: {
      type: Boolean,
      default: false
    },
    // 是否单行显示,当内容过长被隐藏时显示 tooltip
    showOverflowTooltip: {
      type: Boolean,
      default: true
    }
  }),
  // 定义属性
  data() {
    return {
      // eslint-disable-next-line no-mixed-spaces-and-tabs
      isShowScrollbar: false,
      screenTableHeight: null
    };
  },
  // 计算属性,会监听依赖属性值随之变化
  computed: {
    elTableProps: function () {
      const props = {};
      Object.keys(Table.props).forEach(k => {
        typeof this[k] !== 'undefined' && (props[k] = this[k]);
        return props[k];
      });
      return props;
    }
  },
  // 监控data中的数据变化
  watch: {},
  // 生命周期 - 创建完成(可以访问当前this实例)
  created() {

  },
  // 生命周期 - 挂载完成(可以访问DOM元素)
  mounted() {
    this.$nextTick(() => {
      this.screenTableHeight = this.height;
    });
    // 动态设置表格的高度
    this.autoHeight && this.setTableHeight(true);
  },
  beforeCreate() {
  }, // 生命周期 - 创建之前
  beforeMount() {
  }, // 生命周期 - 挂载之前
  beforeUpdate() {
  }, // 生命周期 - 更新之前
  updated() {
  }, // 生命周期 - 更新之后
  beforeDestroy() {
  }, // 生命周期 - 销毁之前
  destroyed() {
  }, // 生命周期 - 销毁完成
  activated() {
  },
  // 方法集合
  methods: {
    rowClick(row) {
      this.$emit('rowClick', row);
    },
    clearSelection() {
      this.$refs.elTable.clearSelection(arguments);
    },
    toggleRowSelection() {
      this.$refs.elTable.toggleRowSelection(arguments);
    },
    toggleAllSelection() {
      this.$refs.elTable.toggleAllSelection(arguments);
    },
    toggleRowExpansion() {
      this.$refs.elTable.toggleRowExpansion(arguments);
    },
    setCurrentRow() {
      this.$refs.elTable.setCurrentRow(arguments);
    },
    clearSort() {
      this.$refs.elTable.clearSort(arguments);
    },
    clearFilter() {
      this.$refs.elTable.clearFilter(arguments);
    },
    doLayout() {
      this.$refs.elTable.doLayout(arguments);
    },
    sort() {
      this.$refs.elTable.sort(arguments);
    },
    // 移进
    mouseover() {
      this.isShowScrollbar = true;
    },
    mouseout() {
      this.isShowScrollbar = false;
    },
    // 设置表格的高度
    setTableHeight(onresize = false) {
      if (this.height) return;
      this.$nextTick(() => {
        try {
          const tableTop = this.getElementTop(document.querySelector('.lk-table-offsetTop')) + this.bottomHeight;
          const _height = `${document.documentElement.clientHeight}` - tableTop;
          this.screenTableHeight = _height > 200 ? _height : 200;
        } catch (error) {
        }
      });
      if (!onresize) return;
      window.onresize = () => {
        this.setTableHeight();
      };
    },
    // 获取元素距离浏览器窗口顶部的高度
    getElementTop(el) {
      let actualTop = el.offsetTop || 0;
      let current = el.offsetParent;
      while (current !== null) {
        actualTop += current.offsetTop;
        current = current.offsetParent;
      }
      return actualTop;
    }
  } // 如果页面有keep-alive缓存功能,这个函数会触发
};
</script>

<style lang='scss' scoped>
@import "@/styles/element-variables.scss";

.lk-table {
  background-color: #FFFCF9;
  color: #333333;
  // head
  ::v-deep thead {
    color: #7b7f8d;

    th {
      font-weight: normal;
    }
  }

  ::v-deep.el-table__header-wrapper {
    border-radius: 10px 10px;
    .has-gutter {
      tr {
        th {
          border: none;

          &:nth-child(1) {
            border-radius: 12px 0 0 12px;
          }
        }
      }
    }
  }

  ::v-deep.el-table__fixed {
    .el-table__fixed-header-wrapper {
      .el-table__header {
        tr {
          th {
            border-radius: 10px 0px 0 10px;
          }
        }
      }
    }
  }

  ::v-deep th.el-table__cell {
    background-color: $lk-table-header-background;

    .cell {
      span {
        color: $lk-table-header-color;
        font-weight: 500;
        font-size: 14px;
      }
    }
  }

  body
  &.el-table--striped
    ::v-deep
    .el-table__body
    tr.el-table__row--striped
    td.el-table__cell {
    background-color: #FFFCF9;
  }
  ::v-deep tr {
    background-color: #FFFCF9;
  }

  &.el-table--striped ::v-deep .el-table__body tr.hover-row {
    &,
    &.el-table__row--striped {
      &,
      &.current-row {
        > td.el-table__cell {
          background-color: $lk-table-hover-background;
        }
      }
    }

    ::v-deep .el-table__body-wrapper::-webkit-scrollbar {
      width: 8px;
      height: 8px;
    }

    ::v-deep .el-table__body-wrapper::-webkit-scrollbar-button {
      display: none;
    }

    ::v-deep .el-table__body-wrapper::-webkit-scrollbar-track {
      display: none;
    }

    ::v-deep .el-table__body-wrapper:-webkit-scrollbar-corner {
      display: none;
    }
  }

  ::v-deep .el-table__body-wrapper::-webkit-scrollbar-thumb {
    border-radius: 5px;
    background: #dddee0;
    display: block;
  }

  .isShowScrollbar {
     ::v-deep .el-table__body-wrapper::-webkit-scrollbar-thumb {
      display: block;
    }
  }

  ::v-deep.el-table__fixed::before,
  ::v-deep.el-table__fixed-right::before {
    height: 0px !important;
  }
}

::v-deep.el-table::before {
  height: 0px !important;
}

::v-deep.el-table--border::before {
  height: 1px !important;
}
</style>

2、在组件中引用

html中

		<LkNescTable
            :row-style="{height: '55.5px'}"
            :data="tableData"
            :border="false"
            :autoHeight="false"
            :columns="columns"
          >
            <template slot="tqJe" slot-scope="{row}">
              <el-input
                v-model="row.tqJe"
                maxlength="50"
                :disabled="editRight"
                oninput="value=value.replace(/^\D*(\d*(?:\.\d{0,5})?).*$/g, '$1')"
                style="width:100%"></el-input>
            </template>
          </LkNescTable>

data():columns中prop值绑定后端返回数据的字段,slotName可对当前字段进行单独编辑使用

	columns: [
        { prop: 'contractId', label: '合同编号' },
        { prop: 'tqJe', label: '已提取金额(元)', slotName: 'tqJe' },
        { prop: 'lrsj', label: '录入时间' }
      ],
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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的二次封装。如果有任何问题,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值