用vxe-table实现表尾计算数据并排序

 效果如下:

 最重要的属性配置

     show-footer //是否显示尾部
     :merge-footer-items="mergeFooterItems"  //表尾合并规则
      :footer-cell-class-name="footerCellClassName"//表尾样式
      :footer-method="footerMethod" //最重要的部分,计算表尾的数据并渲染到页面

 详细代码如下

html部分
<vxe-table
      show-footer  //展示尾部
      ref="xTableData"
      :print-config="{}"
      :merge-footer-items="mergeFooterItems"  //表尾合并规则
      :footer-cell-class-name="footerCellClassName"//表尾样式
      :footer-method="footerMethod" //最重要的部分,计算表尾的数据并渲染到页面
      :loading="loading"
      :data="tableData"
      :export-config="{}"
    >
</vxe-table>

js部分
  data() {
    return {
      mergeFooterItems: [
        { row: 0, col: 0, rowspan: 1, colspan: 2 },
        { row: 1, col: 0, rowspan: 1, colspan: 2 },
        { row: 2, col: 0, rowspan: 1, colspan: 2 },
        { row: 3, col: 0, rowspan: 1, colspan: 2 },
          ],
        }
    }


    footerMethod({ columns, data }) {
      const footerData = [
        columns.map((column, _columnIndex) => {
          if (_columnIndex === 0) {
            return "平均分";
          }
          for (let i = 2; i <= this.tableColumn.length + 2; i++) {
            if (_columnIndex === i) {
              return this.sumNum(data, column.property, "1");
            }
          }
          return null;
        }),
        columns.map((column, _columnIndex) => {
          if (_columnIndex === 0) {
            return "平均分";
          }
          for (let i = 2; i <= this.tableColumn.length + 2; i++) {
            if (_columnIndex === i) {
              return this.sumNum(data, column.property, "2");
            }
          }
          return null;
        }),
        columns.map((column, _columnIndex) => {
          if (_columnIndex === 0) {
            return "得分";
          }
          for (let i = 2; i <= this.tableColumn.length + 2; i++) {
            return this.meanNum(data, column.property);
          }
          return null;
        }),
      ];
      footerData.push(
        columns.map((column, _columnIndex) => {
          if (_columnIndex === 0) {
            return "排 序";
          }
          let footerData2 = this.vateUtils.judgeType.deepClone(footerData)[2];
          footerData2.splice(0, 2);
          let scoreSortList = [];
          footerData2.forEach((ele, index) => {
            scoreSortList.push({ [index.toString()]: ele });
          });
          for (let i = 2; i <= this.tableColumn.length + 2; i++) {
            if (_columnIndex === i) {
              return this.sortSore(scoreSortList, _columnIndex - 2);
            }
          }
        })
      );
      return footerData;
    },
    footerCellClassName({ $rowIndex, columnIndex }) {
      if (columnIndex === 0 || columnIndex === 1) {
        switch ($rowIndex) {
          case 3:
            return "col-stylethree col-styleone";  //在<style>标签中写class对应这里的字符串名
          default:
            return "col-styleone";
        }
      }
      if ($rowIndex === 3) {
        return "col-stylethree";
      }
    },

        footerMethod({ columns, data }) {
      const footerData = [
        columns.map((column, _columnIndex) => {
          if (_columnIndex === 0) {
            return "平均分";
          }
          for (let i = 2; i <= this.tableColumn.length + 2; i++) {
            if (_columnIndex === i) {
              return this.sumNum(data, column.property, "1");
            }
          }
          return null;
        }),
        columns.map((column, _columnIndex) => {
          if (_columnIndex === 0) {
            return "平均分";
          }
          for (let i = 2; i <= this.tableColumn.length + 2; i++) {
            if (_columnIndex === i) {
              return this.sumNum(data, column.property, "2");
            }
          }
          return null;
        }),
        columns.map((column, _columnIndex) => {
          if (_columnIndex === 0) {
            return "得分";
          }
          for (let i = 2; i <= this.tableColumn.length + 2; i++) {
            return this.meanNum(data, column.property);
          }
          return null;
        }),
      ];
      footerData.push(
        columns.map((column, _columnIndex) => {
          if (_columnIndex === 0) {
            return "排 序";
          }
          let footerData2 = this.vateUtils.judgeType.deepClone(footerData)[2];
          footerData2.splice(0, 2);
          let scoreSortList = [];
          footerData2.forEach((ele, index) => {
            scoreSortList.push({ [index.toString()]: ele });
          });
          for (let i = 2; i <= this.tableColumn.length + 2; i++) {
            if (_columnIndex === i) {
              return this.sortSore(scoreSortList, _columnIndex - 2);
            }
          }
        })
      );
      return footerData;
    },
        meanNum(list, field) {
      let count = 0;
      list.forEach((item) => {
        if (item[field] == undefined) {
          count += 0;
        } else {
          count += Number(item[field]);
        }
      });
      return (count / (list.length / 2)).toFixed(2);
    },
    sumNum(list, field, bidtype) {
      let count = 0;
      list.forEach((item) => {
        if (item.bidType == bidtype && item[field] != undefined) {
          count += Number(item[field]);
        } else {
          count += 0;
        }
      });
      return (count / (list.length / 2)).toFixed(2);
    },
    sortSore(arrList, value) {
      let scoreValue;
      arrList.sort((a, b) => {
        return b[Object.keys(b)[0]] - a[Object.keys(a)[0]];
      });
      arrList.forEach((ele, index) => {
        if (value.toString() == Object.keys(ele)[0]) {
          scoreValue = index + 1;
        }
      });
      return scoreValue;
    },
    

css部分
<style>
.col-styleone {
  background-color: #e4e4e4;
}
.col-stylethree {
  color: red;
}
</style>

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
vxe-table是一个基于vue的格组件,支持增删改查、虚拟滚动、懒加载、快捷菜单、数据校验、树形结构、打印导出、单渲染、数据分页、模态窗口、自定义模板、灵活的配置项、丰富的扩展插件等... 设计理念: 面向现代浏览器,高效的简洁 API 设计 模块化格、按需加载、插件化扩展 为单行编辑格而设计,支持增删改查及更多扩展,强大的功能的同时兼具性能 功能: Basic table (基础格) Grid (高级格) Size (尺寸) Striped (斑马线条纹) Table with border (带边框) Cell style (单元格样式) Column resizable (列宽拖动) Maximum table height (最大高度) Resize height and width (响应式宽高) Fixed column (固定列) Grouping table head (头分组) Highlight row and column (高亮行、列) Table sequence (序号) Radio (单选) Checkbox (多选) Sorting (排序) Filter (筛选) Rowspan and colspan (合并行或列) Footer summary (尾合计) Import (导入) Export (导出) Print (打印) Show/Hide column (显示/隐藏列) Loading (加载中) Formatted content (格式化内容) Custom template (自定义模板) Context menu(快捷菜单) Virtual Scroller(虚拟滚动) Expandable row (展开行) Pager(分页) Form(单) Toolbar(工具栏) Tree table (树形格) Editable CRUD(增删改查) Validate(数据校验) Data Proxy(数据代理) Keyboard navigation(键盘导航) Modal window(模态窗口) Charts(图工具) 更新日志: v4.0.20 table 修改单选框、复选框获取值错误问题 grid 修复 KeepAlive 中报错问题

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值