旧行李 2019-01-05 08:01 采纳率: 25%
浏览 48

动态数据打印

Print dynamically generated data when print button clicked- Table has an ID but table data (td and tr) data's are dynamically generated one.

I am able to get the data in table and i have tried printing everything using window.print java-script.

HTML code -

 <div class="panel-body">
  <table class ="table table-hover" id="TableData">
   <caption>Representative Report</caption>
      <thead>
         <th>Date</th>
         <th>Doctor's Name </th>
         <th>Sampling Tablets </th>
         <th>Samling Quantity</th>
         <th>Chemists</th>
         <th>POB</th>
         <th>Location</th>
         <th>Area</th>
      </thead>
      <tbody id="tbody"></tbody>
      </table>
 </div>

Ajax calls to generate the Report in table -

 for (element in data)
 {
  var productsArray = data[element].products.split(',');
  var quantityArray = data[element].quantity.split(',');
  var chemistArray = data[element].Retailername.split(',');
  var pobArray = data[element].Pob.split(',');

  // find the largest row number
  var maxRows = Math.max(productsArray.length, quantityArray.length, chemistArray.length, pobArray.length);

 var content = '';
 var date = '<td rowspan="' + maxRows + '">' + data[element].date + '</td>';
  var doctorName = '<td rowspan="' + maxRows + '">' + data[element].doctor_name + '</td>';
  var locations = '<td rowspan="' + maxRows + '">' + data[element].locations + '</td>';
  var area = '<td rowspan="' + maxRows + '">' + data[element].area + '</td>';

  content += '<tr>' + date + doctorName;

  for (var row = 0; row < maxRows; row++) {
  if (row !== 0) {
     content += '<tr>';
   }

  // the ternary operator is used to check whether there is items in the array
                               // if yes, insert the value between the <td></td> tag
                               // if not, just add an empty <td></td> to the content as a placeholder
  content += '<td>' + (productsArray[row] ? productsArray[row] : '') + '</td>';
  content += '<td>' + (quantityArray[row] ? quantityArray[row] : '') + '</td>';
  content += '<td>' + (chemistArray[row] ? chemistArray[row] : '') + '</td>';
                               content += '<td>' + (pobArray[row] ? pobArray[row] : '') + '</td>';

   if (row === 0) {
   content += locations + area + '</tr>';
   } else {
   content += '</tr>';
   }
  }

  $('#tbody').append(content);
  }
 },

Here i am appending all the td's and tr's to table

And i want to print only Some columns of the table assume (Date, Doctorname and chemistname) when print Button is clicked.

I can use Onclick="Window.print" But this prints whole page either whole table.

Any help would really be appreciated.

  • 写回答

1条回答 默认 最新

  • weixin_33722405 2019-01-05 08:57
    关注

    You can use css for this. Just define the column that you want to hide. Here is a sample. I hide the second and last column. Hope to help and Happy new year, my friend :))

    <style>
         @media print { 
            table td:last-child {display:none} 
            table th:last-child {display:none}
    
            table td:nth-child(2) {display:none} 
            table th:nth-child(2) {display:none}
        } 
    </style>
    
    
    <div class="panel-body">
      <table class ="table table-hover" id="TableData">
       <caption>Representative Report</caption>
          <thead>
             <th>Doctor's Name</th>   
             <th>Chemists</th>
             <th>POB</th>
             <th>Location</th>
             <th>Area</th>
          </thead>
          <tbody id="tbody">
              <tr>
                <td>Iron Man</td>
                <td>ABC</td>
                <td></td>
                <td>China</td>
                <td>Asia</td>
              </tr>
              <tr>
                <td>Captain</td>
                <td>XYZ</td>
                <td></td>
                <td>England</td>
                <td>Europe</td>
              </tr>
          </tbody>
          </table>
     </div>
     <button id="btnPrin" onclick="window.print();">Print</button>       
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?