JQuery Datatables分页:显示当前页面中的2个前一个和下一个2

时间:2018-05-04 13:23:06

标签: jquery pagination datatables

在JQuery上Datatable我有以下分页风格。

|Previous|1| ....|4|5|6|...|13|Next|

但我想要这样的东西。

|Previous|1| ...|3|4|5|6|7|...|13|Next|

在这两种情况下,5是当前页面。有什么办法可以实现吗?非常感谢。

2 个答案:

答案 0 :(得分:1)

像这样:



var count_of_pages=13;

function set_pages(_page){
   $('#test').empty();
   if(_page==1){
   	     //start no act
   	   $('#test').append('<div class="white-btn-noactive">Previous</div>');
   }else{
   	     //start act
   	   $('#test').append('<div class="white-btn" onclick="alert(\'start page '+(_page-1)+'\');set_pages('+(_page-1)+');">Previous</div>');
   }
   if(_page>3){
        //1
      $('#test').append('<div class="white-btn" onclick="alert(\'start page '+1+'\');set_pages(1);">'+1+'</div>');
       //.....
      $('#test').append('<div style="display:inline-block;">...</div>');
   }
   for(var i = 1; i < count_of_pages+1; i++) {
   	     if(((i-2)==_page)||((i-1)==_page)||(i==_page)||((i+1)==_page)||((i+2)==_page)){
   	  	     if(i==_page){
   	  	     	$('#test').append('<div class="white-btn-active">'+i+'</div>');
   	  	     }else{
   	  	     	$('#test').append('<div class="white-btn" onclick="alert(\'start page '+i+'\');set_pages('+i+')">'+i+'</div>');
   	  	     }
   	     }
   	  }
   if(_page<count_of_pages-3){
        ///....
        $('#test').append('<div style="display:inline-block;">...</div>');
        //count_of_pages
        $('#test').append('<div class="white-btn" onclick="alert(\'start page '+count_of_pages+'\');set_pages('+count_of_pages+');">'+count_of_pages+'</div>');
   }
   if(_page==count_of_pages){
   	   //end no act
   	  $('#test').append('<div class="white-btn-noactive">Next</div>');
   }else{
   	   //end act
   	  $('#test').append('<div class="white-btn" onclick="alert(\'start page '+(_page+1)+'\');set_pages('+(_page+1)+');">Next</div>');
   }

}

set_pages(6);
&#13;
.white-btn{
  	display:inline-block;
  	margin:1px;
  	padding:2px;
  	padding-left:6px;
  	padding-right:6px;
    border-style:solid;
    border-width:2px;
    border-color:gray;
    color:gray;
    border-radius:4px;
    font-weight:bold;
    cursor:pointer;
  }
  .white-btn:hover{
  	border-color:black;
  	color:black;
  }
  .white-btn-active{
  	display:inline-block;
  	margin:1px;
  	padding:2px;
  	padding-left:6px;
  	padding-right:6px;
    border-style:solid;
    border-width:2px;
    border-color:red;
    color:red;
    border-radius:4px;
    font-weight:bold;
  }
  .white-btn-noactive{
  	display:inline-block;
  	margin:1px;
  	padding:2px;
  	padding-left:6px;
  	padding-right:6px;
    border-style:solid;
    border-width:2px;
    border-color:gray;
    color:gray;
    border-radius:4px;
    font-weight:bold;
  }
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>


<div id="test"></div>


</br></br>
<input type="button" value="set page 5" onclick="set_pages(5);">
<input type="button" value="set page 9" onclick="set_pages(9);">
<input type="button" value="set page 13" onclick="set_pages(13);">
<input type="button" value="set page 1" onclick="set_pages(1);">
<input type="button" value="set page 3" onclick="set_pages(3);">
&#13;
&#13;
&#13;

答案 1 :(得分:0)

我从类似的StackOverflow问题中找到了解决方案。

DataTables change number of pagination buttons

以上问题的解决方案是加载datatable并添加以下代码行作为要求。

<script type="text/javascript">
    $.fn.DataTable.ext.pager.numbers_length = 9;
    // number need to be odd number; default is 7
</script>