el-table动态表格实现选择多列打印

<template>
	// 打印按钮
	<el-popover placement="bottom" :width="400" trigger="click">
        <template #reference>
          <el-button>打印</el-button>
        </template>
        <div>
          <el-checkbox-group v-model="checkedColumn" :min="1" :max="8" @change="checkedChange">
            <el-checkbox v-for="item in riZhiTableHeader" :key="item.prop" :label="item.label"
              size="large" />
            <el-button type="success" @click="printJson">确定</el-button>
          </el-checkbox-group>
        </div>
	</el-popover>
	// 动态渲染表格
	<el-table :data="riZhiTable" style="width: 100%;" highlight-current-row border fit size="small">
        <el-table-column v-for="item in riZhiTableHeader" :key="item.prop" :prop="item.prop" :label="item.label"
          :width="item.width" align="center" header-align="center">
        </el-table-column>
      </el-table>
</template>

<script lang="ts" setup>
import printJS from 'print-js'
// 打印选中列
const checkedColumn = ref([])
// 打印列格式化
const properties: any = ref([])
// 表格表头
const riZhiTableHeader = ref([
  { label: "费用类型", prop: "fee_cls", width: '120', checked: true },
  { label: "实收价", prop: "fee_shishou", width: '100', checked: true },
  { label: "应收价", prop: "fee_yingshou", width: '100', checked: true },
  { label: "折扣价", prop: "fee_zhekou", width: '100', checked: true },
])
// 表格数据  通过请求获取存储在这
const riZhiTable = ref([])

// 选中打印列格式化
const checkedChange = (value: any) => {
  // 每次改变复选框置空数组
  checkedColumn.value = []
  properties.value = []
  checkedColumn.value = value
  // 循环表头查找选中的值,找到则 push 打印需要的属性
  riZhiTableHeader.value.forEach((item: any) => {
    checkedColumn.value.forEach((itemCheck: any) => {
      if (item.label == itemCheck) {
        properties.value.push({
          field: item.prop,
          displayName: item.label,
          columnSize: 1
        })
      }
    })
  })
}

// 打印表格
const printJson = () => {
  printJS({
  	// riZhiTable .value 为表格所有数据
    printable: riZhiTable .value,
    // 要打印的列
    properties: properties.value,
    type: 'json',
    header: '费用分类统计',
    // 样式设置
    gridStyle: 'border: 1px solid #333; text-align:center;',
    gridHeaderStyle: 'color: #333;  border: 1px solid #333;'
  })
}
</script>
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

A_ugust__

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

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

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

打赏作者

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

抵扣说明:

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

余额充值