javascript 分页组件
自己写的一个简单的分页组件,主要功能还有实现都在JS中,html页面中只用增加一个放置生成分页的DIV,并给定容器的id.
html结构如下:
<ul class="pagination" id="pageDIV"> </ul>
class="pagination" 给定了分页的样式,
id="pageDIV"用于放置JS生成的分页 CSS结构如下:
.pagination{
margin-top: 10px;
margin-bottom: 10px;
display: inline-block;
padding-left:;
margin: 20px 0;
border-radius: 4px;
}
.pagination>li {
display: inline;
}
.pagination>li:first-child>a{
margin-left:;
border-top-left-radius: 4px;
border-bottom-left-radius: 4px;
}
.pagination>li>a{
position: relative;
float: left;
padding: 6px 12px;
margin-left: -1px;
line-height: 1.42857143;
color: #337ab7;
text-decoration: none;
background-color: #fff;
border: 1px solid #ddd;
cursor: pointer;
}
.pagination>li>a.navcur{
background: #cccccc;
color: #ffffff;
}
下面是JS结构,注意要引用JQuery
/**
* @pageContentID 渲染分页的DIV元素
* @curPage 当前开始页
* @totalCount 总数量
* @pageRows 每页显示数量
* @callback 显示数据的回调函数
*/
function PageList(pageContentID,option){
this.pageContentID=document.getElementById(pageContentID);
this.curPage=option.curPage;
this.totalCount=option.totalCount;
this.pageRows=option.pageRows;
this.callback=option.callback;
this.pageSize=Math.ceil(this.totalCount/this.pageRows);
}
PageList.prototype={
init:function(){
this.renderbtn();
},
firstpage:function(){
var _self=this;
_self._firstpage=document.createElement("li");
_self._firstpageA=document.createElement("a");
_self._firstpageA.innerHTML="首页";
_self._firstpage.appendChild(_self._firstpageA);
this.pageContentID.appendChild(_self._firstpage);
_self._firstpage.onclick=function(){
_self.gotopage(1);
}
},
lastpage: function () {
var _self=this;
_self._lastpage=document.createElement("li");
_self._lastpageA=document.createElement("a");
_self._lastpageA.innerHTML="尾页";
_self._lastpage.appendChild(_self._lastpageA);
this.pageContentID.appendChild(_self._lastpage);
_self._lastpage.onclick= function () {
_self.gotopage(_self.pageSize);
}
},
prewpage: function () {
var _self=this;
_self._prew=document.createElement("li");
_self._prewA=document.createElement("a");
_self._prewA.innerHTML="<<";
_self._prew.appendChild(_self._prewA);
this.pageContentID.appendChild(_self._prew);
_self._prew.onclick= function () {
if(_self.curPage>1){
_self.curPage--;
}
_self.callback.call(this,this.curPage);
_self.init();
console.log(_self.curPage); }
},
nextpage: function () {
var _self=this;
_self._next=document.createElement("li");
_self._nextA=document.createElement("a");
_self._nextA.innerHTML=">>";
_self._next.appendChild(_self._nextA);
this.pageContentID.appendChild(_self._next);
_self._next.onclick= function () {
if(_self.curPage<_self.pageSize){
_self.curPage++;
}
_self.callback.call(this,this.curPage);
_self.init();
console.log(_self.curPage);
}
},
pagenum: function () {
var _self=this;
if(this.pageSize<=10){
for(var i= 1,len=this.pageSize;i<=len;i++){
_self._num=document.createElement("li");
_self._numA=document.createElement("a");
_self._numA.innerHTML=i;
_self._num.appendChild(_self._numA);
this.pageContentID.appendChild(_self._num);
_self._num.onclick= function () {
var curpage = $(this).text();
_self.gotopage(curpage);
}
}
}
else{
if(_self.curPage<=10){
for(var i= 1;i<=10;i++){
_self._num=document.createElement("li");
_self._numA=document.createElement("a");
_self._numA.innerHTML=i;
_self._num.appendChild(_self._numA);
this.pageContentID.appendChild(_self._num);
_self._num.onclick= function () {
var curpage = $(this).text();
_self.gotopage(curpage);
}
}
}
else if(_self.curPage>10&&_self.curPage<=this.pageSize){
if(this.pageSize<Math.ceil(_self.curPage/10)*10){
for(var i=Math.floor(_self.curPage/10)*10+1;i<=this.pageSize;i++){
if(_self.curPage>this.pageSize)
return;
_self._num=document.createElement("li");
_self._numA=document.createElement("a");
_self._numA.innerHTML=i;
_self._num.appendChild(_self._numA);
this.pageContentID.appendChild(_self._num);
_self._num.onclick= function () {
var curpage = $(this).text();
_self.gotopage(curpage);
}
}
}else{
if(Math.ceil(_self.curPage/10)*10==_self.curPage){
for(var i=_self.curPage-9;i<=_self.curPage;i++){
_self._num=document.createElement("li");
_self._numA=document.createElement("a");
_self._numA.innerHTML=i;
_self._num.appendChild(_self._numA);
this.pageContentID.appendChild(_self._num);
_self._num.onclick= function () {
var curpage = $(this).text();
_self.gotopage(curpage);
}
}
}else{
for(var i=Math.floor(_self.curPage/10)*10+1;i<=Math.ceil(_self.curPage/10)*10;i++){
_self._num=document.createElement("li");
_self._numA=document.createElement("a");
_self._numA.innerHTML=i;
_self._num.appendChild(_self._numA);
this.pageContentID.appendChild(_self._num);
_self._num.onclick= function () {
var curpage = $(this).text();
_self.gotopage(curpage);
}
}
} }
}
}
$(".pagination li").each(function(){
if($(this)[0].innerText==_self.curPage){
$(".pagination li").children("a").removeClass("navcur");
$(this).children("a").addClass("navcur");
}
}); },
gotopage: function (curpage) {
this.curPage=curpage;
this.callback.call(this,this.curPage);
this.init();
console.log(this.curPage);
},
renderbtn: function () {
$(".pagination").html("");
this.firstpage();
this.prewpage();
this.pagenum();
this.nextpage();
this.lastpage();
}
};
$(function(){
var pager = new PageList("pageDIV",{
curPage:1,
totalCount:26,
pageRows:1,
callback:callbackFuc
});
pager.init();
}); function callbackFuc(curpage){ }
说明:
此分页是以10页为标准,低于10页的时候全部显示,大于10页的时候,进行翻页显示余下页数.
调用方法:
$(function(){
var pager = new PageList("pageDIV",{
curPage:1,
totalCount:26,
pageRows:1,
callback:callbackFuc
});
pager.init();
});
javascript 分页组件的更多相关文章
- 基于Vue.js的表格分页组件
有一段时间没更新文章了,主要是因为自己一直在忙着学习新的东西而忘记分享了,实在惭愧. 这不,大半夜发文更一篇文章,分享一个自己编写的一个Vue的小组件,名叫BootPage. 不了解Vue.js的童鞋 ...
- 基于Vue封装分页组件
使用Vue做双向绑定的时候,可能经常会用到分页功能 接下来我们来封装一个分页组件 先定义样式文件 pagination.css ul, li { margin: 0px; padding: 0px;} ...
- Vue.js的表格分页组件
转自:http://www.cnblogs.com/Leo_wl/p/5522299.html 有一段时间没更新文章了,主要是因为自己一直在忙着学习新的东西而忘记分享了,实在惭愧. 这不,大半夜发文更 ...
- vue2.0实现分页组件
最近使用vue2.0重构项目, 需要实现一个分页的表格, 没有找到合适的组件, 就自己写了一个, 效果如下: 该项目是使用 vue-cli搭建的, 如果你的项目中没有使用webpack,请根据代码自己 ...
- asp.mvc中的vue分页实例,分页组件无法重置reload,解决点击查询按钮后,分页不刷新的问题
刚刚接触Vue.js,现在需要做一个查询功能,并且进行服务端分页.主要思路是在页面中注册一个分页组件,然后进行调用.代码如下 1.引用vue.js,具体去网上下载 2.在html的body中添加如下代 ...
- Django----列表分页(使用Django的分页组件)
目的:是为了实现列表分页 1.定制URL http://127.0.0.1:8000/blog/get_article?page=3之前定制URL是在url后增加了/id,这次使用参数的方式 def ...
- vuejs2.0实现分页组件,使用$emit进行事件监听数据传递
上一篇文章介绍了vuejs实现的简单分页,如果我有几个页面都需要有分页效果,不可能每个页面都去复制一下这段代码吧,意思是封装一下,变成通用的组件. 首先使用基础 Vue 构造器,创建一个“子类”,Vu ...
- Python自定义分页组件
为了防止XSS即跨站脚本攻击,需要加上 safe # 路由 from django.conf.urls import url from django.contrib import admin from ...
- Angular4.+ ngx-bootstrap Pagination 自定义分页组件
Angular4 随笔(二) ——自定义分页组件 1.简介 本组件主要是实现了分页组件显示功能,通过使用 ngx-bootstrap Pagination分页组件实现. 基本逻辑: 1.创建一个分页 ...
随机推荐
- 同时使用python2和Python3
问题:thrift生成的是python2代码,之前使用的是Python3因此需要同时使用两个版本. 方案:将python3的可执行文件重命名为python3(默认为Python),这样使用pyhton ...
- mybatis底层源码分析之--配置文件读取和解析
现在企业级开发中ssm是很常见的技术标配,mybatis比hibernate轻量了很多,而且学习成本相对较低,简单易上手. 那么,问题来了,简单好用的mybatis底层到底是如何实现的呢?都使用了什么 ...
- JSTL标签出错:<c:forEach var="book" items="${requestScope.books}" varStatus="status">
今天在运行书里的JSTL标签代码的时候出错,总结一下: 问题1.The JSP specification requires that an attribute name is preceded by ...
- c#组元(Tuple)的使用
组元(Tuple)是C# 4.0引入的一个新特性,可以在.NET Framework 4.0或更高版本中使用.组元使用泛型来简化类的定义,多用于方法的返回值.在函数需要返回多个类型的时候,就不必使用o ...
- JS中的_proto_
var grandfather = function(){ this.name = "LiuYashion" ; } var father = function(){}; fath ...
- Rectangle Area || LeetCode
把交叉点的坐标求出来即可. #define max(a,b) ( (a)>(b)?(a):(b) ) #define min(a,b) ( (a)<(b)?(a):(b) ) int co ...
- 换手率的公司使用MQTT的框架
向换手率公司学习.该公司貌似也使用 Golang
- 【转】PHP实现连贯操作
[第一种方案 __call] 我们在使用一些框架(如ThinkPHP)编码的时候,常用到这样的代码. M('User')->where(array('id'=>1))->field( ...
- c#读取Word模板,利用书签替换内容包括表格
//生成WORD程序对象和WORD文档对象 Microsoft.Office.Interop.Word.Application appWord = new Microsoft.Office.Inter ...
- 实用命令dd
1.命令简介 dd 的主要选项: 指定数字的地方若以下列字符结尾乘以相应的数字: b=512, c=1, k=1024, w=2, xm=number m if=file #输入文件名,缺省为标准输入 ...