vue el-table的合并单元格与拆分单元格

vue el-table的合并单元格与拆分单元格

 methods: {
    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      const fields = ['stritemname', 'stritemstyle', 'strmadefactname', 'strpurchaseunit', 'dblquantity']
      if (fields.includes(column.property)) {
        const cellValue = row['lngitemid']
        if (cellValue && fields.includes(column.property)) {
          const prevRow = this.tableData[rowIndex - 1]
          let nextRow = this.tableData[rowIndex + 1]
          if (prevRow && prevRow['lngitemid'] === cellValue) {
            return { rowspan: 0, colspan: 0 }
          } else {
            // return { rowspan: row.rowspan, colspan: 1 }
            let countRowspan = 1
            while (nextRow && nextRow['lngitemid'] === cellValue) {
              nextRow = this.tableData[++countRowspan + rowIndex]
            }
            if (countRowspan > 1) {
              return { rowspan: countRowspan, colspan: 1 }
            }
          }
        }
      }
    },
}


   <!-- 拆分表格 -->
     <el-table
        :data="tableData"
        :border="true"
        row-key="id"
        :key="tableKey"
      >
        <el-table-column
          fixed="left"
          type="index"
          :label="$t('public_label.index')"
          width="70"
        >
        </el-table-column>

        <el-table-column
          v-for="(item, index) in checkeds"
          :key="`col_${index}`"
          :prop="tableOptions[index].value"
          :label="tableOptions[index].label"
          :width="tableOptions[index].value == 'backupFileName'?'490px':''"
        >
          <template slot-scope="scope">
            <span v-if="tableOptions[index].value == 'useStatus'">
              {{ scope.row.useStatus.toString().split("\n")[0] == 101 ? $t("mes_public_label.Successed") : $t("mes_public_label.Failed")}}
               <el-divider direction="horizontal"></el-divider>
              {{
              scope.row.useStatus.toString().split("\n")[1] == '101'
              ? $t("mes_public_label.Successed") :
              scope.row.useStatus.toString().split("\n")[1] == null ? '-' : $t("mes_public_label.Failed")}}
            </span>

            <span v-else-if="tableOptions[index].value == 'backupType'">
              {{ scope.row.backType == 0 ? "差异备份" : "完全备份" }}
            </span>

            <span v-else-if="tableOptions[index].value == 'backupFileName'" width="1222px">
              {{ scope.row.backupFileName.toString().split("\n")[0]}}
               <el-divider direction="horizontal"></el-divider>
              {{ scope.row.backupFileName.toString().split("\n")[1] == null ? '-' : scope.row.backupFileName.toString().split("\n")[1] }}
            </span>
            <span v-else-if="tableOptions[index].value == 'fileNumber'">
              {{ scope.row.fileNumber.toString() == '' ? '-' :  scope.row.fileNumber.toString().split("\n")[0] }}
               <el-divider direction="horizontal"></el-divider>
              {{ scope.row.fileNumber.toString() == '' ? '-' :  scope.row.fileNumber.toString().split("\n")[0] }}
            </span>
            <span v-else-if="tableOptions[index].value == 'backupFileNumber'">
              {{ scope.row.backupFileNumber.toString() == '' ? '-' :  scope.row.backupFileNumber.toString().split("\n")[0] }}
               <el-divider direction="horizontal"></el-divider>
              {{ scope.row.backupFileNumber.toString() == '' ? '-' :  scope.row.backupFileNumber.toString().split("\n")[0] }}
            </span>
            <span v-else-if="tableOptions[index].value == 'backupFileCapacity'">
              {{ scope.row.backupFileCapacity.toString() == '' ? '-' :  scope.row.backupFileCapacity.toString().split("\n")[0] }}
               <el-divider direction="horizontal"></el-divider>
              {{ scope.row.backupFileCapacity.toString() == '' ? '-' :  scope.row.backupFileCapacity.toString().split("\n")[1] }}
            </span>
            <span v-else>{{ scope.row[tableOptions[index].value] }}</span>
          </template>
        </el-table-column>

        <el-table-column
          fixed="right"
          :label="$t('public_label.operation')"
          width="220px"
        >
          <template slot-scope="scope">
            <!-- 删除、还原 -->
            <el-dropdown
              trigger="click"
              style="margin-right: 22px"
              v-if="
                $_has('deptQuery') ||
                $_has('deptEdit') ||
                $_has('deptDelete') ||
                $_has('deptEnable') ||
                $_has('deptDisable')
              "
            >
              <el-button type="primary">
                <span>{{ $t("public_btn_name.feature") }}</span>
                <i class="el-icon-arrow-down el-icon--right"></i>
              </el-button>
              <el-dropdown-menu slot="dropdown">
                <!-- <el-dropdown-item @click.native="toView(scope.row)" v-if="$_has('backupQuery')">{{$t('public_btn_name.view')}}</el-dropdown-item> -->
                <el-dropdown-item
                  @click.native="toDeleteBack(scope.row)"
                  v-if="$_has('backupDelete')"
                >
                  {{ $t("public_btn_name.delete") }}
                </el-dropdown-item>
                <el-dropdown-item
                  @click.native="toReback(scope.row)"
                  v-if="$_has('backupEnable')"
                >
                  {{ $t("mes_public_label.reback") }}
                </el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
### 回答1: el-tableElementUI 中的表格组件,它提供了一些功能来合并单元格。下面是一个简单的示例: ```html <template> <el-table :data="tableData" border style="width: 100%"> <el-table-column prop="name" label="姓名"></el-table-column> <el-table-column prop="age" label="年龄"></el-table-column> <el-table-column prop="address" label="地址"></el-table-column> <el-table-column label="操作"> <template slot-scope="scope"> <el-button size="mini" @click="handleDelete(scope.$index)">删除</el-button> </template> </el-table-column> </el-table> </template> <script> export default { data() { return { tableData: [ { name: '张三', age: 18, address: '北京市海淀区' }, { name: '李四', age: 20, address: '上海市黄浦区' }, { name: '王五', age: 22, address: '广州市天河区' }, { name: '赵六', age: 24, address: '深圳市南山区' }, { name: '孙七', age: 26, address: '杭州市西湖区' } ] } }, methods: { handleDelete(index) { this.tableData.splice(index, 1) } } } </script> ``` 上面的代码中,我们通过使用 `<el-table-column>` 组件的 `prop` 属性来定义数据源中的每列数据。我们可以通过设置 `span-method` 属性为一个函数来定义合并单元格的规则。例如,我们想要将第一列相同的单元格合并成一个单元格,可以这样写: ```html <el-table :data="tableData" border style="width: 100%"> <el-table-column prop="name" label="姓名" :span-method="nameSpanMethod"></el-table-column> <el-table-column prop="age" label="年龄"></el-table-column> <el-table-column prop="address" label="地址"></el-table-column> <el-table-column label="操作"> <template slot-scope="scope"> <el-button size="mini" @click="handleDelete(scope.$index)">删除</el-button> </template> </el-table-column> </el-table> ``` 然后,在 Vue 实例中,我们定义 `nameSpanMethod` 方法来实现单元格的合并: ```js methods: { nameSpanMethod({ row, column, rowIndex, columnIndex }) { if (columnIndex === 0) { if (rowIndex === 0) { return { rowspan: this.tableData.length, colspan: 1 } } else { return { rowspan: 0, colspan: 0 } } } }, handleDelete(index) { this.tableData.splice(index, 1) } } ``` 在 `nameSpanMethod` 方法中,我们根据当前单元格的位置来判断是否需要进行合并。如果当前单元格是第一列的第一个单元格,我们就返回一个对象,其中 `rowspan` 属性表示需要合并的行数,`colspan` 属性表示需要合并的列数。如果不需要合并,则返回 `{ rowspan: 0, colspan: 0 }`,这样就不会对单元格进行合并。 这样就实现了 el-table单元格合并功能。 ### 回答2: el-table是一个基于Vue.js的表格组件,可以帮助我们快速构建数据展示的表格。在el-table合并单元格通常需要通过使用span-method属性来实现。 span-method属性是一个函数,用来指定合并单元格的规则。我们需要在这个函数中判断当前单元格是否需要合并,并返回需要合并的行数和列数。 通过以下步骤可以实现el-table单元格合并: 1. 在el-table的<el-table-column>组件中设置span-method属性,将其指向我们自定义的函数。 2. 自定义一个函数,在使用el-table组件的父组件中定义。这个函数接收两个参数:row和column。row代表当前单元格所在的行对象,column代表当前单元格所在列对象。 3. 在自定义的函数中,我们可以根据需要判断是否要合并单元格。如果需要合并,就返回一个包含两个属性的对象:rowspan和colspan。rowspan代表要合并的行数,colspan代表要合并的列数。如果不需要合并,就返回一个没有任何属性的对象。 4. 最后,el-table会根据我们返回的对象,来对相应的单元格进行合并。 通过上述步骤,我们就可以实现在el-table合并单元格的功能。注意,这个函数可以根据具体的需求进行定制,可以根据单元格的内容、行号、列号等信息来判断是否需要合并单元格。 ### 回答3: el-tableElement UI中的一个表格组件,它提供了丰富的功能和易用的API来实现表格的展示和交互操作。在el-table合并单元格可以通过使用span-method属性来实现。 在el-table中,可以通过设置span-method属性来动态合并单元格。span-method属性是一个函数,它返回一个对象,对象中的属性设置了行合并和列合并的方式。具体的合并方式可以根据具体的需求来进行设置。 在span-method函数中,我们可以通过传入参数来获取当前行和当前列的数据,然后根据业务逻辑来判断是否需要合并单元格。如果需要合并单元格,我们可以返回一个对象,设置合并的行数和列数,如果不需要合并单元格,我们可以返回一个空对象。 具体的合并单元格的逻辑可以根据具体的需求来进行编写,比如我们可以根据某一列的值相同来合并单元格,或者根据某一列的值和下一行的值相同来合并单元格等等。 总之,利用el-table的span-method属性,我们可以很方便地实现单元格的合并操作,只需要根据具体的需求编写合适的合并逻辑即可。这样就可以在el-table中实现单元格的合并。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

勒布朗-前端

请多多支持,留点爱心早餐

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

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

打赏作者

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

抵扣说明:

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

余额充值