zoukankan      html  css  js  c++  java
  • BootStrap带样式打印

    在新窗口打印时bootstrap表格的样式出不来,因为打印时没有加载CSS样式。

    我在jquery.PrintArea.js的基础上改造了下打印的方法:

    (function ($) {
        var printAreaCount = 0;
        $.fn.printArea = function () {
            var ele = $(this);
            var idPrefix = "printArea_";
            removePrintArea(idPrefix + printAreaCount);
            printAreaCount++;
            var iframeId = idPrefix + printAreaCount;
            var iframeStyle = 'position:absolute;0px;height:0px;left:-500px;top:-500px;';
            iframe = document.createElement('IFRAME');
            $(iframe).attr({
                style: iframeStyle,
                id: iframeId
            });
            document.body.appendChild(iframe);
            var doc = iframe.contentWindow.document;
            $(document).find("link").filter(function () {
                return $(this).attr("rel").toLowerCase() == "stylesheet";
            }).each(
                    function () {
                        doc.write('<link type="text/css" rel="stylesheet" href="'
                                + $(this).attr("href") + '" >');
                    });
            doc.write('<div class="' + $(ele).attr("class") + '">' + $(ele).html()
                    + '</div>');
            doc.close();
            var frameWindow = iframe.contentWindow;
            frameWindow.close();
            frameWindow.focus();
            frameWindow.print();
        }
        var removePrintArea = function (id) {
            $("iframe#" + id).remove();
        };
    
    
        $.fn.printAreas = function () {
            var ele = $(this);
            winname = window.open('', "_blank", '');
            var doc = winname.document;
            $(document).find("link").filter(function () {
                return $(this).attr("rel").toLowerCase() == "stylesheet";
            }).each(
                    function () {
                        doc.write('<link type="text/css" rel="stylesheet" href="'
                                + $(this).attr("href") + '" >');
                    });
            doc.write($(ele).html());
            doc.close();
            winname.print();
        }
    
    })(jQuery);

    前台调用的时候,把table放在一个div里,打印div即可:

           <div class="ibox-content" id="divprint">
                                <table id="tablePrint" class="footable table table-stripped toggle-arrow-tiny" data-page-size="8">
                                    <thead>
                                        <tr>
                                            <th class="text-center">项目</th>
                                            <th class="text-center">部门</th>
                                            <th class="text-center">营业收入</th>
                                            <th class="text-center">利润</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        @foreach (var item in Model)
                                        {
                                            <tr>
                                              <td class="text-center">@item.ProjectName</td>
                                              <td class="text-center">@item.DepartNameS</td>
                                              <td class="text-right"><a onclick="loadDetail('@item.ProjectId','@item.DepartId')">@item.InComeS</a></td>
                                       <td class="text-right"><a  onclick="loadProftsDetail('@item.ProjectId','@item.DepartId')">@item.ProfitS</a></td>
                                           </tr>
                                        }
                                    </tbody>
                                </table>
                                <div class="dataTables_paginate paging_bootstrap pagination">
                                  
                                </div>
                            </div>

    js调用:

       //打印
            $("#print").click(function ()
                $("#divprint").printAreas();
            });
  • 相关阅读:
    dir for RequestHandler and request
    python globals和locals
    Spring AOP(通知、连接点、切点、切面)
    Elasticsearch和Head插件安装(转)
    服务发现
    全面的软件测试( 转)
    软件开发项目人员配置
    阿里云oss缩略图如何产生读取 超简单 不看后悔(转)
    Elasticsearch模糊查询
    小米Pro 安装苹果系统
  • 原文地址:https://www.cnblogs.com/kennyliu/p/5009416.html
Copyright © 2011-2022 走看看