当前位置:   article > 正文

【若依前后端分离】选中表格内容,打印为表格_若依 前端打印

若依 前端打印

基于hiprint 的vue-plugin-hiprint来打印

hiprint地址:hiprint.io
实现点击某些行的数据,然后点击打印按钮直接弹出下面的打印框

 

1.安装依赖

npm install vue-plugin-hiprint

2.找到依赖文件夹node_modules/vue-plugin-hiprint/dist/print-lock.css文件,赋值一份到public目录下,然后打开public/index.html,再其中添加下列代码

  1. <link rel="stylesheet" type="text/css" media="print" href="https://cdn.jsdelivr.net/npm/vue-plugin-hiprint@latest/dist/print-lock.css">
  2. <!-- * 以处理打印所需css, 当然你也可以自行处理-->
  3. <!-- * 比如: index.html目录下放一个print-lock.css, 然后在index.html添加:-->
  4. <link rel="stylesheet" type="text/css" media="print" href="/print-lock.css">

3.再main.js中引入全局依赖

  1. import {hiPrintPlugin} from 'vue-plugin-hiprint'
  2. Vue.use(hiPrintPlugin, '$pluginName')

 

4.在所需要的页面中使用 ,this.selectedData是选中数据是一个数组,需要注意的是数组中字段需要与模板中定义的字段名相同

this.printTemplate是在hiprint.io中生成的

  1. handlePrint(){
  2. this.$pluginName.init();
  3. let hiprintTemplate = new this.$pluginName.PrintTemplate({
  4. template: JSON.parse(this.printTemplate),
  5. });
  6. var selectedData=this.selectedData;
  7. hiprintTemplate.print(selectedData);
  8. }

5.设计模板,上列代码中的this.printTemplate内容为下列代码

 

  1. {
  2. "panels": [
  3. {
  4. "index": 0,
  5. "name": 1,
  6. "height": 297,
  7. "width": 210,
  8. "paperHeader": 49.5,
  9. "paperFooter": 780,
  10. "printElements": [
  11. {
  12. "options": {
  13. "left": 17.5,
  14. "top": 87.5,
  15. "height": 72,
  16. "width": 550,
  17. "textAlign": "center",
  18. "field": "printData",
  19. "coordinateSync": false,
  20. "widthHeightSync": false,
  21. "columns": [
  22. [
  23. {
  24. "width": 40,
  25. "title": "字段1",
  26. "field": "one",
  27. "checked": true,
  28. "columnId": "one",
  29. "fixed": false,
  30. "align": "center"
  31. },
  32. {
  33. "width": 40,
  34. "title": "字段2",
  35. "field": "two",
  36. "checked": true,
  37. "columnId": "two",
  38. "fixed": false,
  39. "rowspan": 1,
  40. "colspan": 1
  41. },
  42. {
  43. "width": 40,
  44. "title": "字段三",
  45. "field": "three",
  46. "checked": true,
  47. "columnId": "three",
  48. "fixed": false,
  49. "rowspan": 1,
  50. "colspan": 1
  51. }
  52. ]
  53. ]
  54. },
  55. "printElementType": {
  56. "title": "表格",
  57. "type": "table",
  58. "editable": true,
  59. "columnDisplayEditable": true,
  60. "columnDisplayIndexEditable": true,
  61. "columnTitleEditable": true,
  62. "columnResizable": true,
  63. "columnAlignEditable": true,
  64. "isEnableEditField": true,
  65. "isEnableContextMenu": true,
  66. "isEnableInsertRow": true,
  67. "isEnableDeleteRow": true,
  68. "isEnableInsertColumn": true,
  69. "isEnableDeleteColumn": true,
  70. "isEnableMergeCell": true
  71. }
  72. }
  73. ],
  74. "paperNumberLeft": 565.5,
  75. "paperNumberTop": 819,
  76. "paperNumberContinue": true,
  77. "watermarkOptions": {
  78. "content": "vue-plugin-hiprint",
  79. "rotate": 25,
  80. "timestamp": true,
  81. "format": "YYYY-MM-DD HH:mm"
  82. }
  83. }
  84. ]
  85. }

6.表格内容,及标题 4中的this.selectedData内容为

  1. {
  2. printData: [{
  3. one: '第一行第一列内容',
  4. two: '第一行第二列内容',
  5. three: '第一行第三列内容'
  6. },
  7. {
  8. one: '第二行第一列内容',
  9. two: '第二行第二列内容',
  10. three: '第二行第三列内容'
  11. }]
  12. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/595011
推荐阅读
相关标签
  

闽ICP备14008679号