vue封装element中table组件

后台系统,table被用的次数比较多,所以决定提出来作为组件

1.新建一个Table.vue文件

<!--region 封装的分页 table-->
<template>
 <div class="table">
 <el-table id="iTable" v-loading.iTable="options.loading" :data="list" :stripe="options.stripe"
    ref="mutipleTable"
    @selection-change="handleSelectionChange">
  <!--region 选择框-->
  <el-table-column v-if="options.mutiSelect" type="selection" style="width: 55px;">
  </el-table-column>
  <!--endregion-->
  <!--region 数据列-->
  <template v-for="(column, index) in columns">
  <el-table-column :prop="column.prop"
       :key=‘column.label‘
       :label="column.label"
       :align="column.align"
       :width="column.width">
   <template slot-scope="scope">
   <template v-if="!column.render">
    <template v-if="column.formatter">
    <span v-html="column.formatter(scope.row, column)"></span>
    </template>
    <template v-else>
    <span>{{scope.row[column.prop]}}</span>
    </template>
   </template>
   <template v-else>
    <expand-dom :column="column" :row="scope.row" :render="column.render" :index="index"></expand-dom>
   </template>
   </template>
  </el-table-column>
  </template>
  <!--endregion-->
  <!--region 按钮操作组-->
  <el-table-column ref="fixedColumn" label="操作" align="center" :width="operates.width" :fixed="operates.fixed"
      v-if="operates.list.filter(_x=>_x.show === true).length > 0">
  <template slot-scope="scope">
   <div class="operate-group">
   <template v-for="(btn, key) in operates.list">
    <div class="item" v-if="btn.show" :key=‘btn.id‘>
    <el-button :type="btn.type" size="mini" :icon="btn.icon" :disabled="btn.disabled"
       :plain="btn.plain" @click.native.prevent="btn.method(key,scope.row)">{{ btn.label }}
    </el-button>
    </div>
   </template>
   </div>
  </template>
  </el-table-column>
  <!--endregion-->
 </el-table>

 </div>
</template>
<!--endregion-->
<script>
 export default {
 props: {
    list: {
      type: Array,
      default: []
      }, // 数据列表
    columns: {
      type: Array,
      default: []
      }, // 需要展示的列 === prop:列数据对应的属性,label:列名,align:对齐方式,width:列宽
    operates: {}, // 操作按钮组 === label: 文本,type :类型(primary / success / warning / danger / info / text),show:是否显示,icon:按钮图标,plain:是否朴素按钮,disabled:是否禁用,method:回调方法
    options: {
      type: Object,
      default: {
       stripe: false, // 是否为斑马纹 table
       highlightCurrentRow: false // 是否要高亮当前行
      },
    } // table 表格的控制参数
 },
//组件
 components: {
  expandDom: {
    functional: true,
    props: {
     row: Object,
     render: Function,
     index: Number,
     column: {
       type: Object,
       default: null
       }
      },
    render: (h, ctx) => {
       const params = {
       row: ctx.props.row,
       index: ctx.props.index
      }
     if (ctx.props.column) params.column = ctx.props.column
      return ctx.props.render(h, params)
    }
  }
 },
// 数据
 data () {
  return {
    pageIndex: 1,
    multipleSelection: [] // 多行选中
  }
 },
 mounted () {
 },
 computed: {
 },
 methods: {

  // 多行选中
  handleSelectionChange (val) {
    this.multipleSelection = val
    this.$emit(‘handleSelectionChange‘, val)
  },
  // 显示 表格操作弹窗
  showActionTableDialog () {
    console.log(4444)
    this.$emit(‘handelAction‘)
  }
 }
 }
</script>
<style lang="scss" >

</style>

 2.调用组件

<template>
 <div class="table-page">
 <i-table :list="list"
    @handleSelectionChange="handleSelectionChange"
    :options="options"
    :columns="columns"
    :operates="operates"
  >
 </i-table>
 </div>
</template>
<script>
 import iTable from ‘../components/Table‘
 export default {
  components: {iTable},
  data () {
   return {
    list: [
       {
        id: ‘24‘,
        title: ‘编号3‘,
        state:0
       },
       {
        id: ‘23‘,
        title: ‘编号3‘,
        state:1
       },
       {
        id: ‘23‘,
        title: ‘编号3‘,
        state:2
       },
       {
        id: ‘2‘,
        title: ‘编号3‘,
        state:0
       },
       {
        id: ‘223‘,
        title: ‘编号3‘,
        state:1
       },
       {
        id: ‘2444‘,
        title: ‘编号3‘,
        state:1
       },
    ], // table数据
    options: {
     stripe: true, // 是否为斑马纹 table
     loading: false, // 是否添加表格loading加载动画
     highlightCurrentRow: true, // 是否支持当前行高亮显示
     mutiSelect: true, // 是否支持列表项选中功能
    }, // table 的参数
    columns: [
     {
      prop: ‘id‘,
      label: ‘编号‘,
      align: ‘center‘,
     },
     {
      prop: ‘title‘,
      label: ‘标题‘,
      align: ‘center‘,
      formatter: (row, column, cellValue) => {
       return `<span style="white-space: nowrap;color: dodgerblue;">${row.title}</span>`
      }
     },
     {
      prop: ‘state‘,
      label: ‘状态‘,
      align: ‘center‘,
      render: (h, params) => {
       return h(‘el-tag‘, {
       props: {type: params.row.state === 0 ? ‘success‘ : params.row.state === 1 ? ‘info‘ : ‘danger‘} // 组件的props
       }, params.row.state === 0 ? ‘上架‘ : params.row.state === 1 ? ‘下架‘ : ‘审核中‘)
      }
     },

    ], // 需要展示的列
    operates: {
     width: 200,
     fixed: ‘right‘,
     list: [
      {
       id:‘1‘,
       label: ‘编辑‘,
       type: ‘warning‘,
       show: true,
       icon: ‘el-icon-edit‘,
       plain: true,
       disabled: false,
       method: (index, row) => {
        this.handleEdit(index, row)
       }
      },
      {
       id:‘2‘,
       label: ‘删除‘,
       type: ‘danger‘,
       icon: ‘el-icon-delete‘,
       show: true,
       plain: false,
       disabled: false,
       method: (index, row) => {
        this.handleDel(index, row)
       }
      }
     ]
    } // 列操作按钮
   }
  },
  methods: {
   // 选中行
   handleSelectionChange (val) {
    console.log(‘val:‘, val)
   },
   // 编辑
   handleEdit (index, row) {
    console.log(‘ index:‘, index)
    console.log(‘ row:‘, row)
   },
   // 删除
   handleDel (index, row) {
    console.log(‘ index:‘, index)
    console.log(‘ row:‘, row)
   }
  }
 }
</script>

  

原文地址:https://www.cnblogs.com/linsx/p/9882303.html

时间: 2024-07-29 09:14:36

vue封装element中table组件的相关文章

ivew | element ui- 关于table组件自定义sortMethod排序不生效或错误问题处理

最近项目需求需要,需要对表格列进行自定义排序,用的是iview的组件,看了文档,table 排序这部分,但是没有给出相关例子.以为不难搞的,是不难搞,就是折腾了好一会... Iview table 排序 data () { return { columns5: [ { title: 'Date', key: 'date', sortable: true, }, { title: 'Name', key: 'name' }, { title: 'Age', key: 'age', sortable

element ui table组件自定义合计栏,后台给的数据

合计的数据是后台传的,所以用table组件自定义一行用来合计 <el-table border fit v-loading.body="listLoading" element-loading-text="拼命加载中" :data="getChannelData" style="width: 100%" :default-sort = "{prop: 'date', order: 'descending'}&q

vue+element ui table组件封装,使用render渲染

后台管理经常会用到表格,一开始封装了一个常用的功能性表格,点击这里: 后来由于需求增加,在表格中还会用到switch,select,input等多种组件,每次都要在html中增加<el-table-column></el-table-column>, 维护起来相当麻烦,就想到了使用render渲染. 组件内部封装代码: <template> <el-table :data="tableData" size="medium"

Vue+Element中Table懒加载,新增、删除操作后手动更新

https://blog.csdn.net/qq_34817440/article/details/96482818 this.$set(this.$refs.tableDom.store.states.lazyTreeNodeMap, id, []); 原文地址:https://www.cnblogs.com/zhaoyun4122/p/12134174.html

vue.js 1中父组件跳到子组件中并传参让子组件显示不同的内容

父组件中的点击跳转: <ul class="insurance-entry clearfloat"> <li v-link="{name:'productlist',params:{id:1}}">1</li> <li v-link="{name:'productlist',params:{id:2}}">2</li> <li v-link="{name:'product

vue之element中el-dialog关闭相关属性

在el-dialog中加入这俩属性: :close-on-press-escape="false" :close-on-click-modal="false" 效果:键盘按Esc键,或者鼠标点击dialog外面, dialog都不会关闭了. PS: Vue中属性有无冒号的区别: 加冒号的,说明后面的是一个变量或者表达式:没加冒号的后面就是对应的字符串字面量! 尤其是Boolean类型的容易混淆.例如上面dialog例子,带冒号表示后面属性是Boolean,否则只是单

vue.js + element中el-select实现拼音匹配,分词、缩写、多音字匹配能力

1.既然要用到拼音搜索,我们就需要一个拼音库,在这里我推荐一个第三方包:https://github.com/xmflswood/pinyin-match,在这里首先对这个包的开发者表示万分的感谢. 2.安装第三方包: npm install pinyin-match --save3.在index.html中引入 <script src="pinyin-match/dist/main.js"></script> 4.好了现在我们就能使用了,话不多说直接上代码,为

修改element中table里面的右侧滚动条样式

/*定义滚动条高宽及背景 高宽分别对应横竖滚动条的尺寸*/ ::-webkit-scrollbar { width: 7px; /*滚动条宽度*/ height: 7px; /*滚动条高度*/ background-color: white; } /*定义滑块 内阴影+圆角*/ ::-webkit-scrollbar-thumb { -webkit-box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.3); background-color: rgba(221,

elementui中table组件表头和内容不对齐的问题

表头与内容没对齐 在样式里加入 .el-table th.gutter{ display: table-cell!important; } 终于舒服了 原文地址:https://www.cnblogs.com/reround/p/11981909.html