// 后端>app.js
//package.json文件里配置dev
```
"scripts": {
"dev": "nodemon app.js"
},
```
```
//app.js
const koa = require('koa')
const router = require('koa-router')()
const cors = require('koa-cors')
const bodyparser = require('koa-bodyparser')
const { Sequelize, DataTypes, Op } = require('sequelize')
const sequelize = new Sequelize('BlogsDb', 'sa', '123456', {
host: 'localhost',
dialect: 'mssql'
})
let blogs = sequelize.define('blogs', {
'name': {
type: DataTypes.STRING,
allowNull: false
},
'sex': {
type: DataTypes.STRING,
allowNull: false
},
'age': {
type: DataTypes.INTEGER,
allowNull: false
},
'classID': {
type: DataTypes.INTEGER,
allowNull: false
}
})
let classes = sequelize.define('class', {
'claName': {
type: DataTypes.STRING,
allowNull: false
}
})
function into() {
sequelize.sync({ force: true }).then(() => {
blogs.bulkCreate([
{
name: '蔡小强',
sex: '雄',
age: 100000,
classID: 1
},
{
name: '黄小✌',
sex: '雄',
age: 100000,
classID: 3
},
{
name: '万小祥',
sex: '雄',
age: 100000,
classID: 1
},
{
name: '唐小健',
sex: '雄',
age: 100000,
classID: 2
},
]),
classes.bulkCreate([
{
claName: '吴彦组'
},
{
claName: '如来佛组'
},
{
claName: '重案六组'
},
])
})
}
into()
const app = new koa()
async function link() {
let data = await blogs.findAll();
let classData = await classes.findAll();
data.forEach((item) => {
let classN = classData.find(res => {
return res.id == item.classID
})
if (classN) {
item.dataValues.classN = classN.claName
}
})
return data
}
router
.get('/blogs', async (ctx) => {
let data = await link()
ctx.body = data
})
.get('/data', async (ctx) => {
let keyword = ctx.request.query.keyword;
if (keyword !== '') {
if (isNaN(keyword) !== false) {
ctx.body = await blogs.findAll({
where: {
[Op.or]: [
{ name: keyword },
{ sex: keyword },
]
}
})
} else {
ctx.body = await blogs.findAll({
where: {
[Op.or]: [
{ id: keyword },
{ age: keyword },
]
}
})
}
} else {
let data = await link()
ctx.body = data
}
})
.post('/add', async (ctx) => {
let data = ctx.request.body;
blogs.create(data)
let add = await link()
ctx.body = add
})
.get('/edit', async (ctx) => {
let id = ctx.request.query.id
let data = await blogs.findAll()
let editData = data.filter((item) => {
return item.id == id
})
ctx.body = editData
})
.post('/edit', async (ctx) => {
let data = ctx.request.body
let id = ctx.request.query.id
await blogs.update(data, {
where: {
id: id
},
order: ['id']
})
ctx.body = '修改成功'
})
.get('/del', async (ctx) => {
let id = ctx.request.query.id
await blogs.destroy({
where: {
id: id
}
})
let data = await link()
ctx.body = data
})
app.use(cors())
app.use(bodyparser())
app.use(router.routes());
let prot = 8000;
app.listen(prot)
//前端>js>添加jQuery文件
//前端>index.html
id | 姓名 | 性别 | 年纪 | 组号 | 组名 | *** 作 |
---|
$(function () {
$.get('http://localhost:5000/blogs', (data) => {
print(data)
}
)
})
function print(data) {
data.forEach(item => {
let html = `
`
$('#data').append(html)
})
}
function query() {
let keyword = $('[name=keyword]').val();
$.get('http://localhost:5000/data?keyword=' + keyword, (data) => {
let tr = $('tr').length;
if (data.length < 1) {
alert('表中无数据')
} else {
for (let i = 0; i < tr; i++) {
$('#html').remove();
}
print(data);
}
})
}
function add() {
window.location.href = "./AddOrEdit.html"
}
function Edit(id) {
window.location.href = `./AddOrEdit.html?id=${id}`
}
function Delete(id) {
if (confirm("确认删掉该数据吗?")) {
$.get('http://localhost:5000/del?id='+id,async (data)=>{
let len = $('tr').length
for (let i = 0; i < len; i++) {
$('#html').remove()
}
print(data)
})
}
}
```
//前端>AddOrEdit.html
```
id: |
|
姓名: |
|
年龄: |
|
性别: |
|
组号: |
|
|
|
$(function () {
let id = window.location.search.replace(/[^\d]/g, "")
if (id) {
$.get('http://localhost:5000/edit?id=' + id, (data) => {
$('#id').val(data[0].id)
$('#name').val(data[0].name)
$('#sex').val(data[0].sex)
$('#age').val(data[0].age)
$('#classID').val(data[0].classID)
})
}
})
function cel() {
window.location.href = "./index.html"
}
function save() {
let id = $('#id').val()
let name = $('#name').val()
let sex = $('#sex').val()
let age = $('#age').val()
let classID = $('#classID').val()
let obj = {
name,
sex,
age,
classID
}
console.log(name);
if (id) {
$.post('http://localhost:5000/edit?id=' + id, obj, async (data) => {
window.location.href = "./index.html"
})
} else {
if (name == '' || age == '' || sex == '' || classID == '') {
alert('无法添加空值')
} else {
$.post('http://localhost:5000/add', obj, async (data) => {
window.location.href = "./index.html"
})
}
}
}
```
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)