首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >引导DataTables -不适用于动态网格系统的分页

引导DataTables -不适用于动态网格系统的分页
EN

Stack Overflow用户
提问于 2019-11-21 08:59:00
回答 2查看 1.1K关注 0票数 1

我试图在网格视图中显示用户列表。

我的密码:

下面是一个简单的php数组。

代码语言:javascript
复制
<?php $aso_arr = array( 
    "1"=>"1",  
    "2"=>"2",  
    "3"=>"3",  
    "4"=>"4",
    "5"=>"5",  
    "6"=>"6",  
    "7"=>"7",
    "8"=>"8",  
    "9"=>"9",  
    "10"=>"10",  
    "11"=>"11",
    "12"=>"12",  
    "13"=>"13",  
    "14"=>"14",
    "15"=>"15",  
    "16"=>"16",  
    "17"=>"17",  
    "18"=>"18",
    "19"=>"19",  
    "20"=>"20",  
    "21"=>"21"
); ?>

而桌子的结构-

代码语言:javascript
复制
<table id="example" class="table table-striped table-bordered" >
<thead>
</thead>
<tbody>
<tr>
<?php $i=0; foreach($aso_arr as $side=>$s) { ?>
<td>
<div class="card" >
<div class="card-img-top" ></div>
<div class="card-body text-center">
<img class="avatar rounded-circle" src="https://s3.eu-central-1.amazonaws.com/bootstrapbaymisc/blog/24_days_bootstrap/robert.jpg" alt="Bologna">
<h4 class="card-title">Robert Downey Jr.</h4>
<h6 class="card-subtitle mb-2 text-muted">Famous Actor</h6>
<p class="card-text">Robert John  </p>
<a href="#" class="btn btn-info">View Profile</a>
</div>
</div>
</div>
</td>
<?php  $i++; if ($i % 4 == 0) {echo '</tr><tr>';}  } ?>
</tbody>
</table>
<script>
$(document).ready(function() {
$('#example').DataTable();
} );
</script>

<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<script src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.10.20/js/dataTables.bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.20/css/dataTables.bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

我在每四个元素的间隔内创建了一个新行。这张桌子工作得很好,如果我去掉了前叶循环的话,分页也很好。但是有了循环,分页就不能工作了。请帮助不要知道为什么它不起作用。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-11-21 09:24:32

看来您的tbody坏了,这就是为什么DataTable不能正常工作的原因。

尝试用next替换theadtbody

代码语言:javascript
复制
<?php  
$aso_arr = array( 
    "1"=>"1",  
    "2"=>"2",  
    "3"=>"3",  
    "4"=>"4",
    "5"=>"5",  
    "6"=>"6",  
    "7"=>"7",
    "8"=>"8",  
    "9"=>"9",  
    "10"=>"10",  
    "11"=>"11",
    "12"=>"12",  
    "13"=>"13",  
    "14"=>"14",
    "15"=>"15",  
    "16"=>"16",  
    "17"=>"17",  
    "18"=>"18",
    "19"=>"19",  
    "20"=>"20",  
    "21"=>"21"
); ?>
代码语言:javascript
复制
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.20/css/dataTables.bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<table id="example" class="table table-striped table-bordered" style="width:100%">
<thead>
    <tr>
        <th></th>
        <th></th>
        <th></th>
        <th></th>
        <th></th>
    </tr>
</thead>
<tbody>
<?php $num = 1; foreach($aso_arr as $side=>$s) { ?>
   <?php if ($num % 4 == 1) {?>
 <tr>
    <td style="display:none;"><?= $num; ?></td>
   <?php } ?>
    <td>
       <div class="card" >
       <div class="card-img-top" ></div>
       <div class="card-body text-center">
       <img class="avatar rounded-circle" src="https://s3.eu-central-1.amazonaws.com/bootstrapbaymisc/blog/24_days_bootstrap/robert.jpg" alt="Bologna">
       <h4 class="card-title">Robert Downey Jr.<?= $num; ?></h4>
       <h6 class="card-subtitle mb-2 text-muted">Famous Actor</h6>
       <p class="card-text">Robert John  </p>
       <a href="#" class="btn btn-info">View Profile</a>
       </div>
       </div>
       </div>
    </td>
    <?php if ($num % 4 == 0) {?>
 </tr>
     <?php } ?>

<?php $num++; } ?>

     <?php $td = 4 - count($aso_arr) % 4; if ($td != 0): ?>
            <?php for($i=0;$i< $td; $i++): ?>
            <td></td>
            <?php endfor;?>
     <?php endif; ?>
</tbody>
</table>

<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<script src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.10.20/js/dataTables.bootstrap.min.js"></script>

JS

代码语言:javascript
复制
<script>
    $(document).ready(function() {
        $('#example').DataTable({       
          "order": [[ 0, "asc" ]],
          "lengthMenu": [ 2, 25, 50, 75, 100 ]
        });
    } );
</script>

thead中添加与td相同数量的th

此循环将创建4列(td),其中一行(tr)为$aso_arr

票数 2
EN

Stack Overflow用户

发布于 2019-11-21 09:43:13

代码语言:javascript
复制
<?php  

$aso_arr = array( 
    "1"=>"1",  
    "2"=>"2",  
    "3"=>"3",  
    "4"=>"4",
    "5"=>"5",  
    "6"=>"6",  
    "7"=>"7",
    "8"=>"8",  
    "9"=>"9",  
    "10"=>"10",  
    "11"=>"11",
    "12"=>"12",  
    "13"=>"13",  
    "14"=>"14",
    "15"=>"15",  
    "16"=>"16",  
    "17"=>"17",  
    "18"=>"18",
    "19"=>"19",  
    "20"=>"20",  
    "21"=>"21",
); ?>

<!DOCTYPE html>
<html>
<head>
  <title></title>
  <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.20/css/dataTables.bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> 
</head>
<body>
 <table id="example" class="table table-striped table-bordered" style="width:100%">
  <thead>
    <tr>
      <th></th><th></th><th></th><th></th>
    </tr>
  </thead>

        <tbody>
           <?php  

            $loop_counter = round((sizeof($aso_arr))/4); 

            $split_array = array_chunk($aso_arr,4);  
            $loop_counter = sizeof($split_array); 
             for ($j=0; $j < $loop_counter; $j++) {   
            ?>

            <tr>
               <?php 
               $array_length = sizeof($split_array[$j]);
               $colspan = 0;
                for($i=0;$i < $array_length;$i++){
                  if($array_length < 4){ 
                    $colspan = 4-$array_length;
                  }
                 ?>
                <td >
                    <div class="card" >
                    <div class="card-img-top" ></div>
                    <div class="card-body text-center">
                      <img class="avatar rounded-circle" src="https://s3.eu-central-1.amazonaws.com/bootstrapbaymisc/blog/24_days_bootstrap/robert.jpg" alt="Bologna">
                      <h4 class="card-title">Robert Downey Jr.</h4>
                      <h6 class="card-subtitle mb-2 text-muted">Famous Actor</h6>
                      <p class="card-text">Robert John  </p>
                      <a href="#" class="btn btn-info">View Profile</a>
                      <!--<a href="#" class="btn btn-outline-info">Message</a>-->
                    </div>
                  </div>
                </div>
                </td>
                <?php 
                if($colspan > 0){
                for ($k=0; $k < $colspan; $k++) { ?>
                   <td style="display: none"></td>

           <?php     }}
            ?>
            <?php }
            echo "</tr>";
          }?>

        </tbody>
   <tfoot> 
    <tr>
      <th></th><th></th><th></th><th></th>
    </tr>
  </tfoot>
    </table>


<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<script src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.10.20/js/dataTables.bootstrap.min.js"></script>



<script>
    $(document).ready(function() {
        $('#example').DataTable({
          "pageLength": 2
        });
    } );
</script>


</body>
</html>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58970874

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档