Vue Elementui Table 列项求和(分页和和总和)

Vue Elementui Table 列项求和(表格底部)分页求和 和 总和 希望可以对你开发有帮助

大家也都知道, Elementui中有其组件进行分页求和如下演示:

 在底部进行合计:

上源码:

<el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange" :cell-style="cellStyle"
          :summary-method="getSummaries" show-summary >

:summary-method="getSummaries" show-summary   这两个属性必须加上!切记 

对求和的数据进行了一番整理: 

    // 求和操作
    getSummaries(param) {
      const { columns, data } = param
      const sums = []
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '合计:'
        }
        if (index === 8) {
          sums[index] = '总记录数:' + this.total
        }
          // index >= 5 || index <= 7 是你需要计算的列
          // 当然也可以指定一个或者多个列
        // index === 2 || index === 3 这样值求和第二个与第三个列

        else if (index >= 5 && index <= 7) {
          const values = data.map(item => Number(item[column.property]))
          if (!values.every(value => isNaN(value))) {
            sums[index] = values.reduce((prev, curr) => {
              const value = Number(curr)
              if (!isNaN(value)) {
                // 保存了两位小数点
                return Math.floor((prev + curr) * 100) / 100;
              } else {
                // 保存了两位小数点
                return Math.floor(prev * 100) / 100;
              }
            }, 0)
            sums[index] += ' 元';
          } else {
            sums[index] = 'N/A'
          }
        } else {
          // sums[index] = ''
        }
      })
      return sums
    },

 

这个是分页求和

很多人想实现求和不受分页的限制这个前端好像暂时无法办到,只能后端进行求和后传递到前端

小贴一段求和sql吧

select
        count(1) as totalRecord,
        IFNULL(SUM(r.pay_price),0) as payPriceSum
        from XXXX(表名) r
        left join client_user c on c.id = r.user_id
<where>
    // 中间可以是你的筛选条件
</where>

 这个是进行总的求和的操作,不过类似于preValueSum等一些属性是后台传递过来的!

    // 求和操作
    getSummaries(param) {
      const { columns, data } = param
      const sums = []
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '合计:'
        }
        if (index === 8) {
          sums[index] = '总记录数:' + this.total
        }

          // 指定一个或者多个列
        // index === 2 || index === 3 这样值求和第二个与第三个列

        else if (index === 5) {
          sums[index] = this.preValueSum + ' 元'
        } else if (index === 6) {
          sums[index] = this.servicePriceSum + ' 元'
        } else if (index === 7) {
          sums[index] = this.realPriceSum + ' 元'
        }
   
      })
      return sums
    },

  

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

学无丿止境

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值