網(wǎng)站首頁 編程語言 正文
假設(shè)在一個頁面上有眾多內(nèi)容,而我們只想把該頁面上的表格內(nèi)容打印出來,window.print()方法會把整個頁面的內(nèi)容打印出來,如何做到只打印表格內(nèi)容呢?
既然window.print()只會打印整頁的內(nèi)容,何不把表格放在一個部分視圖中,在部分視圖中再調(diào)用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這個強類型視圖中調(diào)用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>分數(shù)</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視圖中,點擊按鈕,彈出部分視圖內(nèi)容:
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<button id="p">打印已經(jīng)確定好的內(nèi)容</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>
}
點擊"打印已經(jīng)確定好的內(nèi)容"按鈕:
取消禁用彈出窗口,再次點擊"打印已經(jīng)確定好的內(nèi)容"按鈕:
點擊"打印表格":
原文鏈接:https://www.cnblogs.com/darrenji/p/3825236.html
相關(guān)推薦
- 2022-01-06 解決 el-form 異步校驗導(dǎo)致重復(fù)校驗的問題
- 2022-07-15 SQL?Server中的排名函數(shù)與分析函數(shù)詳解_MsSql
- 2022-12-10 C++?Boost?Spirit進階教程_C 語言
- 2022-06-10 基于PyQt5制作一個群發(fā)郵件工具_python
- 2022-02-11 ES6的Promise用法詳解_基礎(chǔ)知識
- 2022-12-24 C++中STL容器的主要使用及含義說明_C 語言
- 2022-06-16 VS?Code?C++環(huán)境的搭建過程_C 語言
- 2022-06-01 利用Python實現(xiàn)外觀數(shù)列求解_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支