基于elementUI自定义表格

这个是基于elelmentUI,自定义表格,写了很久了,最近才整理。
自定义表格可以根据数据自动生成表头,多级表头,列
约定的数据格式如下:

/** 数据格式
   *
   headList:[
   {
     prop:"date",
     label:"日期",
     width:"150",
     url:'',
     path:'',
     yl:'',
     children:[]
   },
   {
     prop:"",
     label:"地址",
     width:"",
     url:'',
     path:'',
     yl:'',
     children:[{
       prop:"province",
       label:"省份",
       width:"120",
       url:'',
       path:'',
       yl:'',
     },
       {
         prop:"zip",
         label:"邮编",
         width:"120",
         url:'',
         path:'',
         yl:'',
       }
     ]
   }
   ],
   tableData: [{
        date: '2016-05-03',
        name: '王小虎',
        province: '上海',
        city: '普陀区',
        address: '上海市普陀区金沙江路 1518 弄',
        zip: 200333
      }]
   *
   */

HeadTable.vue

<template>
  <el-table
    v-loading.iTable="loading"
    :data="tableDatas"
    :row-style="selectedRowStyle"
    :row-class-name="rowStyle"
    style="width: 100%"
    :height="height"
    stripe
    border
    @row-click="scaleRow"
    @row-dblclick="dblclick"
    @sort-change="sortchange"
  >
    <!--:span-method="objectSpanMethod"-->
    <template>
      <el-table-column
        v-for="(column,index) in headList"
        :key="index"
        :label="column.label"
        align="center"
        :fixed="column.gd && column.gd.indexOf(column.prop)>-1 "
        :width="column.width"
        show-overflow-tooltip
        :prop="column.prop"
        :sortable="(!(column.children && column.children.length !== 0 || column.label === '序号' || column.label === '操作' || column.label === '得分组成'))?'custom':false"
      >
        <template v-if="column.children && column.children.length !== 0">
          <el-table-column
            v-for="(column1,index1) in column.children"
            :key="index1"
            align="center"
            :label="column1.label"
            :width="column1.width"
            show-overflow-tooltip
            :prop="column1.prop"
            sortable="custom"
          >
            <template slot-scope="scope">
              <div v-if="(column1.hasOwnProperty('path')&&column1.path&&column1.path.length!==0)||(column1.hasOwnProperty('url')&&column1.url&&column1.url.length!==0)">
                <el-button
                  style="border: none;padding: 0;"
                  :style="[{color:column1.higlight!=='#464646'?column1.higlight:''}]"
                  class="cell_span"
                  :class="clickedRow==scope.row.index?'scale-row':''"
                  @click="toputh(column1,scope.row)"
                >{{ scope.row[(column1.prop+(column1.yl?column1.yl:''))] }}</el-button>
              </div>
              <div v-else>
                <span :style="[{color:column1.higlight}]">{{ scope.row[(column1.prop+(column1.yl?column1.yl:''))] }}</span>
              </div>
            </template>
          </el-table-column>
        </template>
        <template slot-scope="scope">
          <div v-if="(column.hasOwnProperty('path')&&column.path&&column.path.length!==0)||(column.hasOwnProperty('url')&&column.url&&column.url.length!==0)">
            <el-button
              style="border: none;padding: 0;"
              :style="[{color:column.higlight!=='#464646'?column.higlight:''}]"
              class="cell_span"
              :class="clickedRow==scope.row.index?'scale-row':''"
              @click="toputh(column,scope.row)"
            >{{ scope.row[(column.prop+(column.yl?column.yl:''))] }}</el-button>
          </div>
          <div v-else>
            <span :style="[{color:column.higlight}]">{{ scope.row[(column.prop+(column.yl?column.yl:''))] }}</span>
          </div>
        </template>
      </el-table-column>
    </template>>
  </el-table>
</template>

<script>
/**
   * @author wangligang
   * @date 2020/8/4
   * @Description:
   * @update by: 表格组件
   *
   *
   /

const evalData = (data) => {
  const tmp = []
  Array.from(data).forEach((row, index) => {
    if (!Array.isArray(row)) {
      tmp.push({ no: index + 1, ...row })
    } else {
      //
    }
  })
  return tmp
}

export default {
  props: {
    tableData: {
      type: [Array, Object],
      required: true
    },
    headList: {
      type: Array,
      default: () => []
    },
    query: {
      type: Object,
      default: () => {}
    },
    height: {
      type: String,
      default: () => 'calc(100vh - 220px)'
    },
    loading: {
      type: Boolean,
      default: () => false
    }
  },
  data() {
    return {
      clickedRow: -1,
      spanArr: [],
      pos: 0
    }
  },
  computed: {
    // 格式化表格数据
    tableDatas: function() {
      const tmp = !Array.isArray(this.tableData) ? [this.tableData] : this.tableData
      this.getSpanArr(evalData(tmp))
      return evalData(tmp)
    }
  },
  methods: {
    getSpanArr(data) {
      this.spanArr = []
      this.pos = 0
      for (var i = 0; i < data.length; i++) {
        if (i === 0) {
          this.spanArr.push(1)
          this.pos = 0
        } else {
          // 判断当前元素与上一个元素是否相同
          if (data[i].nsrsbh && data[i].nsrsbh === data[i - 1].nsrsbh) {
            this.spanArr[this.pos] += 1
            this.spanArr.push(0)
          } else {
            this.spanArr.push(1)
            this.pos = i
          }
        }
      }
    },
    dblclick(row, column, event) {
      let data = {}
      let Y = false
      this.headList.map(item => {
        if (item.label === '操作') {
          data = item
          Y = true
        } else if (item.url && item.url.length !== 0) {
          Y = true
        }
      })
      if (Y) {
        this.$emit('dblclick', row, data)
      }
    },
    toputh(row, data) {
      if (row.path && row.path.length !== 0) {
        this.$router.push({ path: row.path, query: { ...this.query }})
      } else if (row.url && row.url.length !== 0) {
        this.$emit('urlClick', row, data)
      }
    },
    scaleRow(row) {
      this.clickedRow = row.index
    },
    rowStyle({ rowIndex }) {
      if (this.clickedRow === rowIndex) {
        return 'scale-row'
      }
    },
    selectedRowStyle({ row, rowIndex }) {
      row.index = rowIndex
    },
    sortchange({ column, prop, order }) {
      this.$emit('sortchange', { column, prop, order })
    },
    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      if (!row.nsrsbh) {
        this.obj = { row, column, rowIndex, columnIndex }
        return {
          rowspan: 1,
          colspan: 1
        }
      }
      if (row.columnspan.indexOf(column.property) > -1) {
        const _row = this.spanArr[rowIndex]
        const _col = _row > 0 ? 1 : 0
        return {
          rowspan: _row,
          colspan: _col
        }
      }
    }
  }
}
</script>
<style lang="scss">
  .el-table{
    .scale-row{
      font-size:15px;
      font-weight:600;
      background-color:#f5f7fa;
      transition:all .3s ease-in-out;
    }
    .bg-disable{
      background:rgba(224,224,224,.3);
    }
    .bg-active{
      background: #bccbe4;
    }
  }
</style>

使用方式
引入组件

<HeadTable :headList="headList" :tableData="tableData"></HeadTable>

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue ElementUIVue.js 的 UI 组件库,是一款基于 Vue.js 的桌面端组件库。它提供了一些常用的 UI 组件,例如按钮、输入框、下拉列表、表格等。其中,表格是一种非常常见的组件,在数据展示和数据编辑上都有着重要的作用。 在使用 Vue ElementUI 编辑表格时,我们通常需要以下步骤: 1. 安装 Vue ElementUI 首先,我们需要使用 npm 安装 Vue ElementUI: ``` npm i element-ui -S ``` 2. 引入 Vue ElementUI 在需要使用 Vue ElementUI 的组件中引入 Vue ElementUI: ``` import Vue from 'vue'; import ElementUI from 'element-ui'; import 'element-ui/lib/theme-chalk/index.css'; Vue.use(ElementUI); ``` 3. 使用表格组件 Vue ElementUI 提供了一个非常方便的表格组件,我们只需要配置表格的列和数据,表格就可以自动生成。同时,表格支持编辑、排序、分页等常见操作。 ``` <el-table :data="tableData"> <el-table-column label="姓名" prop="name"></el-table-column> <el-table-column label="年龄" prop="age"></el-table-column> <el-table-column label="地址" prop="address"></el-table-column> <el-table-column label="操作" width="150"> <template slot-scope="scope"> <el-button size="mini" @click="edit(scope.$index, scope.row)">编辑</el-button> <el-button size="mini" type="danger" @click="del(scope.$index)">删除</el-button> </template> </el-table-column> </el-table> ``` 如上所示,我们只需要传入一个数组(tableData),以及配置每一列的 prop 和 label,就可以生成一个表格。同时,我们还可以在某一列中使用 slot,进行自定义操作,例如上面的“编辑”和“删除”按钮。 4. 编辑数据 在表格中编辑数据通常有两种方式:单元格编辑和行编辑。单元格编辑是指用户点击单元格后,编辑内容并保存;行编辑是指用户点击编辑按钮后,进入一条数据的编辑页面。 对于单元格编辑,Vue ElementUI 提供了 el-table-editable 组件: ``` <el-table :data="tableData"> <el-table-column label="姓名" prop="name" editable></el-table-column> <el-table-column label="年龄" prop="age" editable></el-table-column> <el-table-column label="地址" prop="address" editable></el-table-column> </el-table> ``` 而对于行编辑,则需要自行实现一个编辑页面,例如使用 Dialog 组件等。一般来说,编辑页面会接收一个具体的数据项作为 prop,并在界面中展示该数据项的各个字段,允许用户进行修改。 综上所述,Vue ElementUI 编辑表格具有简单易用、配置灵活等特点,是一款非常实用的组件库,广泛应用于数据管理、后台管理等多个领域。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值