【问题标题】:Printing a table in jquery在 jquery 中打印表格
【发布时间】:2018-09-27 13:37:12
【问题描述】:

我想打印一个没有edit | delete 按钮字段的表格,并将表格在打印中居中。

我正在尝试使用此代码,但输出打印与我预期的不符。

Here is the output print

<table class="table"  border="1" cellpadding="3" id="printTable">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Equipamento.Nome)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Colaborador)
        </th>
        <th style="align-content:center">
            @Html.DisplayNameFor(model => model.Qtd)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.DataRequisicao)
        </th>
        <th>

        </th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Equipamento.Nome)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Colaborador)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Qtd)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.DataRequisicao)
            </td>

            <td id="x">
                @Html.ActionLink("Edit", "Edit", new { id = item.RequisicaoId }) |
                @Html.ActionLink("Details", "Details", new { id = item.RequisicaoId }) |
                @Html.ActionLink("Delete", "Delete", new { id = item.RequisicaoId })
            </td>
        </tr>

    }

</table>

这是 jquery 脚本,我正在尝试使用 $("#printTable tr td") 隐藏元素,但它不起作用。

<script>
    function printData() {
        var divToPrint = document.getElementById("printTable");
        newWin = window.open("");
        newWin.document.write(divToPrint.outerHTML);
        newWin.print();
        newWin.close();
    }

    $('button').on('click', function () {
        $("#printTable tr td").each(function () {
            $(this).on('click', function () {
                $(this).find("#x").toggleClass('hidden');
            });
        });
        printData();
    })
</script>

【问题讨论】:

  • 你的预期输出是什么?
  • 打印没有编辑的表格 |删除按钮字段,并将表格居中打印。

标签: jquery printing


【解决方案1】:

看起来您正在应用一个没有附带 css 的类 hidden

尝试在文件顶部添加此块:

<style>
  .hidden { display: none; }
</style>

【讨论】:

    猜你喜欢
    • 2018-05-19
    • 2023-04-03
    • 2018-07-26
    • 2016-08-12
    • 1970-01-01
    • 2011-11-13
    • 2013-09-19
    • 2021-12-21
    • 2019-08-30
    相关资源
    最近更新 更多