自己将elementui中的el-table封装成的table,减少template中的代码

本文介绍了如何将ElementUI的el-table组件进行封装,以减少模板中的代码量。主要功能包括:状态转换过滤器,表格内编辑功能,多行单元格内容对齐,以及操作列动态按钮名称变更。示例代码展示了封装后的组件使用方法和数据结构。
摘要由CSDN通过智能技术生成

代码写的有点low,但能用。
使用场景
1.Status:后台传的数字转化成其他内容,类似过滤器
2.操作中点击修改按钮,表格span变成input,不过得这样加属性,类似双向绑定的原理,能立刻变
this.AbnormaOrderList = list.map(item => {
this.$set(item, ‘AbnormaOrderBoolean’, true)
return item

3.一个表格单元格有几行排齐的内容
4.再2的操作基础上,有时要按钮名字变,修改变成确定后,导致每行都变,也封装了只变操作那行的名字,不过会麻烦点,就是btn写成[[]]的形式,具体不说了

template:
<ElTables
v-loading=“listLoading”
:columns=“see_columns”
:data=“seeList”
@handleBtnClick=“handleBtnClick”
/>

data:
see_columns: [
{ name: ‘序号’, type: ‘index’, tooltip: true, width: ‘50’ },
{ name: ‘产品名称’, desc: ‘ItemName’, tooltip: true },
{ name: ‘单价’, desc: ‘OrderPrice’, tooltip: true },
{ name: ‘批次信息’, first: ‘LotCode’, second: ‘ManufactureDt’, third: ‘DisabledDatetime’, fourth: ‘ApprovalNum’, fifth: ‘SterilizationLotMaster’, sixth: ‘Serial’, many: true, tooltip: true },
{ name: ‘是否扫码’, desc: ‘barcodeflag’, tooltip: true, show: true, status: [‘主条码(否)批次条码(否)’, ‘主条码(是)批次条码(否)’, ‘主条码(是)批次条码(是)’] },
{ name: ‘操作’, desc: ‘operate’, width: ‘240’,
btns: [
{ name: ‘编辑’, btnType: ‘success’, clickType: ‘editProductLineUser’ },
{ name: ‘修改’, btnType: ‘success’, clickType: ‘addProductLineUser’ },
{ name: ‘删除’, btnType: ‘success’, clickType: ‘deleteProductLineUser’ }
]
}
],

script
import ElTable from '@/components/table’导入封装的组件
按钮事件
/** 表格内点击方法 */
handleBtnClick(scope) {
if (scope.type === ‘addProductLineUser’) {
} else if (scope.type === ‘editProductLineUser’) {
} else if (scope.type === ‘deleteProductLineUser’) {
}

<template>
  <div>
    <el-table
      ref="multipleTable"
      :data="formatData"
      border
      stripe
      style="width: 100%"
      @select="onTableSelect"
      @selection-change="handleSelectionChange"
    >
      >
      <!-- 表头 、表体-->
      <template v-for="column in columns">
        <!-- 循环父页面数据、对数据进行判断参数处理-->
        <el-table-column
          v-if="column.type"
          :fixed="column.fixed"
          :key="column.desc"
          :label="column.name"
          :sortable="column.sortable?'custom':false"
          :type="column.type"
          :width="column.width==undefined?'auto':column.width"
          align="center"
        />
        <el-table-column
          v-if="!column.type"
          :fixed="column.fixed"
          :key="column.desc"
          :label="column.name"
          :prop="column.desc"
          :show-overflow-tooltip="column.tooltip?true:false"
          :sortable="column.sortable?'custom':false"
          :width="column.width==undefined?'auto':column.width"
          align="center"
        >
          <template slot-scope="scope">
            <!-- column.link为true  显示可点击状态 -->
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
el-tableElement UI库的一个组件,用于展示和操作表格数据。如果你想要封装el-table,可以按照以下步骤进行操作: 1. 创建一个新的Vue组件,比如TableWrapper。 2. 在TableWrapper组件引入el-table,并将其作为模板的一部分。 3. 通过props将数据源、列配置等必要的参数传递给TableWrapper组件。 4. 在TableWrapper组件使用props接收传递的参数,并将其传递给el-table。 5. 可以使用slot在TableWrapper组件定义自定义的表头、表尾等内容,并将其插入到el-table。 6. 可以使用events或者methods在TableWrapper组件处理el-table的事件和方法。 下面是一个简单的示例代码: ```vue <template> <div> <el-table :data="tableData" :columns="tableColumns"> <!-- 自定义表头 --> <el-table-column label="姓名" prop="name"></el-table-column> <el-table-column label="年龄" prop="age"></el-table-column> </el-table> </div> </template> <script> export default { props: { tableData: { type: Array, default: () => [], }, tableColumns: { type: Array, default: () => [], }, }, } </script> ``` 然后你可以在另一个父组件使用TableWrapper组件,并传递相应的数据和列配置: ```vue <template> <div> <table-wrapper :table-data="data" :table-columns="columns"></table-wrapper> </div> </template> <script> import TableWrapper from './TableWrapper.vue' export default { components: { TableWrapper, }, data() { return { data: [ { name: '张三', age: 18 }, { name: '李四', age: 20 }, ], columns: [ { label: '姓名', prop: 'name' }, { label: '年龄', prop: 'age' }, ], } }, } </script> ``` 这样,你就将el-table封装到了TableWrapper组件,并可以通过传递数据和列配置来渲染表格。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值