如何在nodejs中应用分页的概念。我发布了我的整个nodejs 练习模块,但我坚持分页的概念。我需要 每页显示4个条目。 的前端
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form action="/deletechecks" method="post">
<table style="margin-top: 20px" table border="1" text-
align:ccellspacing="20px" cellpadding="15px" width="100%">
<% for(var i = 0 ; i < data.length ; i++) { %>
<tr></td>
<td> <%= data[i].catname %> </td>
<td> <%= data[i].status %> </td>
<td> <%= data[i].id %> </td>
<td> <a href="/delete/<%= data[i].id %>"> delete</a></td>
<td> <a href="/view/<%= data[i].id %>"> view</a></td>
<td> <a href="/update/<%= data[i].id %>"> update</a></td>
<td> <input type="checkbox" name="check" value="<%= data[i].id
%>">delete <%=data[i].id %> </td>
</tr>
<% } %>
</table>
<input type="submit" name="submit" value="Delete Selected">
</div>
</form>
</body>
</html>
后端
var express= require('express');
var app = express();
var url = require('url')
var bodyparser= require('body-parser');
var mysql = require('mysql');
var session= require('express-session');
app.use(session({secret:'hello'}));
app.use(bodyparser.urlencoded({extended:true}));
app.set ("view engine" , 'ejs');
var con = mysql.createConnection({
host: "localhost",
user:'root',
password: "",
database:'blog'
});
con.connect(function(err) {
if(err) throw err;
console.log("connected!");
});
app.get('/',function(req,res){
res.render('blog1');
});
app.post('/action',function(req,res){
var title=req.body.cat;
var status =req.body.sel ;
var q = "insert into category set catname = '"+title+"',
status='"+status+"'";
con.query(q,function(err,result){
if(err)
throw err;
else
res.redirect('/viewdata');
});
});
app.get('/viewdata', function(req,res) {
var w= "select * from category";
con.query(w, function(err,result){
res.render('showdata',{data:result});
});
});
app.get('/delete/:a',function(req,res){
var z= req.params.a;
var d= "delete from category where id='"+z+"'";
con.query(d,function(err,result){
if(err) throw err;
res.redirect('/viewdata');
});
});
app.get('/view/:a',function(req,res){
var z= req.params.a;// url se dynamic value nikalne ke liye
var d= "select * from category where id='"+z+"' ";
con.query(d,function(err,result) {
if(err) throw err;
res.render('showbyid',{data:result});
});
});
app.get('/update/:id', function(req,res){
var d= req.params.id
var z= "select * from category where id='"+d+"'";
con.query(z,function(err,result) {
if(err) throw err;
res.render('blog1', {data:result});
});
});
app.post('/new_action/:id',function(req,res){
var x = req.body.cat;
var y = req.body.sel;
var z = req.params.id;
var updated ="update category set catname ='"+x+"', status='"+y+"' where
id='"+z+"' ";
con.query(updated,function(err,result){
if(err)
throw err;
else
res.redirect('/viewdata');
});
});
app.get('/viewdata',function(req,res){
var q= "select * from category";
con.query(q,function(err,result){
res.render("viewdata",{data:result});
});
});
app.post('/deletechecks',function(req,res){
var ch = req.body.check;
if(typeof ch == "string")
{
var h= "delete from category where id='"+ch+"'";
console.log(h);
con.query(h,function(err,result)
{
console.log('deleted');
});
}
else
{
for( var i=0;i<ch.length;i++)
{
var h= "delete from category where id='"+ch[i]+"'";
console.log(h);
con.query(h,function(err,result)
{
console.log('deleted');
});
}
}
res.redirect('/viewdata');
});
app.get('/login',function(req,res){
if(req.session.name)
{
res.redirect("/dashboard");
}
else {
res.render('login');
}
});
app.post('/new',function(req,res){
var email = req.body.emailfield;
var password = req.body.passwordfield;
var updated =" select count (email) as val from admin where email =
'"+email+"' and password='"+password+"' and status='active'";
con.query(updated,function(err,result){
if(err)
throw err;
if(result[0].val)//
{
req.session.name=email;
res.redirect("/dashboard");
}
else
{
res.redirect("/login");
}
});
});
app.get("/dashboard",function(req,res)
{
if(!req.session.name)
{
res.redirect("/login");
}
res.render("dashboard")
});
app.get("/logout" , function(req,res){
})
app.listen(8080);
**还包括插入数据库的代码以及删除和其他操作。代码在其余部分工作完美 查询只在这里应用分页的概念。 **
答案 0 :(得分:1)
这与节点js无关。这是一个应用程序设计问题。分页以两种方式实现
服务器端:
app.get('/viewdata/:page', function (req, res) {
var page = req.params.page
var w = //some SP to get nth page data;
con.query(w, function (err, result) {
res.render('showdata', { data: result });
});
});