javascript - 如何从 foreach 打印单独的行?

标签 javascript php jquery codeigniter

我在 php 中有一个 foreach 数组,其中显示有很多行的记录,我的 View 文件如下,

         <?php
          $count = 1;
          foreach ($resultlist as $student) {
            ?>
            <div class="slide-row" id="printableArea" style="padding:30px;border:2px solid #999;">
              <div class="row">
                <div class="col-md-10">
                  <div class="row">
                    <div class="col-md-4">
                      <label> Name: </label>
                      <?php echo $student['firstname']; ?>
                    </div>
                  </div>
                  <div class="row">
                    <div class="col-md-5">
                      <label> Standard: </label>
                      <?php echo $student['class']; ?>
                    </div>
                    <div class="col-md-5">
                      <label> Section: </label>
                      <?php echo $student['section']; ?>
                    </div>
                  </div>
                  <div class="row">
                    <div class="col-md-5">
                      <label> Admission No: </label>
                      <?php echo $student['admission_no']; ?>
                    </div>
                    <div class="col-md-5">
                      <label> House: </label>
                      <!-- <?php echo $student['section']; ?> -->
                    </div>
                  </div>
                  <div class="row">
                    <div class="col-md-5">
                      <label> Religion: </label>
                      <?php echo $student['religion']; ?>
                    </div>
                    <div class="col-md-5">
                      <label> Caste: </label>
                      <?php echo $student['cast']; ?>
                    </div>
                  </div>
                </div>
                <div class="col-md-2">
                  <img class="img-responsive img-thumbnail width150" alt="Cinque Terre" src="<?php echo base_url(); ?>backend/dist/img/jeevaschool.png" alt="Image">
                </div>
              </div>
              <br>
              <table class="table table-bordered">
                <thead>
                  <tr>
                    <th>Community</th>
                    <th>DOB</th>
                    <th>Age</th>
                    <th>Blood Group</th>
                  </tr>
                </thead>
                <tbody>
                  <tr>
                    <td><?php echo $student['community']; ?></td>
                    <td><?php echo $student['dob']; ?></td>
                    <td></td>
                    <td><?php echo $student['blood_group']; ?></td>
                  </tr>
                </tbody>
              </table>
              <div class="row">
                <div class="col-md-4">
                  <label> Name of the Father: </label>
                  <?php echo $student['father_name']; ?>
                </div>
                <div class="col-md-4">
                  <label> Occupation: </label>
                  <?php echo $student['father_occupation']; ?>
                </div>
                <div class="col-md-4">
                  <label> Name of the Mother: </label>
                  <?php echo $student['mother_name']; ?>
                </div>
              </div>
              <div class="row">
                <div class="col-md-4">
                  <label> Occupation: </label>
                  <?php echo $student['mother_occupation']; ?>
                </div>
                <div class="col-md-4">
                  <label> Mobile Number: </label>
                  <?php echo $student['mobileno']; ?>
                </div>
                <div class="col-md-4">
                  <label> Email ID: </label>
                  <?php echo $student['email']; ?>
                </div>
              </div><br><br>
            </div>
            <button class="btn btn-primary" onclick="printDiv('printableArea');">Print</button>
            <br><br><br>
              <?php
            }
            $count++;
          }
          ?>
      </div>

记录逐一显示,我需要通过单击相应行上的打印按钮来单独打印每一行。

我有带 id 的 html

<div class="slide-row" id="printableArea" style="padding:30px;border:2px solid #999;">

和打印按钮

<button class="btn btn-primary" onclick="printDiv('printableArea');">Print</button>

JS

  function printDiv(divName) {
     var printContents = document.getElementById(divName).innerHTML;
     var originalContents = document.body.innerHTML;

     document.body.innerHTML = printContents;

     window.print();

     document.body.innerHTML = originalContents;
}

我尝试了上面的方法,它只会打印第一行记录,但我需要获取每一行的相应打印结果。

最佳答案

Id 在页面中应该是唯一的。根据您的代码,页面中将有许多 id printableArea 的元素。相反,您可以添加动态 ID。

例如,将计数变量添加到 Ids

<div class="slide-row" id="printableArea_<?php echo $count; ?>" style="padding:30px;border:2px solid #999;">

在你的按钮

<button class="btn btn-primary" onclick="printDiv('printableArea_<?php echo $count; ?>');">Print</button>

关于javascript - 如何从 foreach 打印单独的行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49081264/

相关文章:

javascript - 当我使用 React Router ROM 时,组件不会重新渲染

javascript - 如何使用 jquery 添加闪烁效果来完成表格行?

php - 为什么password_verify返回false?

javascript - 如何在 react-admin 中的 Nested ArrayInput/FormDataConsumer 中调用 getSource

javascript - ie7 脚本错误行 0 字符 0 代码 0 jquery 1.10.1

php - 还记得在 php 页面上向 self 提交表单时的 "Radio"值吗?

javascript - Node.js javascript 和 html 脚本无法正常运行

Javascript/JQuery 对象数组按属性交集

javascript - 当前 URL hashchange 事件将新哈希附加到目标 URL 并链接到它

javascript - Delete 方法不适用于 Indexed DB HTML5 ...它返回成功但未删除记录