在 Angular 中仅打印模态的内容

Pra*_*rma 1 javascript modal-dialog ng-bootstrap angular

我正在尝试在 Angular 中打印 ng-bootstrap 模态的模态内容。该模式包含一个表格,因此我希望以相同的格式打印文档。我想要这里给出的相同功能,但无法实现它。

根据此链接,它设置打印期间的可见性bodyhidden仅显示模态内容。但在 Angular 中,我们使用组件,因此设置body: hidden不起作用。它打印出整个文档(模态以及我不想要的背景组件、页眉和页脚)。

我已经遵循了很多解决方案,但无法这样做。

我还尝试将可打印的模式内容附加到新窗口,但写入新窗口只需要字符串。我在模态中使用表格,因此我希望打印相同样式的表格。

我怎样才能实现这个目标?

编辑1:这是我要打印的模态内容:

<div id="printable">
    <div class="row shipping">
      <div class="col-4">
        <h6>SHIPPING ADDRESS</h6>
        <p>
          {{ this.order.shipName }} : {{ this.order.addrLine1 }},
          {{ this.order.area1 }}, {{ this.order.area2 }},
          {{ this.order.countryCode }} - {{ this.order.pin }}
        </p>
      </div>
      <div class="col-4">
        <h6>SHIPPING METHOD</h6>
        <p>Standard</p>
      </div>
      <div class="col-4">
        <h6>ACCOUNT PAID</h6>
        <p>Paypal Account: {{ this.order.order.payerEmail }}</p>
      </div>
    </div>
    <br /><br />
    <div class="card">
      <table>
        <tr>
          <th>ITEM</th>
          <th>DETAILS</th>
          <th>QTY</th>
          <th>TOTAL</th>
        </tr>
        <tr>
          <td>
            <img
              [src]="this.order.order.image"
              style="height: 70px; width: 70px"
            />
          </td>
          <td>
            {{ this.order.order.category }}
          </td>
          <td>{{ this.order.order.quantity }}</td>
          <td>${{ this.order.order.amtPaid }}</td>
        </tr>
      </table>
    </div>
    <br />
    <div class="row" style="padding-bottom: 20px">
      <div class="col-8"></div>
      <div class="col-md-4">
        <h5>
          <b>ORDER TOTAL</b> : <span>${{ this.order.order.amtPaid }}</span>
        </h5>
        <br />
      </div>
    </div>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

单击按钮时,我想按原样打印此 div 内容

<div class="modal-footer" id="non-printable">
    <button type="button" class="btn btn-primary" (click)="onPrint()">
      Print Receipt
    </button>
    <button
      type="button"
      class="btn btn-secondary"
      (click)="modal.dismiss('cancel click')"
    >
      Cancel
    </button>
  </div>
Run Code Online (Sandbox Code Playgroud)

打印模态内容的方法:

onPrint() {
    var elem = document.getElementById("printable");
    var domClone = elem.cloneNode(true)
    var $printSection = document.getElementById("printSection");

    if (!$printSection) {
      let $printSection = document.createElement("div");
      $printSection.id = "printSection";
      document.body.appendChild($printSection);
      $printSection.innerHTML = "";
      $printSection.appendChild(domClone);
      window.print();    // only modal content should get print
    }
  }
Run Code Online (Sandbox Code Playgroud)

在CSS中:

@media screen {
  #printSection {
      display: none;
  }
}

@media print {
  body * {
    visibility:hidden;
  }
  #printSection, #printSection * {
    visibility:visible;
  }
  #printSection {
    position:absolute;
    left:0;
    top:0;
  }
}
Run Code Online (Sandbox Code Playgroud)

谢谢

Hit*_*esh 7

无需克隆 js 代码,只需更改代码中的 css

onPrint() {
      window.print();    
    }
Run Code Online (Sandbox Code Playgroud)

为了隐藏页眉、页脚和其他超出组件范围的内容,必须编写在全局 css 文件中,即style.css

onPrint() {
      window.print();    
    }
Run Code Online (Sandbox Code Playgroud)