【问题标题】:dataTables with dynamic id's具有动态 ID 的数据表
【发布时间】:2017-03-03 15:54:36
【问题描述】:

我正在使用带有 HTML、php 和 MySQLjQuery 的数据表插件。当我明确设置表的 id 时一切正常,但如果我循环 MySQL 结果 jquery 不会“理解”表 id。也许用代码更清楚。

DataTable 工作正常:

<table class="table table-bordered table-striped mb-none" id="datatable-tabletools">
  <thead>
    <tr>
      <th style="width: 60px; text-align: center;">Id</th>
      <th style="width: 400px; text-align: center;">Info A</th>
      <th style="width: 390px; text-align: center;">Info B</th>
      <th style="width: 100px; text-align: center;">Actions</th>
    </tr>
  </thead>
  <tbody>
    <!-- tbody dynamically populated -->
  </tbody>
</table>

  <script type="text/javascript">
    $(document).ready(function () {
    $('#datatable-tabletools').DataTable({
     });
   });
 </script>

数据表不工作:

<?php
foreach ($sections as $section)
{
   $insc = Inscripciones::getAllInscriptionBySection($section -> getId());
   if (count($insc) > 0)
   {
     ?>
     <h2 style="margin-bottom: 50px;"><?php echo $section->getNombre(); ?></h2>
     <table class="table table-bordered table-striped mb-none" id="datatable-tabletools-<?php echo $section -> getId(); ?>">
       <thead>
         <tr>
           <th style="width: 60px; text-align: center;">Id</th>
           <th style="width: 450px; text-align: center;">Category</th>
           <th style="width: 390px; text-align: center;">Title</th>
           <th style="width: 140; text-align: center;">Usuario</th>
           <th style="width: 100px; text-align: center;">Acciones</th>
         </tr>
       </thead>
       <tbody>
         <!-- tbody also dynamically populated -->
       </tbody>
     </table>
  <?php
  }
}
  ?>



    <script type="text/javascript">
          $(document).ready(function () {
           $('#datatable-tabletools-1').DataTable({
           });
           $('#datatable-tabletools-2').DataTable({
           })
           $('#datatable-tabletools-3').DataTable({
           })
          });
   </script>

奇怪的是,在渲染的源代码中,table id 显示为“datatable-tabletools-1”、“datatable-tabletools-2”等等......

提前致谢!

【问题讨论】:

    标签: php jquery datatable datatables


    【解决方案1】:

    我认为您当前的代码没有任何问题。 您可以尝试使用类选择器应用数据表。 $('.table').DataTable({ });

    【讨论】:

    • 嗨@Pramod,我尝试在一个新的、干净的脚本中使用类,并且工作起来就像一个魅力,所以我想我错过了其他东西......
    • 太棒了!只要您有通用的数据表初始化代码,类应该没问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-24
    • 1970-01-01
    • 2015-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多