vue-iview-table二次封装,render函数,slot的封装

18 篇文章 4 订阅

前言:

        正好这个项目用到iviewtable,之前有封装的elementtable,这里把iview的也封装下,提高复用性和价值。

首先:我们的公共组件ctable.vue

<template>
  <Table 
    :width='width'
    :height='height'
    :stripe='stripe' 
    :border='border' 
    :disabled-hover='disabledHover'
    :highlight-row='highlightRow'
    :columns="columns" 
    :data="list" 
    @on-selection-change="seclectChange"
    @on-row-click='rowClick'
    @on-row-dblclick='rowdblclick'
    @on-current-change='currentChange'
    @on-select-all='selectAll'
    @on-select-all-cancel='selectAllCancel'
    @on-sort-change='sortChange'
    @on-filter-change='filterChange'
    >
    <!-- 定义slot部分 -->
    <template slot-scope="{ row, index }" slot="operation">
      <Button size="small" type="primary" @click="clickBJ(row)">编辑</Button>
    </template>
    <template slot-scope="{ row, index }" slot="operationT">
      <Button size="small" type="primary" @click="clickZX(row)" style="margin-right:5px;">执行</Button>
      <Button size="small" type="primary" @click="clickTJ(row)">统计</Button>
    </template>
  </Table>
</template>

<script>
  export default {
    name:'iview-table',
    props:{
        columns: Array, // 表头数据
        list: Array, // 表格数据
        width: [Number, String],//表格宽度
        height: [Number, String],//表格高度,单位 px,设置后,如果表格内容大于此值,会固定表头-***注意传进来是number的有滚动条,px的没有
        stripe: { // 是否显示间隔斑马纹
            type: Boolean,
            default: false
        },
        border: { // 是否显示纵向边框
            type: Boolean,
            default: false
        },
        disabledHover: { // 禁用鼠标悬停时的高亮
            type: Boolean,
            default: false
        },
        highlightRow: { // 是否支持高亮选中的行,即单选
            type: Boolean,
            default: false
        }
    },
     data () {
      return {

      };
    },
    methods:{
      /**
       * 自带事件
       */
      rowClick(data,index){//单击某一行时触发,data==当前行数据,index==当前行的下标
        let str={
          'data':data,
          'index':index
        }
        this.$emit("rowClick",str)
      },
      rowdblclick(data,index){//双击击某一行时触发,data==当前行数据,index==当前行的下标
        let str={
          'data':data,
          'index':index
        }
        this.$emit("rowdblclick",str)
      },
      currentChange(currentRow,oldCurrentRow){//开启 highlight-row 后有效,当表格的当前行发生变化的时候会触发,currentRow==当前高亮行数据,oldCurrentRow==上次高亮行数据
        let str={
          'data':currentRow,
          'oldData':oldCurrentRow
        }
        this.$emit("currentChange",str)
      },
      seclectChange(selection){//在多选模式下有效,只要选中项发生变化时就会触发,selection==已选项数据
        let str={
          'data':selection
        }
        this.$emit("selectionData",str)
      },
      selectAll(selection){//在多选模式下有效,点击全选时触发,selection:已选项数据
        let str={
          'data':selection
        }
        this.$emit("selectAll",str)
      },
      selectAllCancel(selection){//在多选模式下有效,取消点击全选时触发,selection:已选项数据
        let str={
          'data':selection
        }
        this.$emit("selectAllCancel",str)
      },
      sortChange(column,key,order){//排序时有效,当点击排序时触发,column==当前列数据,key==排序依据的指标,order==asc/desc
        let str={
          'column':column,
          'order':order,
          'key':key
        }
        this.$emit("sortChange",str)
      },
      filterChange(column){//筛选时有效,筛选条件发生变化时触发,column==当前列数据
        let str={
          'column':column
        }
        this.$emit("filterChange",str)
      },
      /**
       * slot事件
       */
      clickBJ(data){//编辑点击事件
         this.$emit("clickBJ",data)
      },
      clickZX(data){//编辑执行事件
        this.$emit("clickZX",data)
      },
      clickTJ(data){//编辑统计事件
        this.$emit("clickTJ",data)
      }
    }
  }
</script>

其次:就是调用部分,我这里简单说下里面的步骤,最底下会把完整的调用代码贴上的,方便直接用,

1、template部分:

2、script-data部分:

3、script-method部分:

接下来贴一下调用部分的完整代码:

<template>
  <div>
    <CTable 
    :columns='this.dyydyl.col' 
    :list='this.dyydyl.list' 
    :height='this.dyydyl.height' 
    :stripe='this.dyydyl.stripe'
    @clickBJ='clickBJ'
    @clickZX='clickZX'
    @clickTJ='clickTJ'
    ></CTable>
  </div>
</template>

<script>
  
  import CTable from '@/components/comIview/ctable'//iview-table
  export default {
    name:'moral',//道德讲堂
    props:[''],
    data () {
      return {
        dyydyl:{//党员一带一路
          height:280,
          stripe:true,//斑马纹
          col:[
              // {//是否显示序号-不分页
              //     type: 'index',
              //     width: 60,
              //     align: 'center'
              // },
              // {//是否显示复选框
              //     type: 'selection',
              //     width: 60,
              //     align: 'center'
              // },
              {//是否显示序号-分页
				title: '序号',
				width: 50,
				align: 'center',
				render: (h,params) => {
					let rows = this.searchDTO.pageParamers.rows;//page每页展示多少条
					let page = this.searchDTO.pageParamers.page;//page当前页码
					return h('span',params.index + (rows*(page-1)+1))
				    }
				},
             {
                title: '党员',
                key: 'dy',
                align: 'center',
                width: 80,
              },
              {
                title: '等级',
                key: 'dj',
                align: 'center',
                width: 80,
                render: (h, params) => {//render函数,iview这里可以直接用
                  let dj = params.row.dj;//params.row.dj==获取到的值
                  let style={};
                  if(dj == '一般'){
                    style.color = '#00fffc';
                  }else if(dj == '重大'){
                    style.color = '#ff5816';
                  }else if(dj == '紧急'){
                    style.color = '#ffc000';
                  }
                  return h('div', {
                    style:style
                  } ,params.row.dj)
                }
              },
              {
                title: '目标',
                key: 'mb',
                align: 'center',
                width: 'auto',
              },
              {
                title: '操作',
                slot: 'operationT',
                align: 'center',
                width: 'auto',
              },
              {
                title: '操作',
                slot: 'operation',
                align: 'center',
                width: 'auto',
              },
                //这里在加个在页面通过render渲染两个按钮的方法
            //   {
            //     title: '培训地点',
            //     align: 'center',
            //     width: 'auto',
            //     render: (h, params) => {
            //             return h("div", [
            //                 h(
            //                     "Button",
            //                     {
            //                     props: {
            //                         type: "primary",
            //                         size: "small"
            //                     },
            //                     style: {
            //                         marginRight: "5px"
            //                     },
            //                     on: {
            //                         click: (e) => {
            //                              e.stopPropagation();//阻止冒泡
            //                              alert('查看');
            //                         }
            //                     }
            //                     },
            //                     "查看"
            //                 ),
            //                 h(
            //                     "Button",
            //                     {
            //                     props: {
            //                         type: "primary",
            //                         size: "small"
            //                     },
            //                     style: {
            //                         marginRight: "5px"
            //                     },
            //                     on: {
            //                         click: (e) => {
            //                              e.stopPropagation();//阻止冒泡
            //                              alert('删除');
            //                         }
            //                     }
            //                     },
            //                     "删除"
            //                 )
            //             ]);
            //         }
            //    },



          ],
          list:[
            {
                'dy': '陈盛泽',
                'dj': '一般',
                'mb': '争取在年底前超额完成任务',
              },
              {
                'dy': '张盛泽',
                'dj': '重大',
                'mb': '争取在年底前超额完成任务',
              },
              {
                'dy': '张盛泽',
                'dj': '紧急',
                'mb': '争取在年底前超额完成任务',
              },
              {
                'dy': '张盛泽',
                'dj': '一般',
                'mb': '争取在年底前超额完成任务',
              },
              {
                'dy': '张盛泽',
                'dj': '党员',
                'mb': '争取在年底前超额完成任务',
              },
              {
                'dy': '张盛泽',
                'dj': '重大',
                'mb': '争取在年底前超额完成任务',
              },
              {
                'dy': '张盛泽',
                'dj': '党员',
                'mb': '争取在年底前超额完成任务',
              },
              {
                'dy': '张盛泽',
                'dj': '党员',
                'mb': '争取在年底前超额完成任务',
              },
              {
                'dy': '张盛泽',
                'dj': '党员',
                'mb': '争取在年底前超额完成任务',
              },
              {
                'dy': '张盛泽',
                'dj': '党员',
                'mb': '争取在年底前超额完成任务',
              },
              {
                'dy': '张盛泽',
                'dj': '党员',
                'mb': '争取在年底前超额完成任务',
              },
              {
                'dy': '张盛泽',
                'dj': '党员',
                'mb': '争取在年底前超额完成任务',
              },
              {
                'dy': '张盛泽',
                'dj': '党员',
                'mb': '争取在年底前超额完成任务',
              },
              {
                'dy': '张盛泽',
                'dj': '党员',
                'mb': '争取在年底前超额完成任务',
              },
              {
                'dy': '张盛泽',
                'dj': '党员',
                'mb': '争取在年底前超额完成任务',
              },
          ]
        },
      };
    },

    components: {
      CTable
    },

    computed: {},

    beforeMount() {},

    mounted() {},

    methods: {
      clickBJ(){
        alert('编辑');
      },
      clickZX(){
        alert('执行');
      },
      clickTJ(){
        alert('统计');
      },
    },

    watch: {}

  }

</script>
<style lang='' scoped>

</style>

 

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二次封装。如果有任何问题,请随时提问。
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

浩星

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

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

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

打赏作者

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

抵扣说明:

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

余额充值