7

我正在处理 php mysql 中的表单。所以在我的页面中有一个表格,下面我以表格格式显示了该表格的数据。现在我只想打印表格结构,这就是为什么我刚刚制作了一个 PRINT 按钮:

<span style="float:right;"><a href="javascript:window.print()" type="button" class="btn">PRINT</a></span>

但它会打印带有表单字段和表格的整个页面,我只想打印表格。这是我的整个页面的设计,带有表格和表格:

<html>
<head></head>
<body>
 <div class="container">
 <form class="form-horizontal" action="" method="post" name="userform1" id="company-form" enctype="multipart/form-data">
     <?php if($_GET[id]){?>


<fieldset>
     <legend>Add Company</legend>
    <div class="control-group">
    <label class="control-label">Company Name</label>
    <div class="controls">
    <input type="text" name="company" id="company" value="<?php echo $selup['company']?>">
    </div>
    </div>
<div class="control-group">another field</div>
<div class="control-group">another field</div>
<div class="control-group">
    <div class="controls">
    <button type="submit" class="btn" name="submit" id="submit" value="Submit">Submit</button>
    </div>
    </div>
<table class="table table-bordered">
           <tbody>
            <tr>
                <td>S.No.</td>
                <td>Company Name</td>
                <td>Type</td>
                <td>Action</td>
          </tr>
                 <?php 
                 // to print the records
                $select = "select * from company where type='Miscellaneous'";
                $query1 = mysql_query($select);  
                 while($value = mysql_fetch_array($query1)){ ?>

                <tr>
                <td><?php echo $value[id];?></td>
                <td><?php echo $value[company ];?></td>
                <td><?php echo $value[type];?></td>
                <!--<td>&nbsp;</td>-->
                <?php /*?><td><?php echo $value[amount];?></td>               
                <td><?php echo $value[date];?></td><?php */?>
                <td><a href="<?php echo $_SERVER['PHP_SELF']; ?>?id=<?php echo $value[id];?>&cmd=edit"><i class="icon-edit"></i></a>
               <a href="<?php echo $_SERVER['PHP_SELF']; ?>?id=<?php  echo $value[id];?>&cmd=delete" onclick="return confirm('Are you sure you want to delete <?php  echo $value[customer];?>?')"><i class="icon-trash"></i></a></td>


                </tr><?php }?>
        </tbody>
       </table>
</fieldset>
<form>
</div>
</body>

所以我只想打印表格而不是整页。

4

4 回答 4

10

使用 css 隐藏您不想打印的元素:

@media print {
    .control-group {
      display: none;
    }
}
于 2013-07-31T08:09:05.277 回答
5
function printContent(el){
    var restorepage = document.body.innerHTML;
    var printcontent = document.getElementById(el).innerHTML;
    document.body.innerHTML = printcontent;
    window.print();
    document.body.innerHTML = restorepage;
}
<!DOCTYPE html>
<html>
<head>

</head> 
<body> 
<h1>My page</h1>
<div id="div1">DIV 1 content...</div>
<button onclick="printContent('div1')">Print Content</button>
<div id="div2">DIV 2 content...</div>
<button onclick="printContent('div2')">Print Content</button>
<p id="p1">Paragraph 1 content...</p>
<button onclick="printContent('p1')">Print Content</button>
</body> 
</html>
于 2016-04-06T06:23:28.897 回答
3

您可以制作一个 print-css(与@media print 的作用相同,但我认为它更干净,如果需要,您可以通过 javascript 禁用 css 包含):

<link rel="stylesheet" type="text/css" href="print.css" media="print" />

在该 css 中,您隐藏了所有不应打印的元素,例如:

.wrapper, .header {display: none;}
于 2013-07-31T08:09:09.623 回答
2

一种可能的解决方案,也许不是最佳选择:

1. Open a new window with JS
2. Copy the whole table into the new window (with jQuery for example)
3. Print the new window
4. Close the window

当然它有眨眼效果,但它会起作用。

于 2013-07-31T08:07:13.933 回答