javascript - 在 asp.net MVC 中打印表格行

标签 javascript c# asp.net-mvc razor razor-pages

我正在设置一个包含交易的表,并且希望能够通过单击按钮单独打印每笔交易(基本上打印每一行)。 但似乎无法弄清楚如何。

我尝试在 google/stackoverflow 上搜索它,但到目前为止我找不到我要找的东西。

<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Clientnumber)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.LicensePlate)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.AccountNumber)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Amount)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Date)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Status)
        </th>
        <th>
            <a>Gegevens Ophalen</a>
        </th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Clientnumber)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.LicensePlate)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.AccountNumber)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Amount)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Date)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Status)
            </td>
            <td>
                <button id="idButton" class="btn btn-secondary">Print Transactions</button>
            </td>
        </tr>
    }
</table>

所以最后我希望能够按下一个按钮并获得按钮所在行的打印预览。

最佳答案

我在javascript/jquery中创建了一个示例,使用可以应用于您的cshtml文件。

您需要复制到 HTML 文件才能运行此代码。

$('.idButton').on('click',function(){
     var printed = $(this).closest('tr').html();
     newWin= window.open("");
   newWin.document.write(printed);
   newWin.print();
   newWin.close();
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<head>
<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>

<h2>HTML Table</h2>

<table id="yourtable">
  <tr>
    <th>Company</th>
    <th>Contact</th>
    <th>Country</th>
    <th>Print</th>
  </tr>
  <tr>
    <td>Alfreds Futterkiste</td>
    <td>Maria Anders</td>
    <td>Germany</td>
 <td><button class="idButton" class="btn btn-secondary">Print Transactions</button></td>
  </tr>

  <tr>
    <td>Laughing Bacchus Winecellars</td>
    <td>Yoshi Tannamuri</td>
    <td>Canada</td>
 <td><button class="idButton" class="btn btn-secondary">Print Transactions</button></td>
  </tr>
  <tr>
    <td>Magazzini Alimentari Riuniti</td>
    <td>Giovanni Rovelli</td>
    <td>Italy</td>
    <td><button class="idButton" class="btn btn-secondary">Print Transactions</button></td>
  </tr>
</table>

</body>
</html>

关于javascript - 在 asp.net MVC 中打印表格行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55592206/

相关文章:

javascript - 无法根据下拉列表中选定的值过滤数据表数据

c# - OrganizationServiceContext.CreateQuery 与获取

c# - 获取 DEP 设置

c# - 如何读取多个文件txt c#

c# - ASP.NET MVC 传递多个模型以从 Controller 查看

javascript - 将鼠标悬停在列表项上,只显示一项而不是全部

javascript - 一种限制 JavaScript/CSS 范围的方法?

javascript - 将 View 链接到模型、将 View 链接到 Controller 的代码在哪里?

c# - 如何对 KeyedCollection<TKey, TItem> 进行排序?

jquery - 使用asp.net mvc和jquery时在哪里形成html?