当前位置:   article > 正文

解决vue-print插件打印时表格宽度无法撑满自适应_elementui表格宽度撑满

elementui表格宽度撑满

最近帮同事解决一个前端页面布局问题,技术背景vue+elementui,出现问题如下:点击详情表格布局一切正常(列数不固定,估计后端返回决定)点击打印之后的表格出现不能撑满,多出空白列的情况。

 未点击打印时布局展示

 经过各种调试主要解决由:tableLayout属性用来显示表格单元格、行、列的算法规则

①auto:(默认值)列宽度由单元格内容设定;

 ②fixed: 列宽由表格宽度和列宽度设定;

 ③inherit:规定应该从父元素继承table-layout属性的值。

/设置表格布局算法*/
 table{
     table-layout:auto!important;
}

和重新给elementui的表格内的宽度一定要设置成100%,.el-table__header和.el-table__body这个是框架内置的一些样式,得css重新更改

.el-table__header{
  width: 100%!important;
}
.el-table__body{
  width: 100%!important;
}

附带让表格内容超过自适宽度就换行

word-wrap: normal!important;
width: auto!important;
word-break: break-all!important;

修改完成后打印的表格展示就正常了 

 其中打印print.js

  1. // 打印类属性、方法定义
  2. /* eslint-disable */
  3. const Print = function (dom, options) {
  4. if (!(this instanceof Print)) return new Print(dom, options);
  5. this.options = this.extend({
  6. 'noPrint': '.no-print'
  7. }, options);
  8. if ((typeof dom) === "string") {
  9. this.dom = document.querySelector(dom);
  10. } else {
  11. this.isDOM(dom)
  12. this.dom = this.isDOM(dom) ? dom : dom.$el;
  13. }
  14. this.init();
  15. };
  16. Print.prototype = {
  17. init: function () {
  18. var content = this.getStyle() + this.getHtml();
  19. this.writeIframe(content);
  20. },
  21. extend: function (obj, obj2) {
  22. for (var k in obj2) {
  23. obj[k] = obj2[k];
  24. }
  25. return obj;
  26. },
  27. getStyle: function () {
  28. var str = "",
  29. styles = document.querySelectorAll('style,link');
  30. for (var i = 0; i < styles.length; i++) {
  31. str += styles[i].outerHTML;
  32. }
  33. str += "<style>" + (this.options.noPrint ? this.options.noPrint : '.no-print') + "{display:none;}</style>";
  34. return str;
  35. },
  36. getHtml: function () {
  37. var inputs = document.querySelectorAll('input');
  38. var textareas = document.querySelectorAll('textarea');
  39. var selects = document.querySelectorAll('select');
  40. for (var k = 0; k < inputs.length; k++) {
  41. if (inputs[k].type == "checkbox" || inputs[k].type == "radio") {
  42. if (inputs[k].checked == true) {
  43. inputs[k].setAttribute('checked', "checked")
  44. } else {
  45. inputs[k].removeAttribute('checked')
  46. }
  47. } else if (inputs[k].type == "text") {
  48. inputs[k].setAttribute('value', inputs[k].value)
  49. } else {
  50. inputs[k].setAttribute('value', inputs[k].value)
  51. }
  52. }
  53. for (var k2 = 0; k2 < textareas.length; k2++) {
  54. if (textareas[k2].type == 'textarea') {
  55. textareas[k2].innerHTML = textareas[k2].value
  56. }
  57. }
  58. for (var k3 = 0; k3 < selects.length; k3++) {
  59. if (selects[k3].type == 'select-one') {
  60. var child = selects[k3].children;
  61. for (var i in child) {
  62. if (child[i].tagName == 'OPTION') {
  63. if (child[i].selected == true) {
  64. child[i].setAttribute('selected', "selected")
  65. } else {
  66. child[i].removeAttribute('selected')
  67. }
  68. }
  69. }
  70. }
  71. }
  72. return this.dom.outerHTML;
  73. },
  74. writeIframe: function (content) {
  75. var w, doc, iframe = document.createElement('iframe'),
  76. f = document.body.appendChild(iframe);
  77. iframe.id = "myIframe";
  78. //iframe.style = "position:absolute;width:0;height:0;top:-10px;left:-10px;";
  79. iframe.setAttribute('style', 'position:absolute;width:0;height:0;top:-10px;left:-10px;');
  80. w = f.contentWindow || f.contentDocument;
  81. doc = f.contentDocument || f.contentWindow.document;
  82. doc.open();
  83. doc.write(content);
  84. doc.close();
  85. var _this = this
  86. iframe.onload = function(){
  87. _this.toPrint(w);
  88. setTimeout(function () {
  89. document.body.removeChild(iframe)
  90. }, 100)
  91. }
  92. },
  93. toPrint: function (frameWindow) {
  94. try {
  95. setTimeout(function () {
  96. frameWindow.focus();
  97. try {
  98. if (!frameWindow.document.execCommand('print', false, null)) {
  99. frameWindow.print();
  100. }
  101. } catch (e) {
  102. frameWindow.print();
  103. }
  104. frameWindow.close();
  105. }, 10);
  106. } catch (err) {
  107. console.log('err', err);
  108. }
  109. },
  110. isDOM: (typeof HTMLElement === 'object') ?
  111. function (obj) {
  112. return obj instanceof HTMLElement;
  113. } :
  114. function (obj) {
  115. return obj && typeof obj === 'object' && obj.nodeType === 1 && typeof obj.nodeName === 'string';
  116. }
  117. };
  118. const MyPlugin = {}
  119. MyPlugin.install = function (Vue, options) {
  120. // 4. 添加实例方法
  121. Vue.prototype.$print = Print
  122. }
  123. export default MyPlugin

print.css样式

  1. @media print {
  2. section{
  3. margin-top: -35px;
  4. }
  5. .el-table__header{
  6. width: 100%!important;
  7. }
  8. .el-table__body{
  9. width: 100%!important;
  10. }
  11. .el-table__header{
  12. table-layout: auto;
  13. }
  14. .el-table__body{
  15. table-layout: auto!important;
  16. }
  17. .modal-body {
  18. min-height: auto;
  19. }
  20. .el-table--border th, .el-table--border td{
  21. border-right: 1px solid #ebeef5;
  22. }
  23. .el-table th, .el-table td{
  24. padding: 0px;
  25. }
  26. .el-table .cell{
  27. padding-right: 0px;
  28. }
  29. .el-table{
  30. width: 100%!important;
  31. }
  32. }

具体vue页面部分关键代码

  1. <template>
  2. <div id="associateInfo">
  3. <v-breadcrumb :title="vBreadCrumbData"></v-breadcrumb>
  4. <div class="pull-right">
  5. <span class="fa fa-print fa-2x" @click="printContent()" style="cursor: pointer"></span>
  6. <span>&nbsp</span>
  7. </div>
  8. <div style="margin-top: 10px;padding: 0px 15px;">
  9. <div id="printDiv" style="width: 50%;margin-left: 25%;">
  10. <section ref="print">
  11. <div style="width: 90%;margin-left: 5%;line-height:2;font-size: 20px;">
  12. <div id="extra" style="page-break-before: always;">
  13. <hr id="printHidden">
  14. <div style="color: #000000;font-size: 16px;">附件:协查信息表
  15. <el-button v-if="associate.assistOrgId == currentOrgId && !isOpenSendBack" @click="isOpenSendBack=true"
  16. style="float: right" type="danger" size="mini" round>退回
  17. </el-button>
  18. <span v-if="isOpenSendBack" style="float: right">
  19. <el-button data-toggle='modal' href="#modal-send-back" type="success" size="mini" round>确定</el-button>
  20. <el-button @click="isOpenSendBack=false" size="mini" round>取消</el-button>
  21. </span>
  22. </div>
  23. <div v-for="(value,index) in dynamicTable" style="padding-bottom: 10px;">
  24. <div style="font-size:16px">{{value.table_name}}</div>
  25. <el-table
  26. :data="associate.tableDataList[value.table_key]"
  27. border
  28. style="width: 100%; border: 1px solid #ebeef5;">
  29. <el-table-column v-for="(item,idx) in value.column_key" :key="idx" :label="item.column" width="auto"
  30. :property="item.key" align="center" :cell-style="timeStyle">
  31. <template slot-scope="scope">{{scope.row[scope.column.property]}}</template>
  32. </el-table-column>
  33. <el-table-column v-if="isOpenSendBack" align="center" prop="id" label="操作">
  34. <template slot-scope="scope"><input type='checkbox' name='checkBox1' :value=scope.row.id>
  35. </template>
  36. </el-table-column>
  37. </el-table>
  38. </div>
  39. </div>
  40. </div>
  41. </section>
  42. </div>
  43. </div>
  44. </div>
  45. </template>
  46. <script>
  47. import vBreadcrumb from '../../../components/common/Breadcrumb'
  48. import vModal from '../../../components/common/modal'
  49. import vTable from '../../../components/common/table'
  50. export default {
  51. name: "associateInfo",
  52. components: {vBreadcrumb, vTable, vModal},
  53. data() {
  54. return {
  55. vBreadCrumbData: [
  56. {name: '详情', url: '#'},
  57. ],
  58. associate: {
  59. associateContent: {
  60. title: "",
  61. receiver: "",
  62. content: "",
  63. contacts: "",
  64. phone: "",
  65. fax: "",
  66. email: "",
  67. createOrgName: "",
  68. time: ""
  69. },
  70. tableDataList: [],
  71. },
  72. dynamicTable: [],
  73. }
  74. },
  75. methods: {
  76. printContent(){
  77. this.$print(this.$refs.print)
  78. },
  79. },
  80. }
  81. </script>
  82. <style lang="scss">
  83. @import "../../../../static/print/print.css";
  84. #associateInfo {
  85. .modal-body {
  86. min-height: auto;
  87. }
  88. .el-table__header{
  89. table-layout: auto;
  90. }
  91. .el-table__body{
  92. table-layout: auto!important;
  93. }
  94. .el-table--border th, .el-table--border td{
  95. border-right: 1px solid #ebeef5;
  96. }
  97. .el-table th, .el-table td{
  98. padding: 0px;
  99. word-wrap: normal!important;
  100. width: auto!important;
  101. word-break: break-all!important;
  102. }
  103. .el-table .cell{
  104. padding-right: 0px;
  105. }
  106. }
  107. </style>

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

闽ICP备14008679号