a-table手动分页,指定列相同内容的行合并

<template>
  <a-table
    bordered
    :columns="columns"
    :dataSource="paginatedData"
    :pagination="pagination"
    @change="handleTableChange"
  >
    <span slot="action" slot-scope="text,record">
      <a-input-number v-model="record.amount" :min="0"/>
    </span>
  </a-table>
</template>
 
<script>
export default {
  props: {
    tableData: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      columns: [
        {
          'amount': 500, // 分
          'region_ids': [1, 2, 3, 5],
          'week_times': [{
            'weeks': ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
            'time_ranges': [
              { 'start_time': '00:00:00', 'end_time': '12:59:59' }
            ] }],
          'sku_id': 111,
          'price': 100, // 分
          'product_id': 2222,
          'product_name': 'xxxx'
        },
        {
          'amount': 444,
          'region_ids': [4, 6, 7],
          'week_times': [{
            'weeks': ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
            'time_ranges': [{ 'start_time': '13:00:00', 'end_time': '23:59:59' }]
          }],
          'sku_id': 111,
          'price': 100, // 分
          'product_id': 2222,
          'product_name': 'xxxx'
        },
        {
          'amount': 444,
          'region_ids': [4, 6, 7],
          'week_times': [{
            'weeks': ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
            'time_ranges': [{ 'start_time': '13:00:00', 'end_time': '23:59:59' }]
          }],
          'sku_id': 111,
          'price': 100, // 分
          'product_id': 2222,
          'product_name': 'xxxx'
        },
        {
          'amount': 444,
          'region_ids': [4, 6, 7],
          'week_times': [{
            'weeks': ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
            'time_ranges': [{ 'start_time': '13:00:00', 'end_time': '23:59:59' }]
          }],
          'sku_id': 111,
          'price': 100, // 分
          'product_id': 2222,
          'product_name': 'xxxx'
        },
        {
          'amount': 444,
          'region_ids': [4, 6, 7],
          'week_times': [{
            'weeks': ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
            'time_ranges': [{ 'start_time': '13:00:00', 'end_time': '23:59:59' }]
          }],
          'sku_id': 111,
          'price': 100, // 分
          'product_id': 2222,
          'product_name': 'xxxx'
        },
        {
          'amount': 444,
          'region_ids': [4, 6, 7],
          'week_times': [{
            'weeks': ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
            'time_ranges': [{ 'start_time': '13:00:00', 'end_time': '23:59:59' }]
          }],
          'sku_id': 111,
          'price': 100, // 分
          'product_id': 2222,
          'product_name': 'xxxx'
        },
        {
          'amount': 444,
          'region_ids': [4, 6, 7],
          'week_times': [{
            'weeks': ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
            'time_ranges': [{ 'start_time': '13:00:00', 'end_time': '23:59:59' }]
          }],
          'sku_id': 111,
          'price': 100, // 分
          'product_id': 2222,
          'product_name': 'xxxx'
        },
        {
          'amount': 444,
          'region_ids': [4, 6, 7],
          'week_times': [{
            'weeks': ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
            'time_ranges': [{ 'start_time': '13:00:00', 'end_time': '23:59:59' }]
          }],
          'sku_id': 111,
          'price': 100, // 分
          'product_id': 2222,
          'product_name': 'xxxx'
        },
        {
          'amount': 444,
          'region_ids': [4, 6, 7],
          'week_times': [{
            'weeks': ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
            'time_ranges': [{ 'start_time': '13:00:00', 'end_time': '23:59:59' }]
          }],
          'sku_id': 111,
          'price': 100, // 分
          'product_id': 2222,
          'product_name': 'xxxx'
        },
        {
          'amount': 444,
          'region_ids': [4, 6, 7],
          'week_times': [{
            'weeks': ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
            'time_ranges': [{ 'start_time': '13:00:00', 'end_time': '23:59:59' }]
          }],
          'sku_id': 111,
          'price': 100, // 分
          'product_id': 2222,
          'product_name': 'xxxx'
        }],
      data: [],
      pagination: {
        current: 1,
        pageSize: 4,
        total: 0
      }
    }
  },
  computed: {
    paginatedData() {
      const { current, pageSize } = this.pagination
      const start = (current - 1) * pageSize
      const end = start + pageSize
      return this.data.slice(start, end)
    }
  },

  async mounted() {
   if(this.tableData.length>0){
       this.data = this.tableData
    }
    this.data.forEach(item => {
      const weeksArr = item.week_times[0].weeks.map(item => this.getWeekText(item))
      const text = weeksArr.length === 8 ? '全部周期' : weeksArr.join(',')
      item.weekText = text
    })
    //将名字相同周期相同的放在一起
    this.data.sort((a, b) => {
      if (a.product_name === b.product_name && a.weekText.length === b.weekText.length) {
        return 0
      } else if (a.product_name === b.product_name) {
        return a.weekText.length - b.weekText.length
      }
      return a.product_name > b.product_name ? 1 : -1
    })
    // console.log('8888888data', this.data)
    this.pagination.total = this.data.length
    await this.init()
  },

  methods: {
     init() {
      this.getColumns()
    },

    renderContent(value, row, index, type) {
      const obj = {
        children: value,
        attrs: { rowSpan: 0 }
      }
      if (this.paginatedData.length > 1) {
        if (index === 0) {
          obj.attrs.rowSpan = (value === this.paginatedData[index + 1][type] && row.product_name === this.paginatedData[index + 1]['product_name']) ? this.getRowSpanNum(value, type, row, 0) : 1
        } else {
          obj.attrs.rowSpan = (value === this.paginatedData[index - 1][type] && row.product_name === this.paginatedData[index - 1]['product_name']) ? 0 : this.getRowSpanNum(value, type, row, index)
        }
      } else {
        obj.attrs.rowSpan = 1
      }
      
      return obj
    },


    getRowSpanNum (value, type, row, index) {
      const newPaginatedData = this.paginatedData.slice(index)
      let num = 0
      for (let i = 0; i < newPaginatedData.length; i++) {
        if (value === newPaginatedData[i][type] && row.product_name === newPaginatedData[i]['product_name']) {
          num += 1
        } else {
          break
        }
      }
      return num
    },

    handleTableChange(pagination) {
      this.pagination = pagination
    },
    
    getWeekText (value) {
      switch (value) {
        case 'Mon':
          return '周一'
        case 'Tue':
          return '周二'
        case 'Wed' :
          return '周三'
        case 'Thu' :
          return '周四'
        case 'Fri' :
          return '周五'
        case 'Sat' :
          return '周六'
        case 'Sun' :
          return '周日'
        default:
          return ''
      }
    },

    getColumns() {
      const columns = [{
        title: '商品名称',
        dataIndex: 'product_name',
        align: 'center',
        customRender: (value, data, index) => {
          return this.renderContent(value, data, index, 'product_name')
        }
      }, {
        title: '价格',
        dataIndex: 'price',
        align: 'center',
        customRender: (value, data, index) => {
          return this.renderContent(value, data, index, 'price')
        }
      },
      {
        title: '周期',
        align: 'center',
        // scopedSlots: { customRender: 'weekTimes' }
        dataIndex: 'weekText',
        customRender: (value, data, index) => {
          return this.renderContent(value, data, index, 'weekText')
        }
      }, {
        title: '时段',
        dataIndex: 'time_ranges',
        align: 'center',
        customRender: (value, data) => {
          return `${data.week_times[0].time_ranges[0].start_time}~${data.week_times[0].time_ranges[0].end_time}`
        }
      },{
        title: '现抽金额',
        align: 'center',
        width: 200,
        dataIndex: 'amount',
        scopedSlots: { customRender: 'action' }
      }]

      this.columns = columns
    }
  }
}
</script>

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
为了在el-table合并指定内容相同,可以使用el-table的span-method属性。该属性允许我们自定义单元格的合并方式。下面是一个例子,假设我们要合并第一内容相同: ```html <template> <el-table :data="tableData" :span-method="handleSpanMethod"> <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> </template> <script> export default { data() { return { tableData: [ { name: '张三', age: 18, address: '北京市海淀区' }, { name: '李四', age: 20, address: '北京市海淀区' }, { name: '王五', age: 22, address: '北京市朝阳区' }, { name: '赵六', age: 24, address: '北京市朝阳区' } ] }; }, methods: { handleSpanMethod({ row, column, rowIndex, columnIndex }) { if (columnIndex === 0) { // 如果是第一 if (rowIndex === 0) { // 如果是第一 return { rowspan: this.getRowspan(row.name, columnIndex), colspan: 1 }; } else { // 如果不是第一 if (row.name === this.tableData[rowIndex - 1].name) { // 如果和上一的name相同 return { rowspan: 0, colspan: 0 }; } else { // 如果和上一的name不同 return { rowspan: this.getRowspan(row.name, columnIndex), colspan: 1 }; } } } else { // 如果不是第一 return { rowspan: 1, colspan: 1 }; } }, getRowspan(name, columnIndex) { // 获取需要合并数 let rowspan = 1; for (let i = 0; i < this.tableData.length; i++) { if (i !== 0 && this.tableData[i].name !== this.tableData[i - 1].name) { rowspan = 1; } if (this.tableData[i].name === name && i !== columnIndex) { rowspan++; } } return rowspan; } } }; </script> ``` 在上面的例子中,我们使用了handleSpanMethod方法来自定义单元格的合并方式。如果是第一,我们需要判断当前和上一的name是否相同,如果相同则返回{ rowspan: 0, colspan: 0 },表示当前单元格不需要渲染。如果不相同,则需要调用getRowspan方法获取需要合并数,并返回{ rowspan: rowspan, colspan: 1 }。如果不是第一,则直接返回{ rowspan: 1, colspan: 1 }。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值