欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

element-ui中Table表格省市区合并单元格的方法实现

程序员文章站 2022-09-05 21:14:44
本文介绍了element-ui中table表格省市区合并单元格的方法实现,分享给大家,具体如下: 效果如图 代码如下:

本文介绍了element-ui中table表格省市区合并单元格的方法实现,分享给大家,具体如下:

效果如图

element-ui中Table表格省市区合并单元格的方法实现

代码如下:

<template>
 <div>
  <el-form :inline="true" :model="forminline" class="demo-form-inline">
   <el-form-item label="搜索">
    <el-input v-model="forminline.search" placeholder="搜索"></el-input>
   </el-form-item>
   <el-form-item>
    <el-button type="primary" @click="onsubmit">查询</el-button>
   </el-form-item>
  </el-form>
  <el-table
   :data="tabledata"
   border
   v-loading="loading"
   element-loading-text="拼命加载中"
   element-loading-spinner="el-icon-loading"
   element-loading-background="rgba(0, 0, 0, 0.8)"
   :span-method="arrayspanmethod"
   style="width: 100%">
   <el-table-column
    prop="province"
    label="省"
    width="150">
   </el-table-column>
   <el-table-column
    prop="city"
    label="市"
    width="150">
   </el-table-column>
   <el-table-column
    prop="zone"
    label="区"
    width="150">
   </el-table-column>
   <el-table-column
    prop="remake"
    label="备注">
    <template slot-scope="scope">
     <template v-if="scope.row.edit">
      <el-input class="edit-input" size="small" v-model="scope.row.remake"></el-input>
     </template>
     <span v-else>{{ scope.row.remake }}</span>
    </template>
   </el-table-column>
   <el-table-column
    prop="publicsubsidy"
    sortable
    label="国补"
    width="150">
    <template slot-scope="scope">
     <template v-if="scope.row.edit">
      <el-input class="edit-input" size="small" v-model="scope.row.publicsubsidy"></el-input>
     </template>
     <span v-else>{{ scope.row.publicsubsidy }}</span>
    </template>
   </el-table-column>
   <el-table-column
    prop="provincesubsidy"
    sortable
    label="省补"
    width="150">
    <template slot-scope="scope">
     <template v-if="scope.row.edit">
      <el-input class="edit-input" size="small" v-model="scope.row.provincesubsidy"></el-input>
     </template>
     <span v-else>{{ scope.row.provincesubsidy }}</span>
    </template>
   </el-table-column>
   <el-table-column
    prop="citysubsidy"
    sortable
    label="市补"
    width="150">
    <template slot-scope="scope">
     <template v-if="scope.row.edit">
      <el-input class="edit-input" size="small" v-model="scope.row.citysubsidy"></el-input>
     </template>
     <span v-else>{{ scope.row.citysubsidy }}</span>
    </template>
   </el-table-column>
   <el-table-column align="center" label="actions" width="200">
    <template slot-scope="scope">
     <el-button v-if="scope.row.edit" type="success" @click="confirmedit(scope.row)" size="small"
           icon="el-icon-circle-check-outline">ok
     </el-button>
     <el-button v-if="scope.row.edit" class='cancel-btn' size="small" icon="el-icon-refresh" type="warning"
           @click="canceledit(scope.row)">cancel
     </el-button>
     <el-button v-else type="primary" @click='scope.row.edit=!scope.row.edit' size="small" icon="el-icon-edit">
      edit
     </el-button>
    </template>
   </el-table-column>
  </el-table>
 </div>
</template>
<script>
 import axios from 'axios'
 export default {
  name: "city",
  data() {
   return {
    tabledata: [], //table的数据
    originaldata: [],//table数据备份
    provincearr: [], //省份要合并数组 [2,0,1,3,0,0] 代表第一二行合并,第三行不变,第四五六行合并,0代表原本的那一行被合并,因此这个列被消除
    provincepos: 0, //省份要合并数组内容的序号
    cityarr: [], //同上 市
    citypos: [],
    zonearr: [],//同上 区
    zonepos: [],
    forminline: { //form表单
     search: ''
    },
    loading: false
   }
  },
  created() {
   this.init()
  },
  methods: {
   init() {
    this.loading = true;
    axios.get('./static/table.json')
     .then(res => {
      this.loading = false;
      this.tabledata = res.data.map((v, index) => {
       this.$set(v, 'edit', false) //增加一个新的属性

       //可以修改的属性值,进行添加一个对应的原本值
       v.originalremake = v.remake;
       v.originalpublics = v.publicsubsidy;
       v.originalprovinces = v.provincesubsidy;
       v.originalcitys = v.citysubsidy;
       return v
      })
      this.originaldata = this.tabledata;
      this.merage() //合并的方法
     })
     .catch(e => {
      console.log(e)
     })
   },
   canceledit(row) {
    //取消编辑,把原本值进行覆盖回来
    row.remake = row.originalremake
    row.publicsubsidy = row.originalpublics
    row.provincesubsidy = row.originalprovinces
    row.citysubsidy = row.originalcitys
    row.edit = false
    this.$message({
     message: 'the title has been restored to the original value',
     type: 'warning'
    })
   },
   confirmedit(row) {
    row.edit = false

    //把新的值,覆盖原本值,以防再次修改
    row.originalremake = row.remake
    row.originalpublics = row.publicsubsidy
    row.originalprovinces = row.provincesubsidy
    row.originalcitys = row.citysubsidy
    this.$message({
     message: 'the title has been edited',
     type: 'success'
    })
   },
   arrayspanmethod({row, column, rowindex, columnindex}) {
    if (columnindex === 0) {
     //第一列的合并方法,省
     const _row_1 = this.provincearr[rowindex];
     const _col_1 = _row_1 > 0 ? 1 : 0; //如果被合并了_row=0则它这个列需要取消
     return {
      rowspan: _row_1,
      colspan: _col_1
     }
    } else if (columnindex === 1) {
     //第二列的合并方法,市
     const _row_2 = this.cityarr[rowindex];
     const _col_2 = _row_2 > 0 ? 1 : 0;
     return {
      rowspan: _row_2,
      colspan: _col_2
     }
    } else if (columnindex === 2) {
     //第三列的合并方法,区
     const _row_3 = this.zonearr[rowindex];
     const _col_3 = _row_3 > 0 ? 1 : 0;
     return {
      rowspan: _row_3,
      colspan: _col_3
     }
    }
   },
   merage() {
    //要合并的数组的方法
    this.merageinit();
    for (var i = 0; i < this.tabledata.length; i++) {
     if (i === 0) {
      //第一行必须存在
      this.provincearr.push(1);
      this.provincepos = 0;

      this.cityarr.push(1);
      this.citypos = 0;
      this.zonearr.push(1);
      this.zonepos = 0;
     }
     else {
      // 判断当前元素与上一个元素是否相同 this.provincepos是provincearr内容的序号

      //省
      if (this.tabledata[i].province === this.tabledata[i - 1].province) {
       this.provincearr[this.provincepos] += 1;
       this.provincearr.push(0);
      } else {
       this.provincearr.push(1);
       this.provincepos = i;
      }

      //市
      if (this.tabledata[i].city === this.tabledata[i - 1].city && this.tabledata[i].province === this.tabledata[i - 1].province) {
       this.cityarr[this.citypos] += 1;
       this.cityarr.push(0);
      } else {
       this.cityarr.push(1);
       this.citypos = i;
      }

      //区
      if (this.tabledata[i].zone === this.tabledata[i - 1].zone && this.tabledata[i].city === this.tabledata[i - 1].city && this.tabledata[i].province === this.tabledata[i - 1].province) {
       this.zonearr[this.zonepos] += 1;
       this.zonearr.push(0);
      } else {
       this.zonearr.push(1);
       this.zonepos = i;
      }
     }
    }
   },
   merageinit() {
    //初始化省市区的合并行的数组
    this.provincearr = [];
    this.provincepos = 0;
    this.cityarr = [];
    this.citypos = 0;
    this.zonearr = [];
    this.zonepos = 0;
   },
   onsubmit() {
    const filterdata = this.originaldata; //每次过滤之前都要把筛选之前的tabledata重置
    this.tabledata = filterdata.filter(value => {
     if (this.forminline.search === value.province || this.forminline.search === value.city || this.forminline.search === value.zone) {
      return value;
     } else if (this.forminline.search === '') {
      return value;
     } else if (value.province.includes(this.forminline.search) || value.city.includes(this.forminline.search) || value.zone.includes(this.forminline.search)) {
      return value;
     }
    })
    this.merage();
   }
  }
 }
</script>
<style scoped>
</style>

static文件下的table.json

[
 {
  "province": "浙江省",
  "city": "杭州市",
  "zone": "余杭区",
  "type": "icbc",
  "remake": "2017-2018年期间建成并网的分布式光伏",
  "publicsubsidy": "0.37",
  "provincesubsidy": "0.1",
  "citysubsidy": "0.1"
 },
 {
  "province": "浙江省",
  "city": "杭州市",
  "zone": "余杭区",
  "type": "dwe",
  "remake": "对居民住宅单独建设的光伏发电项目",
  "publicsubsidy": "0.37",
  "provincesubsidy": "0.1",
  "citysubsidy": "0."
 },
 {
  "province": "浙江省",
  "city": "杭州市",
  "zone": "萧山区",
  "type": "dwe",
  "remake": "对居民住宅单独建设的光伏发电项目",
  "publicsubsidy": "0.37",
  "provincesubsidy": "0.1",
  "citysubsidy": "0."
 },
 {
  "province": "安徽省",
  "city": "阜阳市",
  "zone": "太和县",
  "type": "all",
  "remake": "对居民住宅单独建设的光伏发电项目",
  "publicsubsidy": "0.37",
  "provincesubsidy": "0.2",
  "citysubsidy": "0.1"
 },
 {
  "province": "安徽省",
  "city": "合肥市",
  "zone": "蜀山区",
  "type": "all",
  "remake": "对居民住宅单独建设的光伏发电项目",
  "publicsubsidy": "0.37",
  "provincesubsidy": "0.2",
  "citysubsidy": "0.1"
 },
 {
  "province": "安徽省",
  "city": "合肥市",
  "zone": "庐阳区",
  "type": "all",
  "remake": "对居民住宅单独建设的光伏发电项目",
  "publicsubsidy": "0.37",
  "provincesubsidy": "0.2",
  "citysubsidy": "0.1"
 },
 {
  "province": "浙江省",
  "city": "杭州市",
  "zone": "西湖区",
  "type": "all",
  "remake": "2017-2018年期间建成并网的分布式光伏",
  "publicsubsidy": "0.37",
  "provincesubsidy": "0.1",
  "citysubsidy": "0.2"
 },
 {
  "province": "浙江省",
  "city": "嘉兴市",
  "zone": "海盐县",
  "type": "all",
  "remake": "对居民住宅单独建设的光伏发电项目",
  "publicsubsidy": "0.37",
  "provincesubsidy": "0.2",
  "citysubsidy": "0.1"
 }
]

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。