【问题标题】:Is it possible to do this pagination without backgrid?是否可以在没有背景网格的情况下进行分页?
【发布时间】:2018-02-13 19:03:10
【问题描述】:

我正在尝试实现像datetable这样的服务器端分页,我发现backbone.paginator library,我不知道是否还有其他东西。

通过他们的示例,他们使用了另一个库来帮助实现这一目标,即backgrid.jsits paginator plugin

是否可以在没有 Backgrid 的情况下进行分页?您能否添加任何示例

【问题讨论】:

  • 为什么不想使用分页器插件?你想做什么?场外推荐是题外话。
  • @EmileBergeron 实际上我仅限于可以添加到项目中的库数量,顺便说一下,使用 backgrid 的目的是什么以及它如何简化分页的创建?(我应该添加这个问题?)
  • @BasheerAL-MOMANI 你不能看一下backgrid的来源,然后自己判断是值得自己实现还是仅仅使用现有的库?
  • Backgrid 和 paginator 做不同的事情。 Backgrid 只处理动态表格数据,而 paginator 只是一个处理分页端点的特殊集合,具有最少的配置和解析。
  • 我曾经需要对长列表进行无限滚动样式的分页,其中所有加载的项目都将被渲染,并且额外的页面(块)将被加载和渲染。我tried using Paginator 但它专注于呈现页面,解决这个问题太痛苦了。所以我推出了自己的infinite collection implementation

标签: javascript backbone.js pagination backgrid backbone.paginator


【解决方案1】:

我做到了

View initialize函数中

我打电话

this.reloadCustomPages();

这里是它的实现

reloadCustomPages: function(options) {
    var self = this;
    self.block();
    self.customPages.fetch({data: $.param(options)}).always(function () {
        self.unblock();
    });
}

在服务器端(java spring)我修改了api以接受新的查询字符串

@RequestParam(value = "pageNumber", defaultValue = "1"),
@RequestParam(value = "perPage", defaultValue = "10")

而不是直接返回列表,而是返回有用的分页信息,例如

  • 数据库中的项目总数
  • 当前页数
  • 页面大小
  • 以及物品本身

检查这个服务器端 sn-p(我知道它不相关但值得一看)

@RequestMapping(value = "/editor/pages", method = RequestMethod.GET)
public void listPages(@RequestParam(value = "pageNumber", defaultValue = "1") Integer pageNumber,
                          @RequestParam(value = "perPage", defaultValue = "10") Integer perPage,
                          HttpServletResponse httpResp) throws IOException {

    Long recordsTotal = pageSvc.findTotalNumberOfPages();// select count(*) from table_name    
    List<PbCustomPage> pages = pageSvc.findPages(pageNumber, perPage);// server side query that gets a pagenated data 

    BackbonePaginatorResponse response = new BackbonePaginatorResponse();// I created this simple class 
    response.setTotalCount(recordsTotal);
    response.setPageNumber(pageNumber);
    response.setPerPage(perPage);
    response.setItems(pages);

    httpResp.setContentType("application/json");
    json.createCustomPageSerializer().addProperties().serialize(response, httpResp.getWriter());// just make your server send that obkect
}

现在返回函数调用,直到服务器获取页面大小为 10 的页面 1

在我的模板中,我有这个

<div class="pagination clear" style="text-align: center;">
    <%= customPages.paginationHtml %>
</div>

让我告诉你我是如何填充它的

customPages.paginationHtml = this.generatePagination(customPages);

这是最重要的部分

generatePagination: function (paginationResponse) {
    var currentPage = paginationResponse.pageNumber,
        lastPage = paginationResponse.totalCount==0?1:Math.ceil(paginationResponse.totalCount/paginationResponse.perPage);
    var html = '<ul class="pagination">';
    html += '<li class="'+(currentPage == 1?"disabled":"")+'" data-pb-page-number="1"><a href="#" class="first">&laquo;&laquo;</a></li>';
    html += '<li class="'+(currentPage == 1?"disabled":"")+'" data-pb-page-number="'+(currentPage==1?1:currentPage-1)+'"><a href="#" class="prev">&laquo;</a></li>';


    for (var i = 1; i <= lastPage; i++) {
         html += '<li class="'+(currentPage == i?"active":"")+'" data-pb-page-number="'+i+'"><a href="#" class="page">'+ i +'</a></li>';
    }

    html += '<li class="'+(lastPage == currentPage?"disabled":"")+'" data-pb-page-number="'+(currentPage==lastPage?lastPage:currentPage+1)+'"><a href="#" class="next">&raquo;</a></li>';
    html += '<li class="'+(lastPage == currentPage?"disabled":"")+'" data-pb-page-number="'+(lastPage)+'"><a href="#" class="last">&raquo;&raquo;</a></li>';

    html += '</ul>';
    return html;
},

我创建的每个 li 都有data-pb-page-number 供以后使用

还有一件事,如何向其他页面发出新请求

View initialize函数中

this.el.on('click', 'ul.pagination li:not(.disabled,.active)', this.getCustomPagesPagination);

这里是它的实现

getCustomPagesPagination: function (e) {
    e.preventDefault();
    var $el = $(e.target).closest("li");
    this.reloadCustomPages({pageNumber:$el.data("pb-page-number")})
},

结果是这样的

这就是我解决问题的方法,但问题仍未得到解答

【讨论】:

    【解决方案2】:

    我开发了以下代码与您分享一个用于服务器站点分页的JS / php代码,您可以根据需要更改它,稍后我可能会更好地总结它。

    首先连接数据库并创建分页表:

      CREATE TABLE `pagination` (
     `id` INT( 16 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
     `name` VARCHAR( 50 ) NOT NULL ,
     `age` INT( 3 ) NOT NULL ,
     `company` VARCHAR( 50 ) NOT NULL
      ) 
    

    第二次插入400条相同的记录:

       $i=0;
       while ($i< 400){
       $pag="insert into pagination (id,name,age,company) VALUES 
      ('NULL','john','40','google')";
      $excu=$mysqli->query($pag);
       $i++;
           }
    

    然后制作test.php

     <!DOCTYPE html>
     <html><head>
     <style>
     #container{overflow-x: hidden;max-width:90%;min-width:90% ;margin: 0 auto;}
     td{max-width:10%;min-width:10%}
     tr,td{border:1px solid black }
     #page{display:inline;border:1px solid black}
     #numb,#numbs{display:none}
    .button{background:white}
     </style>
     </head>
     <body >
     <?php $defaultoption ="10";?>
     <div id=container></div><span id=numb></span><span id=numbs><?php echo 
     $defaultoption;?></span>
     <script type=text/javascript  src=js.js></script>
     </body>
     </html>
    

    然后制作js.js:

      onload = function (){ 
      var container=document.getElementById("container");
      var table =document.getElementById("numbs").innerHTML;
       var xhttp = new XMLHttpRequest();
       xhttp.onreadystatechange = function() {
      if (xhttp.readyState == 4 && xhttp.status == 200) {
    
      container.innerHTML=xhttp.responseText;
     var button=document.getElementById("button");
     button.children[0].disabled="true";
     button.children[0].style.background="yellow";
                 }}  
     xhttp.open("POST", "testa.php", true);
     xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xhttp.send("table="+table);
        }      
    
    
     function select(){
     var sel =document.getElementById("select");
     var table=sel.options[sel.selectedIndex].value;
     var b2 =document.getElementById("numbs");
     b2.innerHTML=table;
     var se = new XMLHttpRequest();
     se.onreadystatechange = function() {
      if (se.readyState == 4 && se.status == 200) {
      document.getElementById("container").innerHTML=se.responseText;
      var button=document.getElementById("button");
      button.children[0].disabled="true";
       button.children[0].style.background="yellow";
    
           }}  
       se.open("POST", "testa.php", true);
       se.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
       se.send("table="+table);
       }
    
    
    
    
    
       function button(button){
       var b2 =document.getElementById("numbs");
       var b1 =button.innerHTML;
       var numb = document.getElementById("numb"); 
       numb.innerHTML=b1;
       var b= b2.innerHTML; 
       var butt = new XMLHttpRequest();
         butt.onreadystatechange = function() {
       if (butt.readyState == 4 && butt.status == 200) {
    
      document.getElementById("container").innerHTML=butt.responseText;
      var d = document.getElementsByClassName("button");
      for(i=0;i<d.length;i++){
      if(d[i].innerHTML == numb.innerHTML){d[i].disabled="true";
      d[i].style.background="yellow";
        }}}}
    
      butt.open("POST", "testa.php", true);
      butt.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
       butt.send("b="+b+"&b1="+b1);
      }
    

    制作 testa.php :

      <?php
      $pag="SELECT * FROM pagination ORDER BY id ASC ";
      $exc=$mysqli->query($pag);
      $numrwos =mysqli_num_rows($exc);
      if($_POST['b']){
      $b =$_POST['b'];
      $b1 =$_POST['b1'];
      $max=($b*$b1);
      $m =($b1-1);
      $n =($m*$b);
      $min =($n-1);
      if($numrwos){
      echo "<select id=select onchange=select()><option selected=selected>Show Entries</option>";
      if($numrwos < 11){
      echo "<option>10</option>";
      }elseif($numrwos > 10 && $numrwos < 26){
      echo "<option>10</option><option>25</option>"; }elseif($numrwos > 25 && 
      $numrwos < 51){
      echo "<option>10</option><option>25</option><option>50</option>";}else{
      echo "<option>10</option><option>25</option><option>50</option>
      <option>100</option>";}
      echo "</select><br />";
      echo "<table width=80% id=table>";
      echo "<tr><td width=10%>id</td><td>name</td><td>age</td><td>company</td></tr>";
      $stop="0";
      while ($result=mysqli_fetch_array($exc)){
      if($stop > $min && $stop < $max){
      echo "<tr><td>".$result['id']."</td><td>".$result['name']."</td>
      <td>".$result['age']."</td><td>".$result['company']."</td></tr>";
      }else{}
      $stop++;
     }echo "</table><br />";
      echo "<div id=button>";
      $i=0;
      while($i < $numrwos){
      $page+=$i % $b == $b-1;
      $i++;
      if($i%$b == 1){
      $pagenum=$page+1;
      echo "<button class=button onmouseover='button(this)'>".$pagenum."</button>";}}
     echo "</div>";
     }else{echo "no records";}
    
     }else{
     $t =$_POST["table"];
     if($numrwos){
     echo "<select id=select onchange=select()><option selected=selected>Show Entries</option>";
     if($numrwos < 11){
     echo "<option>10</option>";
     }elseif($numrwos > 10 && $numrwos < 26){
     echo "<option>10</option><option>25</option>"; }elseif($numrwos > 25 && $numrwos < 51){
      echo "<option>10</option><option>25</option><option>50</option>";}else{
      echo "<option>10</option><option>25</option><option>50</option>
      <option>100</option>";}
      echo "</select><br />";
      echo"<table width=80% id=table>";
       echo "<tr><td width=10%>id</td><td>name</td><td>age</td><td>company</td></tr>";
      $stop="0";
      while ($result=mysqli_fetch_array($exc)){
      echo "<tr><td>".$result['id']."</td><td>".$result['name']."</td>
       <td>".$result['age']."</td><td>".$result['company']."</td></tr>";
      if($stop ==$t-1){break;}
      $stop++;
      }echo "</table><br />";
      echo "<div id=button>";
      $i=0;
      while($i < $numrwos){
      $page+=$i % $t == $t-1;
       $i++;
      if($i%$t == 1){
      $pagenum=$page+1;
       echo "<button class=button onmouseover='button(this)'>".$pagenum."</button>";}}
     echo "</div>";
      }else{echo "no records";}
      } 
      ?>
    

    希望对你有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-22
      • 1970-01-01
      • 2011-01-04
      • 1970-01-01
      • 1970-01-01
      • 2022-11-03
      • 2011-07-24
      相关资源
      最近更新 更多