如果在打印对话框中单击取消,如何关闭选项卡?

时间:2017-06-12 11:32:42

标签: angular

我有这个方法:

  print(data): void{
  let printContents, popupWin;
    let trHtml = '';
    for (let entry of data) {
        trHtml += `<tr>
                        <td>` + entry.aaabroj + `</td>
                        <td>` + entry.bbbbroj + `</td>
                        <td>` + entry.sifratipa + `</td>
                        <td>` + entry.vrijemepoziva + `</td>
                        <td>` + entry.trajanjepoziva + `</td>
                        <td>` + entry.iznoskm + `</td>
                    </tr>`;
    }
     window.open('', 'popupWin', '');
     window.print();
    popupWin.document.write(`
    <html>
        <head>
        <title>Detaljni izvještaj</title>
        <style>
            table {
                font-family: arial, sans-serif;
                border-collapse: collapse;
                width: 100%;
            }

            td, th {
                border: 1px solid #dddddd;
                text-align: left;
                padding: 8px;
            }

            tr:nth-child(even) {
                background-color: #dddddd;
            }
        </style>
        </head>
    <body>
       <table>
        <tr>
            <th>Pretp.broj</th>
            <th>Pozvani broj</th>
            <th>Tip poziva</th>
            <th>Start</th>
            <th>Količina</th>
            <th>Cijena</th>
        </tr>
        `+trHtml+`
        </table>
    </body>
    </html>`);
   }

现在我在打印对话框中打开表格但是当用户点击取消时它关闭打印对话框但是标签仍然打开。任何建议我如何关闭该标签呢?

1 个答案:

答案 0 :(得分:0)

您无法直接从常规网页访问Chrome的内部窗口(本例中为打印对话框)。

(function () {

    var beforePrint = function () {
        alert('Functionality to run before printing.');
    };

    var afterPrint = function () {
        alert('Functionality to run after printing');
    };

    if ($window.matchMedia) {
        var mediaQueryList = $window.matchMedia('print');

        mediaQueryList.addListener(function (mql) {
            if (mql.matches) {
                beforePrint();
            } else {
                afterPrint();
            }
        });
    }

   $window.onbeforeprint = beforePrint;
   $window.onafterprint = afterPrint;

}());

参考:How to capture the click event on the default print menu called by Javascript window.print()?

此处未测试此特定代码,但之前做过类似的事情。