一,建立两个模板:db.js (获取数据库的数据) message.js(将得到的数据进行分页,显示)
db.js
/** * 创建数据库连接,并获取数据 */ var mysql = require(‘mysql‘); //获取用户名,连接地址,密码,数据库名 function Db(host,user,pwd,port,database){ this.host = host; this.user = user; this.pwd = pwd; this.port = port; this.database = database; } //获取数据 Db.prototype.excuteQuery = function(sql,connection,fun){ connection.query(sql, function (error, rows) { var result=""; if (error) { result = { "status": "500", "message": "服务器错误" } }else{ result = { "status": "200", "message": "success", "data":rows } } fun(result); }); } //建立连接 Db.prototype.getConnection = function(){ var connection = mysql.createConnection({ host: this.host, user: this.user, password:this.pwd, port: this.port, database: this.database }); connection.connect(); return connection; } module.exports=Db;
message.js
/*数据分页*/ var Db=require(‘./db.js‘); //创建一个新的数据库方法对象 var db=new Db(‘localhost‘,‘dyb‘,‘1743721550‘,‘3306‘,‘test‘); var obj=null; function Message(express) { //创建路由 this.router = express.Router(); this.execute(); } //此函数检查该表中的数据条数 Message.prototype.count = function(sql,fun){ var cSql = "select count(*) from ("+sql+")"; //通过对象调用函数创建连接 var con=db.getConnection(); //调用函数接收数据 db.excuteQuery(cSql,con,function(data){ fun(parseInt(data)); }) } //此函数通过linmit方法获取表中需要的数据 Message.prototype.limtModel = function(sql,start,pageSize,fun){ //返回从start行开始,到pageSize中的数据 var limitSql="select * from ("+sql+")aa limit "+start+","+pageSize; console.log(limitSql); var con=db.getConnection(); db.excuteQuery(limitSql,con,function(data){ fun(data.data) }) } Message.prototype.execute = function(){ var obj=this; //创建路由路径 this.router.get("/message?",function(req,res){ //初始从第几页开始 var currentPage = 2; //每页显示的数据条数 var pageSize = 5; var sql="select * from t_message where 1=1 order by s_date desc"; //返回url中属性使currPafe的参数 var cP=req.query.currPage; if(cP!=null&&(typeof cP)!=undefined){ currentPage=cP; } obj.count(sql,function(count){ //最大页数 var maxPage=(count+pageSize-1)/pageSize; //如果此页大于最大页数,则吧最大页数赋给此页 if(currentPage>maxPage){ currentPage = maxPage; } //此页小于1则将此页置为一,从第一页开始显示 if(currentPage<1){ currentPage = 1; } /* * 第一个参数:sql:需要分页的数据 * 第二个参数:通过当前的页数,和每页需要显示的数据条数,得出这一页数据开始是哪一条数据 * 第三个参数:获得确定这页数据的最后一条数据是那一条数据 * */ var addr=req.protocol+"://"+ req.hostname+":3001/"; obj.limtModel(sql,(currentPage-1)*pageSize,pageSize,function(data){ res.render("qh/message",{args:data,cp:currentPage,addr:addr,last:maxPage}); }); }) }) } module.exports=Message;
二,创建路由
app3.js
var express = require(‘express‘); var fs= require(‘fs‘); var url = require(‘url‘); var app = express(); app.set(‘view engine‘, ‘ejs‘); app.set(‘views‘, __dirname + ‘/views‘); app.use(express.static( __dirname+"/public")); //指向模板 var Message=require(‘./action/message.js‘); app.use("/",new Message(express).router); app.listen(3001,function afterListen(){ console.log("express running ...."); });
三,在页面接收数据
<div class="mailbox mt20"> <% args.forEach(function(row) { %> <div class="online-content clearfix"> <div class="icon fl"> <img src="../images/online.png" alt=""> </div> <div class="question fr"> <p>留言主题:<%=row.title%> </p> <p class="name"><span>网友:<%=row.name%></span><span><%=row.s_date%></span><span><a target="_blank" href="#">我要留言</a></span></p> <div class="message-i "> <p><strong>留言详情:</strong><%=row.s_des%></p> </div> <div class="answer"> <p><strong>执法监察总队回复:</strong><%=row.l_des%></p> </div> </div> </div> <% }) %> <div class="page"> <% cp=parseInt(cp); %> <ul><li class="num"><a href=<%=addr%>message?cp=1>首页</a></li> <li class="num"><a href=<%=addr%>message?cp=<%=cp-1%>>上一页</a></li> <li class="num page-active">1</li> <li class="num" >2</li> <li class="num" >3</li> <li class="num" >4</li> <li class="num">5</li> <li class="page-speciall">...</li><li class="num">34</li> <li class="num"><a href=<%=addr%>message?cp=<%=cp+1%>>下一页</a></li> <li class="num"><a href=<%=addr%>message?cp=<%=last%>>尾页</a></li></ul> </div> </div>
原文地址:https://www.cnblogs.com/dybe/p/8143628.html
时间: 2024-10-05 20:38:32