element-ui table 二次封装

结合项目需求,将element ui table进行了二次封装,封装后通用表格配置更方便

使用时,配置table-cols 以及table-data 来设置表格每列 

<m-table
    :load-data="loadData"
    :is-index="true"
    :is-selection="true"
    :table-data="tableData"
    :table-cols="tableCols"
    :pagination="pagination">
  <template v-slot:input="data">
    <span v-if="data.data.type===1">{{ data.data.id }}</span>
    <el-input v-else v-model="data.data.id"/>
  </template>
  <template v-slot:opt="data">
    <el-button @click="play(data.data)">播放</el-button>
  </template>
  <template v-slot:empty>
    <div>我是空数据提示内容</div>
  </template>
</m-table>
tableCols: [
  {
    label: "日期", prop: 'broadcast_date', formatter: (row) => {
    }
  },
  {label: "节目名称", prop: 'name',},
  {label: "频道", prop: 'channel_name',},
  {label: "id", type: "slot", slotName: 'input',},
  {label: "id", prop: "id", type: "input"},
  {label: "操作", type: "slot", slotName: 'opt',},
  {
    label: "操作", type: "button", formatter: (row) => {
      if (row.index % 2 === 1) {
        return [{
          label: "播放",
          handle: (row) => {
            this.play(row)
          },
          isDisabled: (row) => {
            return row.index % 3 === 1
          }
        },
          {
            label: "放映",
            handle: (row) => {
              this.play(row)
            },
            disabled: true
          },]
      } else {
        return [{
          label: "收听", handle: (row) => {
            console.log("收听", row)
          }
        }]
      }
    },

  }
],

 

 

预留solt 可根据自己需求扩展  组件   

<el-table-column
    v-for="item in tableCols"
    :key="item.id"
    :prop="item.prop"
    :fixed="item.fixed"
    :label="item.label"
    :width="item.width"
    align="center">
  <template slot-scope="scope">
    <slot v-if="item.type==='slot'" :name="item.slotName" :data="scope.row"></slot>
    <el-input
        v-if="item.type==='input'"
        v-model="scope.row[item.prop]"
        :size="item.size || size"
        :disabled="item.disabled || (item.isDisabled && item.isDisabled(scope.row))"
        @focus="item.focus && item.focus(scope.row)"
        @blur="item.blur && item.blur(scope.row)">
    </el-input>

github地址:https://github.com/zhangxiang0316/vueUtils

 

 

 

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值