如何仅打印特定窗口

时间:2018-03-14 05:44:45

标签: php html

我有一个页面,可以查看销售的购买历史记录

enter image description here

当我点击查看完整详细信息时,这就是我得到的内容

enter image description here

问题是,当我点击打印完整的详细信息这是我得到的,我只想打印当我点击查看完整详细信息时弹出的窗口

enter image description here

如何仅打印“查看完整详细信息”窗口部分?

这是我的代码:

<!-- History -->
    <div class="modal fade" id="detail<?php echo $hrow['salesid']; ?>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <center><h4 class="modal-title" id="myModalLabel">Purchase Full Details</h4></center>
                </div>
                <div class="modal-body">
                <?php
                    $sales=mysqli_query($conn,"select * from sales where salesid='".$hrow['salesid']."'");
                    $srow=mysqli_fetch_array($sales);
                ?>
                <div class="container-fluid">
                    <div class="row">x
                        <div class="col-lg-12">
                            <p class="pull-right">Date: <?php echo date("F d, Y", strtotime($srow['sales_date'])); ?></p>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-12">
                            <label>
                                <button onclick="printsales()">Print Full details</button>
                                <script>
                                    function printsales() {
                                        window.print();
                                        }
                                </script>
                            </label>
                            <table width="100%" class="table table-striped table-bordered table-hover">
                                <thead>
                                    <tr>
                                        <th>Product Name</th>
                                        <th>Price</th>
                                        <th>Purchase Qty</th>
                                        <th>SubTotal</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                        $total=0;
                                        $pd=mysqli_query($conn,"select * from sales_detail left join product on product.productid=sales_detail.productid where salesid='".$hrow['salesid']."'");
                                        while($pdrow=mysqli_fetch_array($pd)){
                                            ?>
                                            <tr>
                                                <td><?php echo ucwords($pdrow['product_name']); ?></td>
                                                <td align="right"><?php echo number_format($pdrow['product_price'],2); ?></td>
                                                <td><?php echo $pdrow['sales_qty']; ?></td>
                                                <td align="right">
                                                    <?php 
                                                        $subtotal=$pdrow['product_price']*$pdrow['sales_qty'];
                                                        echo number_format($subtotal,2);
                                                        $total+=$subtotal;
                                                    ?>
                                                </td>
                                            </tr>
                                            <?php
                                        }
                                    ?>
                                    <tr>
                                        <td align="right" colspan="3"><strong>Total</strong></td>
                                        <td align="right"><strong><?php echo number_format($total,2); ?></strong></td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>
                    </div>      
                </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal"><i class="fa fa-times"></i> Close</button>
                </div>
            </div>
        </div>
    </div>

2 个答案:

答案 0 :(得分:0)

为要隐藏的元素添加ID,例如:

<div id="dontprint"> This will be hidden on print mode </div>
<div> This will be shown on print mode </div>

然后,使用CSS隐藏那些您不想打印的元素。在代码末尾添加这样的代码。

<style> 
@media print {
div#dontprint{
display: none;
}
}
</style>

答案 1 :(得分:0)

您只需要插入简单的javascript代码即可打印整页。点击Click here to Print即可获得打印窗口。

<script type="text/javascript">
function PrintElem(elem,title)
{
    Popup($(elem).html(),title);
}
function Popup(data,title) 
{
    var mywindow = window.open('', '_blank','width=1000,height=700');
    mywindow.document.write('<html><head><title>'+title+'</title></head>');
    mywindow.document.write('<body onload="window.print()">');
    mywindow.document.write(data);
    mywindow.document.write('</body></html>');
    mywindow.document.close();
    return true;
}
</script>

<a href="javascript:;" onclick='PrintElem(".container-fluid","Printed Element")' class="print-part">Click here to Print</a>