Comparing version 1.1.1 to 1.1.2
@@ -1,101 +0,95 @@ | ||
/*! | ||
/** | ||
@Name : layPage v1.x - Node.js 分页模块 | ||
@Name: layPage for Node.js | ||
@Author: 贤心 | ||
@Site:http://www.layui.com/ | ||
@License:MIT | ||
*/ | ||
function laypage(options){ | ||
return new Page(options).render() | ||
} | ||
var index = 0, Page = function(options){ | ||
var that = this; | ||
that.query = /\/page\/\d+?(\/*)$/; | ||
that.config = options || {}; | ||
var url = require('url'), Page = function(options){ | ||
var that = this; | ||
that.query = /\/page\/\d+?(\/*)$/; | ||
that.config = options || {}; | ||
}; | ||
laypage.v = '1.x'; | ||
//分页视图 | ||
Page.prototype.view = function(){ | ||
var that = this, conf = that.config, view = [], dict = {}; | ||
conf.pages = conf.pages|0; | ||
conf.curr = (conf.curr|0) || 1; | ||
conf.groups = 'groups' in conf ? (conf.groups|0) : 3; | ||
conf.first = 'first' in conf ? conf.first : 1; | ||
conf.last = 'last' in conf ? conf.last : conf.pages; | ||
conf.prev = 'prev' in conf ? conf.prev : '\u4e0a\u4e00\u9875'; | ||
conf.next = 'next' in conf ? conf.next : '\u4e0b\u4e00\u9875'; | ||
conf.className = conf.className || 'laypage'; | ||
conf.url = conf.url||''; | ||
if(!that.query.test(conf.url)){ | ||
conf.url = conf.url.replace(/\/*$/, '/page/'); | ||
var that = this, conf = that.config, view = [], dict = {}; | ||
var urls = url.parse(conf.url||''); | ||
conf.pages = conf.pages|0; | ||
conf.curr = (conf.curr|0) || 1; | ||
conf.groups = 'groups' in conf ? (conf.groups|0) : 5; | ||
conf.first = 'first' in conf ? conf.first : '\u9996\u9875'; | ||
conf.last = 'last' in conf ? conf.last : '\u5C3E\u9875'; | ||
conf.prev = 'prev' in conf ? conf.prev : '\u4e0a\u4e00\u9875'; | ||
conf.next = 'next' in conf ? conf.next : '\u4e0b\u4e00\u9875'; | ||
conf.className = conf.className || 'laypage'; | ||
conf.url = urls.pathname||''; | ||
if(!that.query.test(conf.url)){ | ||
conf.url = conf.url.replace(/\/*$/, '/page/'); | ||
} | ||
conf.url = conf.url.replace(that.query, '/page/'); | ||
if(conf.groups > conf.pages){ | ||
conf.groups = conf.pages; | ||
} | ||
urls.search = urls.search || ''; | ||
//计算当前组 | ||
dict.index = Math.ceil((conf.curr + ((conf.groups > 1 && conf.groups !== conf.pages) ? 1 : 0))/(conf.groups === 0 ? 1 : conf.groups)); | ||
//当前页非首页,则输出上一页 | ||
if(conf.curr > 1 && conf.prev){ | ||
view.push('<a href="'+ conf.url + (conf.curr - 1) +'/'+ urls.search +'" class="'+ conf.className +'-prev">'+ conf.prev +'</a>'); | ||
} | ||
//当前组非首组,则输出首页 | ||
if(dict.index > 1 && conf.first && conf.groups !== 0){ | ||
view.push('<a href="'+ conf.url +'1/'+ urls.search +'" class="'+ conf.className +'-first" title="\u9996\u9875">'+ conf.first +'</a><span>\u2026</span>'); | ||
} | ||
//输出当前页组 | ||
dict.poor = Math.floor((conf.groups-1)/2); | ||
dict.start = dict.index > 1 ? conf.curr - dict.poor : 1; | ||
dict.end = dict.index > 1 ? (function(){ | ||
var max = conf.curr + (conf.groups - dict.poor - 1); | ||
return max > conf.pages ? conf.pages : max; | ||
}()) : conf.groups; | ||
if(dict.end - dict.start < conf.groups - 1){ //最后一组状态 | ||
dict.start = dict.end - conf.groups + 1; | ||
} | ||
for(; dict.start <= dict.end; dict.start++){ | ||
if(dict.start === conf.curr){ | ||
view.push('<span class="'+ conf.className +'-curr">'+ dict.start +'</span>'); | ||
} else { | ||
view.push('<a href="'+ conf.url + dict.start +'/'+ urls.search +'">'+ dict.start +'</a>'); | ||
} | ||
conf.url = conf.url.replace(that.query, '/page/'); | ||
if(conf.groups > conf.pages){ | ||
conf.groups = conf.pages; | ||
} | ||
//计算当前组 | ||
dict.index = Math.ceil((conf.curr + ((conf.groups > 1 && conf.groups !== conf.pages) ? 1 : 0))/(conf.groups === 0 ? 1 : conf.groups)); | ||
//当前页非首页,则输出上一页 | ||
if(conf.curr > 1 && conf.prev){ | ||
view.push('<a href="'+ conf.url + (conf.curr - 1) +'/" class="'+ conf.className +'-prev">'+ conf.prev +'</a>'); | ||
} | ||
//当前组非首组,则输出首页 | ||
if(dict.index > 1 && conf.first && conf.groups !== 0){ | ||
view.push('<a href="'+ conf.url +'1/" class="'+ conf.className +'-first" title="\u9996\u9875">'+ conf.first +'</a><span>\u2026</span>'); | ||
} | ||
//输出当前页组 | ||
dict.poor = Math.floor((conf.groups-1)/2); | ||
dict.start = dict.index > 1 ? conf.curr - dict.poor : 1; | ||
dict.end = dict.index > 1 ? (function(){ | ||
var max = conf.curr + (conf.groups - dict.poor - 1); | ||
return max > conf.pages ? conf.pages : max; | ||
}()) : conf.groups; | ||
if(dict.end - dict.start < conf.groups - 1){ //最后一组状态 | ||
dict.start = dict.end - conf.groups + 1; | ||
} | ||
for(; dict.start <= dict.end; dict.start++){ | ||
if(dict.start === conf.curr){ | ||
view.push('<span class="'+ conf.className +'-curr">'+ dict.start +'</span>'); | ||
} else { | ||
view.push('<a href="'+ conf.url + dict.start +'/">'+ dict.start +'</a>'); | ||
} | ||
} | ||
//总页数大于连续分页数,且当前组最大页小于总页,输出尾页 | ||
if(conf.pages > conf.groups && dict.end < conf.pages && conf.last && conf.groups !== 0){ | ||
view.push('<span>\u2026</span><a href="'+ conf.url + conf.pages +'/" class="'+ conf.className +'-last" title="\u5c3e\u9875">'+ conf.last +'</a>'); | ||
} | ||
//当前页不为尾页时,输出下一页 | ||
dict.flow = !conf.prev && conf.groups === 0; | ||
if(conf.curr !== conf.pages && conf.next || dict.flow){ | ||
view.push((function(){ | ||
return (dict.flow && conf.curr === conf.pages) | ||
? '<span class="'+ conf.className +'-nomore" title="\u5df2\u6ca1\u6709\u66f4\u591a">'+ conf.next +'</span>' | ||
: '<a href="'+ conf.url + (conf.curr + 1) +'/" class="'+ conf.className +'-next">'+ conf.next +'</a>'; | ||
}())); | ||
} | ||
if(conf.pages <= 1){ | ||
return ''; | ||
} | ||
return '<div name="laypage'+ laypage.v +'" class="'+ conf.className +'-main">'+ view.join('') + function(){ | ||
return conf.skip | ||
? '<span class="'+ conf.className +'-total"><label>\u5230\u7b2c</label><input type="number" min="1" onkeyup="this.value=this.value.replace(/\\D/, \'\');" class="'+ conf.className +'-skip"><label>\u9875</label>' | ||
+ '<button type="button" class="'+ conf.className +'-btn">\u786e\u5b9a</button></span>' | ||
: ''; | ||
}() +'</div>'; | ||
} | ||
//总页数大于连续分页数,且当前组最大页小于总页,输出尾页 | ||
if(conf.pages > conf.groups && dict.end < conf.pages && conf.last && conf.groups !== 0){ | ||
view.push('<span>\u2026</span><a href="'+ conf.url + conf.pages +'/'+ urls.search +'" class="'+ conf.className +'-last" title="\u5c3e\u9875">'+ conf.last +'</a>'); | ||
} | ||
//当前页不为尾页时,输出下一页 | ||
dict.flow = !conf.prev && conf.groups === 0; | ||
if(conf.curr !== conf.pages && conf.next || dict.flow){ | ||
view.push((function(){ | ||
return (dict.flow && conf.curr === conf.pages) | ||
? '<span class="'+ conf.className +'-nomore" title="\u5df2\u6ca1\u6709\u66f4\u591a">'+ conf.next +'</span>' | ||
: '<a href="'+ conf.url + (conf.curr + 1) +'/'+ urls.search +'" class="'+ conf.className +'-next">'+ conf.next +'</a>'; | ||
}())); | ||
} | ||
if(conf.pages <= 1) return ''; | ||
return '<div class="'+ conf.className +'-main">'+ view.join('') + function(){ | ||
return conf.skip | ||
? '<span class="'+ conf.className +'-total"><label>\u5230\u7b2c</label><input type="number" min="1" onkeyup="this.value=this.value.replace(/\\D/, \'\');" class="'+ conf.className +'-skip"><label>\u9875</label>' | ||
+ '<button type="button" class="'+ conf.className +'-btn">\u786e\u5b9a</button></span>' | ||
: ''; | ||
}() +'</div>'; | ||
}; | ||
@@ -105,5 +99,7 @@ | ||
Page.prototype.render = function(){ | ||
return this.view(); | ||
} | ||
return this.view(); | ||
}; | ||
module.exports = laypage; | ||
module.exports = function(options){ | ||
return new Page(options).render(); | ||
}; |
{ | ||
"name": "laypage", | ||
"description": "Node.js Paging Module 分页模块", | ||
"version": "1.1.1", | ||
"version": "1.1.2", | ||
"author": "贤心", | ||
@@ -20,3 +20,3 @@ "keywords": [ | ||
}, | ||
"homepage": "http://www.layui.com/", | ||
"homepage": "http://laypage.layui.com/", | ||
"scripts": { | ||
@@ -23,0 +23,0 @@ "test": "echo \"Error: no test specified\" && exit 1" |
@@ -7,3 +7,3 @@ | ||
## 安装(Installation) | ||
$ npm install laypage | ||
$ npm install laypage | ||
@@ -14,54 +14,54 @@ ## 使用(Usage) | ||
### 第一步:设定分页路由 | ||
var router = require('express').Router(); | ||
var laypage = require('laypage'); | ||
//请注意,list部分你可以任意定义,但是后面必须严格遵循 /page/:num 的格式 | ||
router.get('list/page/:num', function(req, res){ | ||
//第二步 | ||
}); | ||
var router = require('express').Router(); | ||
var laypage = require('laypage'); | ||
//请注意,list部分你可以任意定义,但是后面必须严格遵循 /page/:num 的格式 | ||
router.get('list/page/:num', function(req, res){ | ||
//第二步 | ||
}); | ||
### 第二步:作为渲染的参数 | ||
router.get('list/page/:num', function(req, res){ | ||
res.render('模版文件地址', { | ||
laypage: laypage({ | ||
curr: req.params.page || 1 | ||
,url: req.url //必传参数,获取当前页的url | ||
,pages: 18 //分页总数你需要通过sql查询得到 | ||
}) | ||
}) | ||
}); | ||
router.get('list/page/:num', function(req, res){ | ||
res.render('模版文件地址', { | ||
laypage: laypage({ | ||
curr: req.params.page || 1 | ||
,url: req.url //必传参数,获取当前页的url | ||
,pages: 18 //分页总数你需要通过sql查询得到 | ||
}) | ||
}) | ||
}); | ||
### 第三步:在模版中输出分页 | ||
<html> | ||
<body> | ||
<%= laypage %> <!-- ejs --> | ||
{{ d.laypage }} <!-- laytpl --> | ||
</body> | ||
</html> | ||
<html> | ||
<body> | ||
<%= laypage %> <!-- ejs --> | ||
{{ d.laypage }} <!-- laytpl --> | ||
</body> | ||
</html> | ||
仅需三步,您就轻松地完成了一个分页的输出,但是laypage并不提供样式,它只动态输出分页的基本结构。下面是我们给的样式例子: | ||
/* laypage分页 */ | ||
.laypage-main, | ||
.laypage-main *{display:inline-block; *display:inline; *zoom:1; vertical-align:top;} | ||
.laypage-main{margin:20px 0; border: 1px solid #009E94; border-right: none; border-bottom: none; font-size: 0;} | ||
.laypage-main *{padding: 0 15px; line-height:28px; border-right: 1px solid #009E94; border-bottom: 1px solid #009E94; font-size:14px;} | ||
.laypage-main .laypage-curr{background-color:#009E94; color:#fff;} | ||
/* laypage分页 */ | ||
.laypage-main, | ||
.laypage-main *{display:inline-block; *display:inline; *zoom:1; vertical-align:top;} | ||
.laypage-main{margin:20px 0; border: 1px solid #009E94; border-right: none; border-bottom: none; font-size: 0;} | ||
.laypage-main *{padding: 0 15px; line-height:28px; border-right: 1px solid #009E94; border-bottom: 1px solid #009E94; font-size:14px;} | ||
.laypage-main .laypage-curr{background-color:#009E94; color:#fff;} | ||
## 文档(Document) | ||
好了,是时候介绍基础文档了,laypage的调用提供了以下参数(options)的选择 | ||
laypage({ | ||
curr: 1 //当前页,通过req.params.page即可得到(必选) | ||
,pages: 18 //分页总数,可通过sql查询得到(必选) | ||
,url: req.url //当前页的url(必选) | ||
,groups: 3 //连续显示的分页数,默认3 (可选) | ||
,first: 1 //首页显示的文本,默认1(可选) | ||
,last: pages //尾页显示的文本,默认是最大页数(可选) | ||
,prev: '上一页' //上一页自定义文本(可选) | ||
,next: '下一页' //下一页自定义文本(可选) | ||
,skip: false, //是否输出跳页框,默认false(可选) | ||
,className: 'laypage' //分页样式的前缀,默认为laypage(可选) | ||
}) | ||
laypage({ | ||
curr: 1 //当前页,通过req.params.page即可得到(必选) | ||
,pages: 18 //分页总数,可通过sql查询得到(必选) | ||
,url: req.url //当前页的url(必选) | ||
,groups: 3 //连续显示的分页数,默认3 (可选) | ||
,first: 1 //首页显示的文本,默认1(可选) | ||
,last: pages //尾页显示的文本,默认是最大页数(可选) | ||
,prev: '上一页' //上一页自定义文本(可选) | ||
,next: '下一页' //下一页自定义文本(可选) | ||
,skip: false, //是否输出跳页框,默认false(可选) | ||
,className: 'laypage' //分页样式的前缀,默认为laypage(可选) | ||
}) | ||
@@ -68,0 +68,0 @@ ## 协议(License) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
7788
85