Reputation: 521
On JQuery Datatable I have following pagination style.
|Previous|1| ....|4|5|6|...|13|Next|
But I want something like this.
|Previous|1| ...|3|4|5|6|7|...|13|Next|
In both cases, 5 is the current page. Is there any way we can achieve that? Many Thanks.
Upvotes: 0
Views: 878
Reputation: 521
I found the solution from a similar type of StackOverflow question.
DataTables change number of pagination buttons
And the solution for above question is to load datatable and add the following line of code as a requirement.
<script type="text/javascript">
$.fn.DataTable.ext.pager.numbers_length = 9;
// number need to be odd number; default is 7
</script>
Upvotes: 0
Reputation: 2889
Like this:
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);
.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;
}
<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);">
Upvotes: 1