├── .npmignore ├── .eslintrc ├── test └── index.js ├── .gitignore ├── LICENSE ├── README_v2.md ├── package.json ├── README.md └── index.js /.npmignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | coverage/ -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "think" 3 | } -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | var path = require('path'); 3 | var fs = require('fs'); 4 | var muk = require('muk'); 5 | 6 | var pagenation = require('../index.js'); 7 | 8 | describe ('pagenation', function() { 9 | it ('totalPages = 1', function() { 10 | var html = pagenation({totalPages: 1}); 11 | assert.equal(html, ''); 12 | }); 13 | // it('totalPages > 1', function(){ 14 | // var html = pagenation({totalPages: 2}, { 15 | // query: {} 16 | // }); 17 | // console.log(html) 18 | // }) 19 | }); 20 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | 5 | # Runtime data 6 | pids 7 | *.pid 8 | *.seed 9 | 10 | # Directory for instrumented libs generated by jscoverage/JSCover 11 | lib-cov 12 | 13 | # Coverage directory used by tools like istanbul 14 | coverage 15 | 16 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 17 | .grunt 18 | 19 | # node-waf configuration 20 | .lock-wscript 21 | 22 | # Compiled binary addons (http://nodejs.org/api/addons.html) 23 | build/Release 24 | 25 | # Dependency directory 26 | # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git 27 | node_modules 28 | 29 | lib/ -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 ThinkJS 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README_v2.md: -------------------------------------------------------------------------------- 1 | # think-pagination 2 | 3 | pagination for ThinkJS 2 4 | 5 | ## install 6 | 7 | ```sh 8 | npm install think-pagination@1.6.0 9 | ``` 10 | 11 | ## how to use 12 | 13 | ### controller 14 | 15 | ```js 16 | import pagination from 'think-pagination'; 17 | export default class think.controller.base { 18 | async indexAction(){ 19 | let data = await this.model('user').page(this.get('page')).countSelect(); 20 | let html = pagination(data, this.http, {}); 21 | this.assign('pagination', html); 22 | } 23 | } 24 | ``` 25 | 26 | ### view 27 | 28 | #### ejs 29 | 30 | ```html 31 | {%-pagination%} 32 | ``` 33 | 34 | #### nunjucks 35 | 36 | ```html 37 | {{pagination | safe}} 38 | ``` 39 | 40 | ## api 41 | 42 | ### pagination(pagerData, http, options) 43 | 44 | * `pagerData` get from by model.countSelect 45 | * `http` http object 46 | * `options` options 47 | 48 | `options`: 49 | 50 | ```js 51 | { 52 | desc: false, //show description 53 | pageNum: 2, 54 | url: '', //page url, when not set, it will auto generated 55 | class: '', //pagenation extra class 56 | text: { 57 | next: 'Next', 58 | prev: 'Prev', 59 | total: 'count: ${count} , pages: ${pages}' 60 | } 61 | } 62 | ``` 63 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "think-pagination", 3 | "description": "pagination for ThinkJS 3.x", 4 | "version": "2.0.1", 5 | "author": { 6 | "name": "welefen", 7 | "email": "welefen@gmail.com" 8 | }, 9 | "scripts": { 10 | "test": "istanbul cover ./node_modules/mocha/bin/_mocha -- -t 2000 --recursive -R spec test/", 11 | "prepublish": "npm run lint", 12 | "lint": "eslint index.js" 13 | }, 14 | "contributors": [ 15 | { 16 | "name": "welefen", 17 | "email": "welefen@gmail.com" 18 | } 19 | ], 20 | "main": "index.js", 21 | "dependencies": { 22 | "think-helper": "^1.1.3" 23 | }, 24 | "devDependencies": { 25 | "mocha": "1.20.1", 26 | "muk": "0.3.1", 27 | "istanbul": "0.4.0", 28 | "eslint": "^4.3.0", 29 | "eslint-config-think": "^1.0.1" 30 | }, 31 | "keywords": [ 32 | "thinkjs", 33 | "pagination" 34 | ], 35 | "repository": { 36 | "type": "git", 37 | "url": "https://github.com/thinkjs-team/think-pagination" 38 | }, 39 | "engines": { 40 | "node": ">=6.0.0" 41 | }, 42 | "license": "MIT", 43 | "readmeFilename": "README.md", 44 | "bugs": { 45 | "url": "https://github.com/thinkjs-team/think-pagenation/issues" 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # think-pagination 2 | 3 | pagination for ThinkJS 3, if you want to use in ThinkJS 2.x, please view [README_v2](./README_v2.md) 4 | 5 | ## install 6 | 7 | ```sh 8 | npm install think-pagination 9 | ``` 10 | 11 | ## how to use 12 | 13 | ### controller 14 | 15 | ```js 16 | const pagination = require('think-pagination'); 17 | 18 | module.exports = class extends think.Controller { 19 | async indexAction() { 20 | const data = await this.model('user').page(this.get('page')).countSelect(); 21 | const html = pagination(data, this.ctx, {}); 22 | this.assign('pagination', html); 23 | } 24 | } 25 | ``` 26 | 27 | ### view 28 | 29 | #### ejs 30 | 31 | ```html 32 | {%-pagination%} 33 | ``` 34 | 35 | #### nunjucks 36 | 37 | ```html 38 | {{pagination | safe}} 39 | ``` 40 | 41 | ## API 42 | 43 | ### pagination(pagerData, ctx, options) 44 | 45 | * `pagerData` get from by model.countSelect 46 | * `ctx` ctx object 47 | * `options` options 48 | 49 | `options`: 50 | 51 | ```js 52 | { 53 | desc: false, //show description 54 | pageNum: 2, 55 | url: '', //page url, when not set, it will auto generated 56 | class: '', //pagenation extra class 57 | text: { 58 | next: 'Next', 59 | prev: 'Prev', 60 | total: 'count: __COUNT__ , pages: __PAGE__' 61 | } 62 | } 63 | ``` 64 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const helper = require('think-helper'); 2 | 3 | /** 4 | * get page url 5 | * @param {Object} options [] 6 | * @param {Object} http [] 7 | * @return {String} [] 8 | */ 9 | const getPageUrl = (options, ctx) => { 10 | let pageUrl = options.url; 11 | if (!pageUrl) { 12 | let prefix = (options.prefix || '') + '?'; 13 | const querys = []; 14 | for (const name in ctx.query) { 15 | if (name === 'page') { 16 | continue; 17 | } 18 | querys.push(helper.escapeHtml(name) + '=' + helper.escapeHtml(ctx.query[name])); 19 | } 20 | prefix += querys.join('&'); 21 | if (querys.length) { 22 | prefix += '&'; 23 | } 24 | pageUrl = prefix + 'page=__PAGE__'; 25 | } 26 | return pageUrl; 27 | }; 28 | 29 | /** 30 | * get page index 31 | * @param {Object} pagerData [] 32 | * @param {Object} options [] 33 | * @return {Array} [] 34 | */ 35 | const getPageIndex = (pagerData, options) => { 36 | const num = options.pageNum || 2; 37 | const page = pagerData.currentPage | 0 || 1; 38 | const totalPages = pagerData.totalPages; 39 | const pageIndex = []; 40 | let start = page - num; 41 | let stop = page + num; 42 | 43 | if (start <= 1) { 44 | start = 1; 45 | stop = start + num * 2 + 1; 46 | } 47 | 48 | if (stop >= totalPages) { 49 | stop = totalPages; 50 | start = totalPages - num * 2 - 1; 51 | } 52 | 53 | for (let i = start; i <= stop; i++) { 54 | if (i >= 1 && i <= totalPages) { 55 | pageIndex.push(i); 56 | } 57 | } 58 | return pageIndex; 59 | }; 60 | 61 | /** 62 | * thinkjs pagenation 63 | * @param {Object} pagerData [pagerData by countSelect] 64 | * @param {Object} ctx [] 65 | * @param {Object} options [] 66 | * @return {String} [] 67 | */ 68 | module.exports = (pagerData, ctx, options) => { 69 | if (pagerData.totalPages <= 1) return ''; 70 | 71 | options = Object.assign({ 72 | desc: false, // show desc 73 | pageNum: 2, 74 | url: '', 75 | class: '', 76 | text: { 77 | next: 'Next', 78 | prev: 'Prev', 79 | total: 'count: __COUNT__ , pages: __PAGE__' 80 | } 81 | }, options); 82 | 83 | const pageUrl = getPageUrl(options, ctx); 84 | const currentPage = pagerData.currentPage | 0 || 1; 85 | 86 | let html = `