IT评测·应用市场-qidao123.com

标题: vue3-print打印eletable某一行的数据 [打印本页]

作者: 莱莱    时间: 2024-9-17 22:14
标题: vue3-print打印eletable某一行的数据
主页面的表格
  1. <template>
  2.     <el-table :data="list">
  3.         <el-table-column label="操作" align="center">
  4.            <template #default="scope">
  5.              <el-button
  6.                 link
  7.                 type="primary"
  8.                 @click="handleType(scope.row)"
  9.                 :loading="qrCodeLoading">
  10.                 打印
  11.               </el-button>
  12.             </template>
  13.         </el-table-column>
  14.     </el-table>
  15.     // 打印子页面
  16.     <type ref="qrRef" @success="loadData"/>
  17. </template>
  18. <script>
  19. const qrRef = ref()
  20. const qrCodeLoading = ref(false)
  21. /** 打印 */
  22. const multiple = ref([])
  23. const handleType = async (row) => {
  24.   try {
  25.     multiple.value.push(row)
  26.     console.log('ss', multiple)
  27.     qrRef.value.open(multiple)
  28.     // todo 接口
  29.     multiple.value = []
  30.   } catch {
  31.   } finally {
  32.     qrCodeLoading.value = false
  33.   }
  34. }
  35. const loadData = async (arg) => {
  36.   // todo 打印完毕
  37. }
  38. </script>
复制代码
 子页面---
  1. <template>
  2.   <Dialog
  3.       v-model="dialogVisible"
  4.       :title="dialogTitle"
  5.       width="1000px"
  6.       height="1200px"
  7.       @close="handleCloseModal"
  8.   >
  9.     <e-row>
  10.       <el-button type="success" plain v-print="printObj">
  11.         <Icon icon="ep:download"/>
  12.         打印
  13.       </el-button>
  14.     </e-row>
  15.     <el-row class="row-con" id="printMe">
  16.       <div
  17.           v-for="item in tableData"
  18.           :ref="setItemRef"
  19.           :key="item.partCode"
  20.           style="width: 100%"
  21.        >
  22.        <div style="border: 2px black; margin-top:105px;
  23.                     margin-left: 54px; margin-right: 55px; margin-bottom: 8px;">
  24.           <div style="width: 100%; display: flex; flex-direction: row">
  25.                {{ 需要打印的内容+tableData }}
  26.           </div>
  27.        </div>
  28.       </div>
  29.     </el-row>
  30.   </Dialog>
  31. </template>
  32. <script setup lang="ts">
  33. import qrcode from 'qrcode'
  34. const dialogVisible = ref(false)
  35. const dialogTitle = ref('条码打印')
  36. const handleCloseModal = () => {
  37.   dialogVisible.value = false
  38. }
  39. const tableData = ref([])
  40. onMounted(() => {
  41.   // console.log(t.userInfo.account)
  42. })
  43. const itemRefs = ref([])
  44. const setItemRef = (el) => {
  45.   if (el) {
  46.     itemRefs.value.push(el)
  47.   }
  48. }
  49. const printObj = ref({
  50.   id: 'printMe',
  51.   popTitle: '',
  52.   maxWidth: 302, // 最大宽度
  53.   extraCss:
  54.       'https://cdn.bootcdn.net/ajax/libs/animate.css/4.1.1/animate.compat.css, https://cdn.bootcdn.net/ajax/libs/hover.css/2.3.1/css/hover-min.css',
  55.   extraHead: '<meta http-equiv="Content-Language"content="zh-cn"/>',
  56.   // extraHead: '<meta http-equiv="Content-Language" content="zh-cn"/>,<style> #printMe { height: 302px !important; } <style>',//  可以传进去  style tag 标签;注意要逗号分隔   解决特定区域不显示问题
  57.   beforeOpenCallback(vue) {
  58.     vue.printLoading = true
  59.     console.log('打开之前')
  60.   },
  61.   openCallback(vue) {
  62.     vue.printLoading = false
  63.     console.log('执行了打印', vue)
  64.   },
  65.   closeCallback(vue) {
  66.     console.log('关闭了打印工具', vue)
  67.   }
  68. })
  69. const open = (data) => {
  70.   dialogVisible.value = true
  71.   console.log('data', data)
  72.   tableData.value = data.value
  73.   console.log('tableData', tableData.value)
  74.   tableData.value.forEach((item) => {
  75.     //var code = 'your-data' // 替换为你需要生成二维码的数据
  76.     qrcode.toDataURL(item.partCode, (err, url) => {
  77.       if (err) {
  78.         console.error(err)
  79.       } else {
  80.         console.log('toDataURL', url)
  81.       }
  82.     })
  83.   })
  84. }
  85. defineExpose({open}) // 提供 open 方法,用于打开弹窗
  86. </script>
  87. <style scoped lang="scss">
  88. .row-con {
  89.   display: flex;
  90.   flex-flow: row wrap;
  91. }
  92. .printContainer {
  93.   -webkit-print-color-adjust: exact;
  94.   /* 打印时表格上边框会消失 貌似是因为 使用伪元素的缘故 */
  95.   ::v-deep(.el-table__inner-wrapper::after) {
  96.     height: 0px !important;
  97.   }
  98.   /* 使用自己的表格上边框 */
  99.   ::v-deep(.el-table__inner-wrapper) {
  100.     border-top: 1px solid #e5e7eb;
  101.   }
  102.   /* 打印时边框太小会被挤没,那让边框变大点就好了*/
  103.   ::v-deep(.el-table__cell) {
  104.     border-right: 2px solid #e5e7eb;
  105.   }
  106. }
  107. </style>
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 IT评测·应用市场-qidao123.com (https://dis.qidao123.com/) Powered by Discuz! X3.4