关于elementUi表格合并行数据并展示序号

效果如下:属于同一个厂商的数据要合并成一行

-1

element官网对于合并行和列是这样说的:

通过给table传入span-method方法可以实现合并行或列,方法的参数是一个对象,里面包含当前行row、当前列column、当前行号rowIndex、当前列号columnIndex四个属性。该函数可以返回一个包含两个元素的数组,第一个元素代表rowspan,第二个元素代表colspan。 也可以返回一个键名为rowspancolspan的对象。

实现合并行思路:需要一个数据来记录需要合并的行数据(数字几就代表合并几行,比如 [1, 2, 0, 1]就是第一行第四行不变,第二三行合并成一行),还要有一个变量来记录数组下标。 注意:后台返回的数据一定要有能区分唯一性的数据,来判断前后两条数据是否一样。 主要代码如下:

  1.      <basetable
  2.          :tabledata=“tableData”
  3.          :tabletitle=“tableTitle”
  4.          :spanmethod=“objectSpanMethod”
  5.          maxheight=“600px”
  6.          vbind=“$attrs”
  7.      >
  8.          <template slotscope=“scope” slot=“serialNo”>
  9.          {{ scope.row.serialNo }}
  10.          </template>
  11.          ….
  12.      <;/basetable>
  1. const tableTitle = [
  2.      {
  3.      key: ‘serialNo’,
  4.      title: ‘序号’,
  5.      align: ‘center’,
  6.      width: ‘100’,
  7.      scopedSlots: { customRender: ‘serialNo’ }
  8.      },
  9.      {
  10.      key: ‘unionList’,
  11.      title: ‘厂商名称(编号)’,
  12.      align: ‘center’,
  13.      width: ‘300px’,
  14.      scopedSlots: { customRender: ‘unionList’ }
  15.      },
  16.      {
  17.      key: ‘unionName’,
  18.      title: ‘MQ厂商’,
  19.      tooltip: true,
  20.      align: ‘center’
  21.      },
  22. ]
  23. export default {
  24.      data() {
  25.      return {
  26.          tableTitle,
  27.          tableData: [],
  28.          spanArr: [], // 存合并行数据的数组
  29.          pos: 0,// 合并行数据数组下标
  30.          rowIndex: 1 // 序号
  31.      }
  32.      },
  33.  
  34.      methods: {
  35.      getTableData() {
  36.          this.tableData = [
  37.          {
  38.              AccessId: ‘1’,
  39.              id: 1,
  40.              mqPassword: ‘1011’,
  41.              privateKey: ‘1011’,
  42.              publicKey: ‘1011’,
  43.              unionList: “[{\”union_name\”:\”乐乐\”,\”union_id\”:\”200160\”}]”,
  44.              unionName: ‘1011’
  45.          },
  46.          {
  47.              accessId: ‘2’,
  48.              id: 2,
  49.              mqPassword: ‘1012’,
  50.              privateKey: ‘1012’,
  51.              publicKey: ‘1012’,
  52.              unionList: “[{\”union_name\”:\”小太阳\”,\”union_id\”:\”200734\”},{\”union_name\”:\”包子\”,\”union_id\”:\”200737\”}]”,
  53.              unionName: ‘1012’
  54.          },
  55.          {
  56.              accessId: ‘3’,
  57.              id: 3,
  58.              mqPassword: ‘1013’,
  59.              privateKey: ‘1013’,
  60.              publicKey: ‘1013’,
  61.              unionList: “[{\”union_name\”:\”小太阳\”,\”union_id\”:\”200734\”},{\”union_name\”:\”包子\”,\”union_id\”:\”200737\”}]”,
  62.              unionName: ‘1013’
  63.          },
  64.          {
  65.              accessId: ‘4’,
  66.              id: 4,
  67.              mqPassword: ‘1014’,
  68.              privateKey: ‘1014’,
  69.              publicKey: ‘1014’,
  70.              unionList: “[{\”union_name\”:\”测试\”,\”union_id\”:\”200160\”}]”,
  71.              unionName: ‘1014’
  72.          },
  73.          ]
  74.          this.getSpanArr(this.tableData) // 获取合并单元格数据和序号
  75.      },
  76.      getSpanArr(data) {
  77.      // 重新查询后,要清空行数据数组、序号重置为1
  78.          this.spanArr = []
  79.          this.rowIndex = 1
  80.          // 遍历数据,判断前后两条数据是否相同
  81.          for (let i = 0; i < data.length; i++) {
  82.          data[i].unionList = jsON.parse(data[i].unionList)
  83.          data[i].unionArr = data[i].unionList.map(=> i.union_id).join(‘,’)
  84.          if (=== 0) {
  85.              this.spanArr.push(1)
  86.              this.pos = 0
  87.              data[i].serialNo = this.rowIndex
  88.              this.rowIndex++
  89.          } else {
  90.              // 判断当前元素与上一元素是否相同
  91.              if (data[i].unionArr === data[ 1].unionArr) {
  92.              this.spanArr[this.pos] += 1
  93.              this.spanArr.push(0)
  94.              } else {
  95.              this.spanArr.push(1)
  96.              this.pos = i
  97.              data[i].serialNo = this.rowIndex
  98.              this.rowIndex ++
  99.              }
  100.          }
  101.          }
  102.      },
  103.      objectSpanMethod({ row, column, rowIndex, columnIndex }) {
  104.          // 合并操作和厂商名称
  105.          if (columnIndex === 0 || columnIndex === 1 || columnIndex === 2) {
  106.          const _row = this.spanArr[rowIndex]
  107.          const _col = _row > 0 ? 1 : 0
  108.          // rowspan和colspan都为0,则表示这一行不显示,[x, 1]则表示合并了多少行
  109.          return {
  110.              rowspan: _row,
  111.              colspan: _col
  112.          }
  113.          }
  114.      },
  115.      }
  116. }
  117.  

到此这篇关于关于elementUi表格合并行数据并展示序号的文章就介绍到这了,更多相关elementUi表格合并内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

标签

发表评论