前言:
這個章節內容是比較多的,分為前端部分和后端部分。
目錄:
實現效果:增刪改查
一、后端部分:
(1)數據庫:
新建一張表user,設置幾個字段,效果如下:
(2)egg邏輯部分:
提供的接口:http://localhost:7001/setUserList
(3)egg具體實現步驟:
1、router.js中添加:
2、新建:app / controller / new / user.js
(1)查,模糊查詢
(2)增
(3)改
(4)刪
user.js 源碼:
3、引入mysql:點我
4、跨域問題解決:點我
二、前端部分:
新建vue文件:testApi.vue
1、初始化查詢列表數據:頁面展示列表,列表數據mouted獲取
2、頁面點擊新增:展示彈框,并將彈框內容去除掉,點擊新增,將彈框內容發送給后端
3、頁面點擊列表里面具體數據的編輯:彈框,并回填數據,修改將當前數據的id和表格數據傳給后端
4、刪除按鈕,點擊出現二次確認彈框,點擊確認將當前數據的id給后端就行
testApi.vue 源碼:
實現效果:增刪改查
一、后端部分:
(1)數據庫:
新建一張表user,設置幾個字段,效果如下:
(2)egg邏輯部分:
提供的接口:http://localhost:7001/setUserList
-
get 請求,獲取數據,支持模糊查詢
-
post 請求,新增數據
-
put 請求,給后端當前數據id,修改內容
-
delete 請求,根據數據id刪除當前條數據
(3)egg具體實現步驟:
1、router.js中添加:
-
-
-
-
-
module.exports = app => {
-
const { router, controller } = app;
-
-
-
-
app.get('/setUserList', controller.new.user.getUserList);
-
app.post('/setUserList', controller.new.user.postUserList);
-
app.put('/setUserList', controller.new.user.putUserList);
-
app.delete('/setUserList', controller.new.user.deleteUserList);
-
-
2、新建:app / controller / new / user.js
(1)查,模糊查詢
select * from user where name like ? % 內容 %
(2)增
this.app.mysql.insert('表名',內容)
(3)改
UPDATE loginlist SET `password` = 'Ad123456' WHERE id = 2
(4)刪
delete from user where id = 1
user.js 源碼:
-
-
-
-
-
-
const Controller = require('egg').Controller;
-
class NewsController extends Controller {
-
-
-
let params = this.ctx.query
-
-
-
let sql = 'select * from user'
-
-
-
-
-
-
-
-
-
-
-
-
-
sql += " where name like ?";
-
content.push( "%"+params.name+"%" );
-
-
-
-
-
-
-
sql += " WHERE age LIKE ?";
-
-
content.push( "%"+params.age+"%" )
-
-
-
-
-
sql += "and address LIKE ?";
-
-
sql += " WHERE address LIKE ?";
-
-
content.push( "%"+params.address+"%" )
-
-
-
-
-
sql += "and phone LIKE ?";
-
-
sql += " WHERE phone LIKE ?";
-
-
content.push( "%"+params.phone+"%" )
-
-
-
-
if(params.page || params.pageSize){
-
let current = params.page;
-
let pageSize = params.pageSize;
-
-
content.push((current-1)*pageSize,parseInt(pageSize));
-
-
-
let allList = await this.app.mysql.query(initSql);
-
let userList= await this.app.mysql.query(
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
console.log(this.ctx.request.body);
-
-
let data = this.ctx.request.body
-
data.id = parseInt(Math.random()*100000)
-
let insertResult = await this.app.mysql.insert(
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
let id = this.ctx.query.id
-
let data = this.ctx.request.body
-
let sql = 'update user set '
-
-
-
-
-
-
-
-
-
-
-
-
sql += 'name = "'+data.name+'"'
-
-
-
-
-
-
sql += ',age = "'+data.age+'"'
-
-
sql += 'age = "'+data.age+'"'
-
-
-
-
-
-
-
sql += ',address = "'+data.address+'"'
-
-
sql += 'address = "'+data.address+'"'
-
-
-
-
-
-
-
sql += ',phone = "'+data.phone+'"'
-
-
sql += 'phone = "'+data.phone+'"'
-
-
-
-
-
-
-
sql += ',email = "'+data.email+'"'
-
-
sql += 'email = "'+data.email+'"'
-
-
-
sql += ' where id = ' + id
-
-
let insertResult = await this.app.mysql.query(sql)
-
-
-
-
-
-
-
-
-
-
-
-
-
let params = this.ctx.query
-
-
-
let sql = 'delete from user where id = '+ params.id
-
let res = await this.app.mysql.query(
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
const TABLE_NAME = 'user';
-
const QUERY_STR = 'id, name, age, phone, address';
-
let sql = `select ${QUERY_STR} from ${TABLE_NAME} where authName like "%${auth.authName}%"`;
-
const row = await this.app.mysql.query(sql);
-
-
-
-
-
-
module.exports = NewsController;
-
3、引入mysql:點我
4、跨域問題解決:點我
二、前端部分:
新建vue文件:testApi.vue
1、初始化查詢列表數據:頁面展示列表,列表數據mouted獲取
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
<template slot-scope="scope">
-
<el-button type="text" size="small" @click="editFun(scope.row)">編輯</el-button>
-
<el-button type="text" size="small" @click="deleteFun(scope.row)">刪除</el-button>
-
-
-
-
-
-
-
-
-
-
-
-
-
page: this.tableInfo.page,
-
pageSize: this.tableInfo.pageSize
-
-
this.$axios.get('http://localhost:7001/setUserList',{
-
-
-
this.tableInfo.list = res.data.data.list
-
this.tableInfo.total = res.data.data.total
-
-
this.$message(error.data.message)
-
-
-
-
2、頁面點擊新增:展示彈框,并將彈框內容去除掉,點擊新增,將彈框內容發送給后端
3、頁面點擊列表里面具體數據的編輯:彈框,并回填數據,修改將當前數據的id和表格數據傳給后端
4、刪除按鈕,點擊出現二次確認彈框,點擊確認將當前數據的id給后端就行
testApi.vue 源碼:
-
-
-
<el-button type='success' @click="addModal">新增</el-button>
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
<template slot-scope="scope">
-
<el-button type="text" size="small" @click="editFun(scope.row)">編輯</el-button>
-
<el-button type="text" size="small" @click="deleteFun(scope.row)">刪除</el-button>
-
-
-
-
-
-
-
:layout="'total,prev, pager, next,sizes'"
-
-
:hide-on-single-page="false"
-
:current-page="tableInfo.page"
-
:page-size="tableInfo.pageSize"
-
@current-change="pageChange"
-
@size-change="pageSizeChange"
-
-
-
-
-
-
-
-
:visible.sync="modal.show"
-
:width="modal.type === 'delete'? '470px' : '800px'">
-
<h3 v-if="modal.type === 'add'" slot="title">新增</h3>
-
<h3 v-if="modal.type === 'edit'" slot="title">修改</h3>
-
<h3 v-if="modal.type === 'delete'" slot="title">提示</h3>
-
<div v-if="modal.type !== 'delete'" class="editNameBody">
-
<el-form class="editNameFrom" :model="modalFrom" label-width="120px" label-position="right">
-
<el-form-item label="姓名:">
-
<el-input v-model="modalFrom.name"></el-input>
-
-
<el-form-item label="年紀:">
-
<el-input v-model="modalFrom.age"></el-input>
-
-
<el-form-item label="手機號:">
-
<el-input v-model="modalFrom.phone"></el-input>
-
-
<el-form-item label="郵箱:">
-
<el-input v-model="modalFrom.email"></el-input>
-
-
<el-form-item label="地址:">
-
<el-input v-model="modalFrom.address" type="textarea"></el-input>
-
-
-
-
<div v-if="modal.type === 'delete'">
-
<i class="el-icon-warning" style="margin-right:7px;color:#FFAA00;font-size: 16px;"></i>請確認是否刪除
-
-
<div slot="footer" class="dialog-footer">
-
<el-button @click="modal.show = false">取 消</el-button>
-
<el-button v-if="modal.type === 'add'" type="primary" @click="addUserList">新增</el-button>
-
<el-button v-if="modal.type === 'edit'" type="primary" @click="editUserList">修改</el-button>
-
<el-button v-if="modal.type === 'delete'" type="primary" @click="deleteUserList">確 認</el-button>
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
page: this.tableInfo.page,
-
pageSize: this.tableInfo.pageSize
-
-
this.$axios.get('http://localhost:7001/setUserList',{
-
-
-
this.tableInfo.list = res.data.data.list
-
this.tableInfo.total = res.data.data.total
-
-
this.$message(error.data.message)
-
-
-
-
-
-
-
this.tableInfo.page = page
-
-
-
-
-
-
pageSizeChange(pageSize) {
-
-
this.tableInfo.pageSize = pageSize
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
this.modal.type = 'delete'
-
-
-
-
-
url:'http://localhost:7001/setUserList',
-
-
-
this.$message.success('新增成功')
-
-
-
-
-
-
-
-
url:'http://localhost:7001/setUserList',
-
-
-
-
-
-
this.$message.success('修改成功')
-
-
-
-
-
-
-
-
url:'http://localhost:7001/setUserList',
-
-
-
-
-
this.$message.success('刪除成功')
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
藍藍設計建立了UI設計分享群,每天會分享國內外的一些優秀設計,如果有興趣的話,可以進入一起成長學習,請掃碼藍小助,報下信息,藍小助會請您入群。歡迎您加入噢~~希望得到建議咨詢、商務合作,也請與我們聯系。
分享此文一切功德,皆悉回向給文章原作者及眾讀者.
轉自:csdn
免責聲明:藍藍設計尊重原作者,文章的版權歸原作者。如涉及版權問題,請及時與我們取得聯系,我們立即更正或刪除。
藍藍設計( www.syprn.cn )是一家專注而深入的界面設計公司,為期望卓越的國內外企業提供卓越的UI界面設計、BS界面設計 、 cs界面設計 、 ipad界面設計 、 包裝設計 、 圖標定制 、 用戶體驗 、交互設計、 網站建設 、平面設計服務