提问,刚入行不太清楚如何在nodejs查到的新的数据里面添加查看上一条信息和下一条信息,并在前端的vue里面通过按钮实现这个功能
下面是node.js的代码
const express = require('express');
const router = express.Router();
const { Article } = require('../../models');
const { Op } = require("sequelize");
const { NotFoundError } = require('../../utils/errors');
const { success, failure } = require('../../utils/responses');
/**
* 查询文章列表
* GET /admin/articles
*/
router.get('/', async function (req, res) {
try {
const query = req.query;
const currentPage = Math.abs(Number(query.currentPage)) || 1;
const pageSize = Math.abs(Number(query.pageSize)) || 10;
const offset = (currentPage - 1) * pageSize;
const condition = {
order: [['mlid', 'DESC']],
limit: pageSize,
offset: offset
};
if (query.jingming) {
condition.where = {
jingming: {
[Op.like]: `%${ query.jingming }%`
}
};
}
const { count, rows } = await Article.findAndCountAll(condition);
success(res, '查询文章列表成功。', {
articles: rows,
pagination: {
total: count,
currentPage,
pageSize
}
});
} catch (error) {
failure(res, error);
}
});
/**
* 查询文章详情
* GET /admin/articles/:mlid
*/
router.get('/:mlid', async function (req, res) {
try {
const article = await getArticle(req);
success(res, '查询文章mlid成功。', { article });
} catch (error) {
failure(res, error);
}
});
/**
* 公共方法:查询当前文章
*/
async function getArticle(req) {
const { mlid } = req.params;
const article = await Article.findByPk(mlid);
if (!article) {
throw new NotFoundError(`ID: ${ id }的文章未找到。`);
}
return article;
}
module.exports = router
下面是前端vue代码
<template>
<div>
<el-descriptions title="查看信息" :column="1" border>
<template #extra>
<el-button type="primary" @click="goBack">返回</el-button>
</template>
<el-descriptions-item label="名称">{{ article.mingcheng }}</el-descriptions-item>
<el-descriptions-item label="作者">{{ article.zuozhe }}</el-descriptions-item>
<el-descriptions-item label="网址">{{ article.url }}</el-descriptions-item>
<el-descriptions-item label="词典">{{ article.cd }}</el-descriptions-item>
<el-descriptions-item label="内容">
<div v-html="article.nr"/>
</el-descriptions-item>
</el-descriptions>
</div>
</template>
<script setup>
import { fetchArticle } from '@/api/articles'
import { useRoute, useRouter } from 'vue-router'
import { ref } from 'vue'
const route = useRoute()
const router = useRouter()
const article = ref({})
/**
* 初始化
* @returns {Promise<void>}
*/
const init = async () => {
const res = await fetchArticle(route.params.id)
article.value = res.data.article
}
init()
/**
* 返回
*/
const goBack = () => {
router.push({ path: "/" });
}
</script>
<style>
.el-descriptions__label {
width: 150px;
}
</style>
基础的sql语句如:
根据名字搜是:
select * from article where name=%hello%
点击其中,一个是:
select * from article where id = 50
查询下一个是:
select * from article where name=%hello% and id > 50 order by id asc limit 1
查询上一个是:
select * from article where name=%hello% and id < 50 order by id asc limit 1
order by要根据需求调整。不一定是asc