MVC列印表格,把表格內容放到部分視圖列印

來源:互聯網
上載者:User

標籤:style   blog   http   java   color   width   

假設在一個頁面上有眾多內容,而我們只想把該頁面上的表格內容列印出來,window.print()方法會把整個頁面的內容列印出來,如何做到只列印表格內容呢?

 

既然window.print()只會列印整頁的內容,何不把表格放在一個部分視圖中,在部分視圖中再調用window.print()方法。

 

Model很簡單:

public class Student    {        public int Id { get; set; }        public string Name { get; set; }        public decimal Score { get; set; }    }

Home控制器中有一個Action方法返回Student的集合到部分視圖:

public class HomeController : Controller    {        public ActionResult Index()        {            return View();        }        public ActionResult PrintStudent()        {            var result = new List<Student>            {                new Student(){Id = 1, Name = "darren", Score = 90.9M},                new Student(){Id = 2, Name = "smith", Score = 91.8M},                new Student(){Id = 3, Name = "kathy", Score = 98.6M}            };            return PartialView(result);        }    }

在Home/PrintStudent.cshtml這個強型別視圖中調用window.print()方法:

@model IEnumerable<MvcApplication1.Models.Student><style type="text/css">    .c {        width: 100%;        border: 1px solid green;        border-collapse: collapse;    }    .c td {        padding: 2px;        border: 1px solid green;    }</style><style>    /* 列印的時候讓列印按鈕隱藏 */    @@media only print {        a {            display: none;        }    }</style><a href="#" onclick="window.print();return false;">列印表格</a><table class="c">    <thead>        <tr>            <th>編號</th>            <th>姓名</th>            <th>分數</th>        </tr>    </thead>    <tbody>        @foreach (var student in Model)        {            <tr>                <td>@student.Id</td>                <td>@student.Name</td>                <td>@student.Score</td>            </tr>        }    </tbody></table><a href="#" onclick="window.print();return false;">列印表格</a>

在Home/Index.cshtml視圖中,點擊按鈕,彈出部分視圖內容:

@{    ViewBag.Title = "Index";    Layout = "~/Views/Shared/_Layout.cshtml";}<button id="p">列印已經確定好的內容</button>@section scripts{    <script type="text/javascript">        $(function() {            $(‘#p‘).click(function() {                $.ajax({                    url: ‘@Url.Action("PrintStudent","Home")‘,                    success: function(data) {                        if (judgePopupBlocked) {                            alert("瀏覽器禁用快顯視窗了,請允許快顯視窗");                        }                        var popUpWindow = window.open();                        if (popUpWindow) {                            $(popUpWindow.document.body).html(data);                        } else {                            alert("瀏覽器禁用快顯視窗了,請允許快顯視窗");                        }                    }                });            });        });        //判斷瀏覽器是否阻止了快顯視窗        function judgePopupBlocked() {            var w = window.open(null, "", "width=1,height=1");            try {                w.close();                return false;            } catch (e) {                return true;            }         }    </script>}

點擊"列印已經確定好的內容"按鈕:

 

取消禁用快顯視窗,再次點擊"列印已經確定好的內容"按鈕:

 

點擊"列印表格":

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.