基於nodejs的簡單分頁的實現
阿新 • • 發佈:2019-01-25
一、專案描述
這個demo是基於昨天我寫的增刪改查的例子的。在昨天的例子的基礎上,我增加了基本的分頁功能。由於本人對於nodejs的理解不夠深入所以在分頁實現上,有很多不足之處。在儲存分頁資訊上我使用的是request層儲存,也就是用get傳值的古老辦法,主要是由於對於nodejs的session瞭解還不夠深入。
二、詳細程式碼
由於是基於昨天的專案所以很多地方就請參看上一篇的文章。
1、Config.js
module.exports={ host:'localhost', user:'root', password:'root', database:'db_test_', ports:3306 };
2、DBUtil
var config=require('./config.js');//引入配置檔案 var mysql=require('mysql');//引入mysql驅動 var pool=mysql.createPool(config);//建立資料庫連線池 //dql函式(用於查詢的函式) exports.executeQuery=function(sql, data, callback){ pool.getConnection(function(err,conn){ if(err){ callback(err,null,null); }else{ conn.query(sql,data,function(qerr, vals, fields){ conn.release(); callback(qerr,vals,fields); }); } }); }; //dml函式(用於增加、修改、刪除的函式) exports.executeUpdate = function(sql, data, callback) { pool.getConnection(function(err, conn) { if (err) { callback(err, null, null); } else { conn.query(sql, data, function(qerr, result) { conn.release(); callback(qerr, result); }); } }); };
3、Pagedao.js
var DaoBase = require('./DBUtil');//引入dbutil var user_DB = require('../VO/User');//引入模型模組 var pageVO=require('../VO/PageInfo');//引入分頁模型 var pageSize=5; var userDB = new user_DB();//例項化模型模組 var pageInfo = new pageVO();//例項化模型模組 //總頁數 var numPage=exports.pageNum=function(VO,callback){ var sql='SELECT CEILING(COUNT(id)/'+VO.pageSize+') AS sum FROM tb_test_user_ '; if(VO.static){ sql=sql+'where name like '+VO.queryInfo; } var data=[]; DaoBase.executeQuery(sql,data,callback); }; //首頁 exports.firstPage=function(VO,callback){ var sql='select * from tb_test_user_ '; if(VO.static){ sql=sql+'where name like '+VO.queryInfo; } sql=sql+' limit ?,?'; console.log(sql); var data=[]; data.push(0); console.log(VO.pageSize); data.push(VO.pageSize); DaoBase.executeQuery(sql,data,callback); }; //尾頁 exports.endPage=function(VO,callback){ numPage(VO,function(qerr, vals,fields){ if(!(qerr==null)){ console.log('error'); } var sql='select * from tb_test_user_ '; if(VO.static){ sql=sql+'where name like '+VO.queryInfo; } sql=sql+' limit ?,?'; var data=[]; var pageStartRow= (vals[0].sum-1)*VO.pageSize; data.push(pageStartRow); data.push(VO.pageSize); DaoBase.executeQuery(sql,data,callback); }); }; //當前頁 exports.page=function(VO,callback){ var currentPage=VO.currentPage; var sql='select * from tb_test_user_ '; if(VO.static){ sql=sql+'where name like '+VO.queryInfo; } sql=sql+' limit ?,?'; var data=[]; var pageStartRow= (currentPage-1)*VO.pageSize; data.push(pageStartRow); data.push(VO.pageSize); DaoBase.executeQuery(sql,data,callback); };
4、pageinfo.js
module.exports=PageInfo; function PageInfo(){ this.pageNum='pageNum', //總頁數 this.currentPage='currentPage', //當前頁數 this.pageSize='pageSize', //每頁顯示的條數 this.result='result', //查詢結果 this.totalRows = 'totalRows', // 總資料數 this.pageStartRow ='pageStartRow', // 每頁的起始數 this.pageEndRow = 'pageEndRow', // 每頁顯示資料的終止數 this.queryInfo='queryInfo', //查詢條件 this.static='static' //狀態資訊 }
5、Index.js
var express = require('express'); var express = require('express');//引入express包 var userDao=require('../dao/UserDao.js');//引入userdao檔案 var user_DB = require('../VO/User');//引入user實體 var pageDB=require('../dao/PageDao.js');//引入pagedao var pageInfo=require('../VO/PageInfo'); //引入page實體 var page=new pageInfo(); //首頁 var firtPage=exports.firstPage=function(req,res){ page.pageSize=4; page.static=false; page.currentPage=1; var total=req.query.total; pageDB.firstPage(page,function(qerr, vals,fields){ if(!(qerr==null)){ console.log('error'); } var result=vals; res.render('success',{ title:'這是第一頁', result:result, page:1, total:total }); }); }; //尾頁 var lastPage=exports.endPage=function(req,res){ page.pageSize=4; page.static=false; var total=req.query.total; pageDB.endPage(page,function(qerr, vals,fields){ if(!(qerr==null)){ console.log('error'); } var result=vals; pageDB.pageNum(page,function(qerr, vals,fields){ console.log(vals[0].sum); res.render('success',{ title:'這是最後一頁', result:result, page:vals[0].sum, total:vals[0].sum }); }); }); }; //上一頁 exports.upPage=function(req,res){ page.pageSize=4; page.static=false; var total=req.query.total; var oldPage=req.query.page; if((oldPage-1)<=1){ firtPage(req,res); }else{ page.currentPage=oldPage-1; pageDB.page(page,function(qerr, vals,fields){ if(!(qerr==null)){ console.log('error'); } var result=vals; res.render('success',{ title:'這是第一頁', result:result, page:oldPage-1, total:total }); }); } }; //下一頁 exports.downPage=function(req,res){ page.pageSize=4; page.static=false; var oldPage=req.query.page; var total=req.query.total; var pageNum=Number(oldPage)+1; console.log("???????"+pageNum+"!!!!!"+oldPage); if(pageNum>=total){ lastPage(req,res); }else{ page.currentPage=pageNum; pageDB.page(page,function(qerr, vals,fields){ if(!(qerr==null)){ console.log('error'); } var result=vals; res.render('success',{ title:'這是第一頁', result:result, page:pageNum, total:total }); }); } };
//登入驗證
exports.doLogin=function(req,res){ var username=req.body.username; var password=req.body.password; var user=new user_DB(); user.name=username; user.password=password; userDao.UserLogin(user,function(qerr, vals,fields){ if((!(qerr==null))||(vals.length<1)){ res.render('error',{ message:'登入失敗' }); }else{ page.pageSize=4; page.static=false; page.currentPage=1; pageDB.firstPage(page,function(qerr, vals,fields){ if(!(qerr==null)){ console.log('error'); } var result=vals; pageDB.pageNum(page,function(qerr, vals,fields){ if(!(qerr==null)){ console.log('error'); } console.log(vals); console.log( vals[0].sum);//求總頁數 res.render('success',{ title:'這是第一頁', result:result, page:1, total:vals[0].sum }); }); }); } }); };
6、App.js
app.use('/first',routes.firstPage); app.use('/end',routes.endPage); app.use('/up',routes.upPage); app.use('/down',routes.downPage);
7、Success.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title><%= title %></title> <link rel='stylesheet' href='/stylesheets/style.css' /> </head> <body> <h1><%= title %></h1> <table> <% result.forEach(function(name){ %> <tr> <td><%= name.name %></td> <td><%= name.password %></td> <td></td> <td><a href="/delete?id=<%= name.id %>">刪除</a></td> <td><a href="/update?id=<%= name.id %>">修改</a></td> </tr> <% }) %> </table> <p><a href="/Insert">新增</a></p> <p><a href="/first?page=<%= page %>&total=<%= total %>">首頁</a></p> <p><a href="/up?page=<%= page %>&total=<%= total %>">上一頁</a></p> <p><a href="/down?page=<%= page %>&total=<%= total %>">下一頁</a></p> <p><a href="/end?page=<%= page %>&total=<%= total %>">尾頁</a></p> <p>當前頁數:<%= page %></p> <p>總頁數:<%= total %></p> </body> </html>
8、Pagetest.js
var userDao=require('../dao/PageDao.js'); var user_DB = require('../VO/User'); var pageVO=require('../VO/PageInfo'); var pageInfo = new pageVO(); pageInfo.currentPage=2; pageInfo.static=true; pageInfo.queryInfo='1'; /*userDao.page(pageInfo,function(qerr, vals,fields){ if(!(qerr==null)){ console.log('error'); } console.log(vals); });*/ /*userDao.endPage(pageInfo,function(qerr, vals,fields){ if(!(qerr==null)){ console.log('error'); } console.log(vals); });*/ /*userDao.firstPage(pageInfo,function(qerr, vals,fields){ if(!(qerr==null)){ console.log('error'); } console.log(vals); });*/ /*userDao.pageNum(pageInfo,function(qerr, vals,fields){ if(!(qerr==null)){ console.log('error'); } console.log(vals); console.log( vals[0].sum);//求總頁數 });*/