vue打印表格

主要用到的xmlns:th 和 “vue3-print-nb”: “^0.1.4”,
实现思路
1.前端画出需要打印表格的页面,可以用纯原生css、html
例如:在这里插入图片描述

<body>
    <div style="font-size: 12px;color:#74798C;">
        <section style="margin-top: 10px;">

            <table style="width: 100%;border: 1px solid black;border-collapse:collapse;">
                <tbody>
                    <tr>
                        <td style="border:1px solid black;text-align:center;width:5%;color:black;font-weight: bold; line-height: 30px;font-size: 16px;"
                            colspan="8">22222222222222222222222222222</td>
                        </td>
                    </tr>
                    <tr>
                        <td style="border:1px solid black;text-align:center;width:5%;color:#74798C;writing-mode:vertical-lr;letter-spacing: 18px;"
                            rowspan="8">11111111</td>
                        <td style="border:1px solid black;width:10%;text-align: center;color:#74798C;height: 40px;">
                            22222</td>
                        <td style="border:1px solid black;width:15%;text-align: center;color:#74798C;">
                            <span th:text="${realname}"></span>
                        </td>
                        <td style="border:1px solid black;width:10%;text-align: center;color:#74798C;">
                            33333</td>
                        <td style="border:1px solid black;width:15%;text-align: center;color:#74798C;" colspan="6">
                            <span th:text="${applyDate}"></span>
                        </td>

                    </tr>
                    <tr style="height: 60px">
                        <td style="border:1px solid black;text-align: center;color:#74798C;">11111</td>
                        <td style="border:1px solid black;text-align: center;color:#74798C;">
                            <span th:text="${name}"></span>
                        </td>
                        <td style="border:1px solid black;text-align: center;color:#74798C;">22222222</td>
                        <td style="border:1px solid black;text-align: center;color:#74798C;" colspan="6">
                            <span th:text="${budget}"></span>
                        </td>

                    </tr>

                    <tr style="height: 60px">
                        <td style="border:1px solid black;text-align: center;color:#74798C;">333333</td>
                        <td style="border:1px solid black;text-align: left;color:#74798C; " colspan="8">
                            <div style="margin-left: 20px;">
                                <input type="checkbox" th:checked="${beckonpickStyle1}"
                                    style="margin: 0px;line-height: 40px;">
                                <span style="line-height: 40px; display: inline-block;margin-right: 14px;">11111</span>
                                <input type="checkbox" th:checked="${beckonpickStyle2}"
                                    style="margin: 0px;line-height: 40px;">
                                <span style="line-height: 40px; display: inline-block;margin-right: 14px;">11111</span>
                                <input type="checkbox" th:checked="${beckonpickStyle3}"
                                    style="margin: 0px;line-height: 40px;">
                                <span style="line-height: 40px; display: inline-block;margin-right: 14px;">222222</span>
                                <input type="checkbox" th:checked="${beckonpickStyle4}"
                                    style="margin: 0px;line-height: 40px;">
                                <span style="line-height: 40px; display: inline-block;margin-right: 14px;">3333333</span>
                            </div>

                        </td>

                    </tr>
                    <tr style="height: 60px">
                        <td style="border:1px solid black;text-align: center;color:#74798C;">111111</td>
                        <td style="border:1px solid black;text-align: center;color:#74798C;">
                            <span th:text="${deadline}"></span>
                        </td>
                        <td style="border:1px solid black;text-align: center;color:#74798C;">111111</td>
                        <td style="border:1px solid black;text-align: center;color:#74798C;" colspan="6">
                            <span th:text="${handleName}"></span>
                        </td>
                    </tr>
                    <tr style="height: 60px">
                        <td style="border:1px solid black;text-align: center;color:#74798C;">2222222</td>
                        <td style="border:1px solid black;text-align: left;color:#74798C;" colspan="8">
                            <!-- <span th:text="${idCard}"></span> -->
                            <div style="margin-left: 20px;">
                                <input type="checkbox" th:checked="${beckonpickMoney1}"
                                style="margin: 0px;line-height: 40px;">
                            <span style="line-height: 40px; display: inline-block;margin-right: 14px;">1111111</span>
                            <input type="checkbox" th:checked="${beckonpickMoney2}"
                                style="margin: 0px;line-height: 40px;">
                            <span style="line-height: 40px; display: inline-block;margin-right: 14px;">11111111</span>
                            </div>
                            

                        </td>

                    </tr>
                    <tr style="height: 60px">
                        <td style="border:1px solid black;text-align: center;color:#74798C;">22222222</td>
                        <td style="border:1px solid black;text-align: left;color:#74798C;" colspan="8">
                            <div style="margin-left: 20px;">
                            <input type="checkbox" th:checked="${beckonpickBuy1}"
                                style="margin: 0px;line-height: 40px;">
                            <span style="line-height: 40px; display: inline-block;margin-right: 14px;">1111111</span>
                            <input type="checkbox" th:checked="${beckonpickBuy2}"
                                style="margin: 0px;line-height: 40px;">
                            <span style="line-height: 40px; display: inline-block;margin-right: 14px;">22222222</span>
                            <input type="checkbox" th:checked="${beckonpickBuy3}"
                                style="margin: 0px;line-height: 40px;">
                            <span style="line-height: 40px; display: inline-block;margin-right: 14px;">33333333</span>
                        </div>

                        </td>

                    </tr>
                    <tr style="height: 60px">
                        <td style="border:1px solid black;text-align: center;color:#74798C;">1111111</td>
                        <td style="border:1px solid black;text-align: left;color:#74798C;" colspan="8">
                            <div style="margin-left: 20px;">
                            <input type="checkbox" th:checked="${beckonpickBuytype1}"
                                style="margin: 0px;line-height: 40px;">
                            <span
                                style="line-height: 40px; display: inline-block;margin-right: 4px;margin-right: 4px;">2222222</span>
                            <input type="checkbox" th:checked="${beckonpickBuytype2}"
                                style="margin: 0px;line-height: 40px;">
                            <span style="line-height: 40px; display: inline-block;margin-right: 14px;">33333333</span>
                            <input type="checkbox" th:checked="${beckonpickBuytype3}"
                                style="margin: 0px;line-height: 40px;">
                            <span style="line-height: 40px; display: inline-block;margin-right: 14px;">11111111</span>
                            <input type="checkbox" th:checked="${beckonpickBuytype4}"
                                style="margin: 0px;line-height: 40px;">
                            <span style="line-height: 40px; display: inline-block;margin-right: 14px;">11111111</span>
                            <input type="checkbox" th:checked="${beckonpickBuytype5}"
                                style="margin: 0px;line-height: 40px;">
                            <span style="line-height: 40px; display: inline-block;margin-right: 14px;">1111111</span>
                            <input type="checkbox" th:checked="${beckonpickBuytype6}"
                                style="margin: 0px;line-height: 40px;">
                            <span style="line-height: 40px; display: inline-block;margin-right: 14px;">22222222</span>
                            <input type="checkbox" th:checked="${beckonpickBuytype7}"
                                style="margin: 0px;line-height: 40px;">
                            <span style="line-height: 40px; display: inline-block;margin-right: 14px;">333333333</span>
                            <input type="checkbox" th:checked="${beckonpickBuytype8}"
                                style="margin: 0px;line-height: 40px;">
                            <span style="line-height: 40px; display: inline-block;margin-right: 14px;">444444444</span>
                            <input type="checkbox" th:checked="${beckonpickBuytype9}"
                                style="margin: 0px;line-height: 40px;">
                            <span style="line-height: 40px; display: inline-block;margin-right: 14px;">11111111</span>
                            <input type="checkbox" th:checked="${beckonpickBuytype10}"
                                style="margin: 0px;line-height: 40px;">
                            <span style="line-height: 40px; display: inline-block;margin-right: 14px;">22222222</span>
                            <input type="checkbox" th:checked="${beckonpickBuytype11}"
                                style="margin: 0px;line-height: 40px;">
                            <span style="line-height: 40px; display: inline-block;margin-right: 14px;">3333</span>
                            <input type="checkbox" th:checked="${beckonpickBuytype12}"
                                style="margin: 0px;line-height: 40px;">
                            <span style="line-height: 40px; display: inline-block;margin-right: 14px;">11111</span>
                        </div>
                        </td>

                    </tr>
                    <tr style="height: 60px">
                        <td style="border:1px solid black;text-align: center;color:#74798C;">1111111</td>
                        <td style="border:1px solid black;text-align: left;color:#74798C;" colspan="8">
                            <div style="margin-left: 20px;">
                                <input type="checkbox" th:checked="${beckonpickEffect1}"
                                    style="margin: 0px;line-height: 40px;">
                                <span
                                    style="line-height: 40px; display: inline-block;margin-right: 4px;margin-right: 4px;">333333</span>
                            </div>
                            <div  style="margin-left: 20px;"> 
                                <input type="checkbox" th:checked="${beckonpickEffect2}"
                                    style="margin: 0px;line-height: 40px;">
                                <span
                                    style="line-height: 40px; display: inline-block;margin-right: 4px;margin-right: 4px;">3333333</span>
                            </div>
                            <div  style="margin-left: 20px;">
                                <input type="checkbox" th:checked="${beckonpickEffect3}"
                                    style="margin: 0px;line-height: 40px;">
                                <span
                                    style="line-height: 40px; display: inline-block;margin-right: 4px;margin-right: 4px;">1111111</span>
                            </div>
                        </td>

                    </tr>

                    <tr style="height: 60px">
                        <td style="border:1px solid black;text-align:center;width:50px;color:#74798C;" colspan="2">11111
                        </td>
                        <td style="border:1px solid black;text-align: center;color:#74798C;" colspan="8">
                            <span th:text="${idCard}"></span>
                        </td>
                    <tr style="height: 60px">
                        <td style="border:1px solid black;text-align:center;width:50px;color:#74798C;" colspan="2">
                            222222</td>
                        <td style="border:1px solid black;text-align: center;color:#74798C;" colspan="8">
                            <span th:text="${idCard}"></span>
                        </td>
                    </tr>
                    <tr style="height: 60px">
                        <td style="border:1px solid black;text-align:center;width:50px;color:#74798C;" colspan="2">
                            3333333</td>
                        <td style="border:1px solid black;text-align: center;color:#74798C;" colspan="8">
                            <span th:text="${idCard}"></span>
                        </td>
                    </tr>
                    <tr style="height: 60px">
                        <td style="border:1px solid black;text-align:center;width:50px;color:#74798C;" colspan="2">1111111
                        </td>
                        <td style="border:1px solid black;text-align: center;color:#74798C;" colspan="8">
                            <span th:text="${idCard}"></span>
                        </td>
                    </tr>

                    <tr style="height: 60px">
                        <td style="border:1px solid black;text-align:left;width:5%;color:#74798C; line-height: 10px;font-size: 10px;"
                            colspan="8">
                            <div style="margin-left: 20px;margin-top: 10px;">
                                说明:
                            </div>
                            <div style="margin-left: 20px;">
                                <p>1.111111111111111</p>
                            </div>
                            <div style="margin-left: 20px;">
                                <p> 2.11111111111111111111</p>
                            </div>

                        </td>
                        </td>
                    </tr>
                </tbody>
            </table>
        </section>
    </div>
</body>

代码效果如下
在这里插入图片描述

2.在vue3的页面用到弹框形式

<a-button type="primary" preIcon="ant-design:printer-outlined" @click="handlePrint(selectedRowKeys)">

selectedRowKeys:选择需要打印的数据(因为我这是选择表格的某一行打印,所以selectedRowKeys代表我选择的整行数据)

2.1
弹框

<a-modal style="width: 800px" :visible="showPrint" :footer="null" :closable="false">
      <div id="printContent" v-html="htmlText"></div>
      <button ref="printRef" v-print="printObj" style="display: none"> 打印 </button>
    </a-modal>

2.2
方法

//打印
function handlePrint(arr) {
  if (arr.length != 1) {
    createMessage.warning('请选中一条数据进行打印!');
  } else {
    printTemplate({ id: arr[0] })
      .then((res) => {
        htmlText.value = res;
        showPrint.value = true;
        nextTick(() => {
          showPrint.value = false;
          printRef.value.click();
          // printJS({
          //   printable: 'printContent',
          //   type: 'html',
          //   targetStyles: ['*'],
          //   maxWidth: 1000,

          //   // style: `.changesy{margin-top:40px}`,
          // });
          // printClick.value.click();
        });
      })
      .catch((err) => {
        console.log(err);
      });
  }
}

这个方法中 printTemplate({ id: arr[0] })
.then 这一部分是后台接口 这个接口主要用于告诉后台前端要打印表格哪一行的数据,并且让后台给 我们实现思路 1 中的模板赋值,就是下图红框值

在这里插入图片描述

结合 2.2方法中的 htmlText.value = res; 给我们弹框中的html赋值,(其实就是接口告诉后台打印的数据,后台将数据处理后 插入到我们给到后台的html模板中,然后我们前端在赋值到我们的html中 在打印

最后打印!

  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值