• 关于一篇ElementUI之CUD+表单验证


    目录

    一.CUD增删改查简述

     1.1.增删改功能实现

    二.表单验证

    前端所有代码:

              好啦今天就分享到这了,希望能帮到你哦!!!


    以下的代码基于我博客中的代码进行续写 : 

    关于ElementUI之动态树+数据表格+分页实例

    一.CUD增删改查简述

    CUD是数据库操作中常用的四个基本操作,分别是创建(Create)、更新(Update)、删除(Delete)和查询(Query)。

    • 创建(Create)操作用于向数据库中添加新的数据记录。这可以通过插入一条新记录来完成,包括提供必要的数据和指定要插入的表。

    • 更新(Update)操作用于修改数据库中的现有数据。通过更新操作,可以更改一个或多个记录的特定字段值,以反映实际数据的变化。

    • 删除(Delete)操作用于从数据库中移除数据记录。可以通过指定特定的条件,删除满足条件的记录,或者直接删除整个表的内容。

    • 查询(Query)操作用于从数据库中检索指定数据记录或满足特定条件的数据记录。查询操作可以根据给定的条件过滤和排序数据,并返回满足条件的结果集。

    这四种操作通常是关系型数据库管理系统(RDBMS)中最基本和常用的操作,用于对数据库进行增加、修改、删除和检索数据。

     1.1.增删改功能实现

    在项目中的src文件下api中找到action.js进行配置数据访问的地址:

    1. /**
    2. * 对后台请求的地址的封装,URL格式如下:
    3. * 模块名_实体名_操作
    4. */
    5. export default {
    6. 'SERVER': 'http://localhost:8080/', //服务器
    7. 'SYSTEM_USER_DOLOGIN': 'user/userLogin', //登陆
    8. 'SYSTEM_USER_DOREG': 'user/userRegister', //注册
    9. 'SYSTEM_MENUS': 'module/queryRootNode', //左侧菜单树
    10. 'BOOK_LIST': 'book/queryBookPager', //书籍列表
    11. 'BOOK_ADD': 'book/addBook', //书籍增加
    12. 'BOOK_UPD': 'book/editBook', //书籍修改
    13. 'BOOK_DEL': 'book/delBook', //书籍删除
    14. 'getFullPath': k => { //获得请求的完整地址,用于mockjs测试时使用
    15. return this.SERVER + this[k];
    16. }
    17. }

    在BookList.vue组件中进行编写增加修改的代码:

    ElementUI里面找到弹出窗进行编写增加修改的弹窗如下:

    1. <el-dialog :title="title" :visible.sync="dialogFormVisible" @close="clear">
    2. <el-form :model="book" :rules="rules" ref="book">
    3. <el-form-item label="书籍编号" :label-width="formLabelWidth" prop="id">
    4. <el-input v-model="book.id" autocomplete="off">el-input>
    5. el-form-item>
    6. <el-form-item label="书籍名称" :label-width="formLabelWidth" prop="bookname">
    7. <el-input v-model="book.bookname" autocomplete="off">el-input>
    8. el-form-item>
    9. <el-form-item label="书籍价格" :label-width="formLabelWidth" prop="price">
    10. <el-input v-model="book.price" autocomplete="off">el-input>
    11. el-form-item>
    12. <el-form-item label="书籍类别" :label-width="formLabelWidth" prop="booktype">
    13. <el-select v-model="book.booktype" placeholder="请选择活动区域">
    14. <el-option v-for="t in types" :label="t.name" :value="t.name" :key="'key_'+t.id">el-option>
    15. el-select>
    16. el-form-item>
    17. el-form>
    18. <div slot="footer" class="dialog-footer">
    19. <el-button @click="dialogFormVisible = false">取 消el-button>
    20. <el-button type="primary" @click="dosub">确 定el-button>
    21. div>
    22. el-dialog>

    在script标签中编写方法进行数据增加修改实现,在data中编写属性:

    1. data() {
    2. return {
    3. bookname: '',
    4. tableData: [],
    5. rows: 10,
    6. page: 1,
    7. total: 0,
    8. title: '新增窗体',
    9. dialogFormVisible: false,
    10. formLabelWidth: '100px',
    11. types: [],
    12. form: {},
    13. book: {
    14. id: '',
    15. bookname: '',
    16. price: '',
    17. booktype: ''
    18. },
    19. }
    20. },

    在script标签中编写方法进行数据增加修改实现,在methods中编写方法:

    1. methods: {
    2. del(idx, row) {
    3. this.$confirm('此操作将永久删除id为' + row.id + '的数据, 是否继续?', '提示', {
    4. confirmButtonText: '确定',
    5. cancelButtonText: '取消',
    6. type: 'warning'
    7. }).then(() => {
    8. let url = this.axios.urls.BOOK_DEL;
    9. this.axios.post(url, {
    10. id: row.id
    11. })
    12. .then(r => {
    13. console.log(r);
    14. this.$message({
    15. type: 'success',
    16. message: '删除成功!'
    17. });
    18. this.query({});
    19. })
    20. .catch(e => {
    21. // 处理错误
    22. });
    23. }).catch(() => {
    24. this.$message({
    25. type: 'info',
    26. message: '已取消删除'
    27. });
    28. });
    29. },
    30. dosub() {
    31. this.$refs['book'].validate((valid) => {
    32. if (valid) {
    33. let url = this.axios.urls.BOOK_ADD;
    34. if (this.title == '编辑窗体') {
    35. url = this.axios.urls.BOOK_UPD;
    36. }
    37. // 构建请求参数
    38. let params = {
    39. id: this.book.id,
    40. bookname: this.book.bookname,
    41. price: this.book.price,
    42. booktype: this.book.booktype
    43. };
    44. console.log(params)
    45. console.log(url)
    46. this.axios.post(url, params)
    47. .then(r => {
    48. console.log(r);
    49. // 编辑/添加操作成功后,再进行查询
    50. this.query({});
    51. this.clear();
    52. })
    53. .catch(e => {
    54. // 处理错误
    55. });
    56. } else {
    57. console.log('error submit!!');
    58. return false;
    59. }
    60. });
    61. },
    62. clear() {
    63. // 初始化窗体
    64. this.dialogFormVisible = false;
    65. this.title = '新增窗体';
    66. // 清空表单
    67. this.book = {
    68. id: '',
    69. bookname: '',
    70. price: '',
    71. booktype: ''
    72. };
    73. },
    74. // 打开窗体的方法
    75. open(idx, row) {
    76. // 打开编辑窗口
    77. this.dialogFormVisible = true;
    78. // 初始化标题
    79. this.title = '新增窗体';
    80. if (row != null) {
    81. this.title = '编辑窗体';
    82. }
    83. // 如果有传入行数据,将其填充到表单字段中
    84. if (row) {
    85. this.book.id = row.id;
    86. this.book.bookname = row.bookname;
    87. this.book.price = row.price;
    88. this.book.booktype = row.booktype;
    89. }
    90. },
    91. query(params) {
    92. let url = this.axios.urls.BOOK_LIST;
    93. this.axios.get(url, {
    94. params: params
    95. }).then(r => {
    96. console.log(r);
    97. this.tableData = r.data.rows;
    98. this.total = r.data.total;
    99. }).catch(e => {
    100. })
    101. },
    102. onSubmit() {
    103. let params = {
    104. bookname: this.bookname
    105. }
    106. this.query(params);
    107. },
    108. handleSizeChange(r) {
    109. //当页大小发生变化
    110. console.log("当前页大小为:" + r);
    111. let params = {
    112. bookname: this.bookname,
    113. rows: r,
    114. page: this.page
    115. }
    116. this.query(params);
    117. },
    118. handleCurrentChange(p) {
    119. //当前页码发生变化
    120. console.log("当前页页码为:" + p);
    121. let params = {
    122. bookname: this.bookname,
    123. rows: this.rows,
    124. page: p
    125. }
    126. this.query(params);
    127. }
    128. },

    在created中初始化数据,如下:

    1. created() {
    2. this.query({});
    3. this.types = [{
    4. id: 1,
    5. name: "刘勾八"
    6. },
    7. {
    8. id: 2,
    9. name: "渣渣辉"
    10. },
    11. {
    12. id: 3,
    13. name: "耗子"
    14. },
    15. {
    16. id: 4,
    17. name: "叼毛"
    18. },
    19. {
    20. id: 5,
    21. name: "耗子"
    22. }
    23. ];
    24. }

    实例效果:

    二.表单验证

    在表单中增加以下两个属性 :

    :rules="rules" ref="book"

     在script标签中编写方法进行数据增加修改实现,在data中编写属性:

    1. data() {
    2. return {
    3. bookname: '',
    4. tableData: [],
    5. rows: 10,
    6. total: 0,
    7. page: 1,
    8. formLabelWidth: '120px', //弹出窗输入框前的文字宽度
    9. title: '书籍新增', //弹出窗标题
    10. dialogFormVisible: false, //默认关闭
    11. book: {
    12. id: '',
    13. bookname: '',
    14. price: '',
    15. booktype: ''
    16. },
    17. types: [],
    18. //增加表单验证
    19. rules: {
    20. bookname: [{
    21. required: true,
    22. message: '请输入书籍的名称',
    23. trigger: 'blur'
    24. },
    25. {
    26. min: 2,
    27. max: 10,
    28. message: '书籍名称长度在 2 到 10 个字符',
    29. trigger: 'blur'
    30. }
    31. ],
    32. price: [{
    33. required: true,
    34. message: '请填写书籍价格',
    35. trigger: 'blur'
    36. }],
    37. booktype: [{
    38. required: true,
    39. message: '请选择书籍类型',
    40. trigger: 'blur'
    41. }]
    42. }
    43. }
    44. },

    在script标签中编写方法,在methods中编写submit()方法:

    1. submit() {
    2. //获取值
    3. let params = {
    4. id: this.book.id,
    5. bookname: this.book.bookname,
    6. price: this.book.price,
    7. booktype: this.book.booktype
    8. }
    9. console.log(params);
    10. //获取配置的方法请求地址
    11. let url = this.axios.urls.SYSTEM_BookAdd;
    12. //如果是书籍编辑就将请求地址修改为书籍修改的请求地址
    13. if (this.title == '书籍编辑') {
    14. url = this.axios.urls.SYSTEM_BookEdit;
    15. }
    16. //请求前必须通过表单验证
    17. this.$refs['book'].validate((valid) => {
    18. console.log(valid);
    19. if (valid) {
    20. //请求后端地址进行书籍的新增或修改
    21. this.axios.post(url, params).then(d => {
    22. // console.log(url);
    23. // console.log(d);
    24. this.close();
    25. this.query({});
    26. }).catch(e => {});
    27. } else {
    28. this.$message('有必输入项或者没有按要求输入,请正确填写!!');
    29. return false;
    30. }
    31. });
    32. }

    表单验证的效果  :

    前端所有代码:

    BookList.vue 所有代码如下  :

    1. <template>
    2. <div class="Book" style="padding: 30px;">
    3. <el-form :inline="true" class="demo-form-inline">
    4. <el-form-item label="书籍名称 : ">
    5. <el-input v-model="bookname" placeholder="书籍名称">el-input>
    6. el-form-item>
    7. <el-form-item>
    8. <el-button type="primary" plain @click="onSubmit">查询el-button>
    9. el-form-item>
    10. <el-form-item>
    11. <el-button type="primary" plain @click="open">新增el-button>
    12. el-form-item>
    13. el-form>
    14. <el-table :data="tableData" style="width: 100%">
    15. <el-table-column prop="id" label="书籍ID">el-table-column>
    16. <el-table-column prop="bookname" label="书籍名称">el-table-column>
    17. <el-table-column prop="price" label="书籍价格">el-table-column>
    18. <el-table-column prop="booktype" label="书籍类型">el-table-column>
    19. <el-table-column label="操作">
    20. <template slot-scope="scope">
    21. <el-button size="mini" @click="open(scope.$index,scope.row)">编辑el-button>
    22. <el-button size="mini" type="danger" @click="Del(scope.row)">删除el-button>
    23. template>
    24. el-table-column>
    25. el-table>
    26. <div class="block" style="padding: 20px;">
    27. <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="page"
    28. background :page-sizes="[10, 20, 30, 40]" :page-size="rows" layout="total, sizes, prev, pager, next, jumper"
    29. :total="total">
    30. el-pagination>
    31. div>
    32. <el-dialog :title="title" :visible.sync="dialogFormVisible" :before-close="close">
    33. <el-form :model="book" :rules="rules" ref="book">
    34. <el-form-item label="书籍编号 :" :label-width="formLabelWidth">
    35. <el-input v-model="book.id" autocomplete="off">el-input>
    36. el-form-item>
    37. <el-form-item label="书籍名称 :" prop="bookname" :label-width="formLabelWidth">
    38. <el-input v-model="book.bookname" autocomplete="off">el-input>
    39. el-form-item>
    40. <el-form-item label="书籍价格 :" prop="price" :label-width="formLabelWidth">
    41. <el-input v-model="book.price" autocomplete="off">el-input>
    42. el-form-item>
    43. <el-form-item label="书籍类型 :" prop="booktype" :label-width="formLabelWidth">
    44. <el-select v-model="book.booktype" placeholder="请选择书籍类型">
    45. <el-option v-for="t in types" :label="t.tname" :value="t.tname" :key="'key'+t.tid">el-option>
    46. el-select>
    47. el-form-item>
    48. el-form>
    49. <div slot="footer" class="dialog-footer">
    50. <el-button @click="close">取 消el-button>
    51. <el-button type="primary" @click="submit">确 定el-button>
    52. div>
    53. el-dialog>
    54. div>
    55. template>
    56. <script>
    57. export default {
    58. data() {
    59. return {
    60. bookname: '',
    61. tableData: [],
    62. rows: 10,
    63. total: 0,
    64. page: 1,
    65. formLabelWidth: '120px', //弹出窗输入框前的文字宽度
    66. title: '书籍新增', //弹出窗标题
    67. dialogFormVisible: false, //默认关闭
    68. book: {
    69. id: '',
    70. bookname: '',
    71. price: '',
    72. booktype: ''
    73. },
    74. types: [],
    75. //增加表单验证
    76. rules: {
    77. bookname: [{
    78. required: true,
    79. message: '请输入书籍的名称',
    80. trigger: 'blur'
    81. },
    82. {
    83. min: 2,
    84. max: 10,
    85. message: '书籍名称长度在 2 到 10 个字符',
    86. trigger: 'blur'
    87. }
    88. ],
    89. price: [{
    90. required: true,
    91. message: '请填写书籍价格',
    92. trigger: 'blur'
    93. }],
    94. booktype: [{
    95. required: true,
    96. message: '请选择书籍类型',
    97. trigger: 'blur'
    98. }]
    99. }
    100. }
    101. },
    102. methods: {
    103. //书籍删除的方法
    104. Del(r) {
    105. this.$confirm('你确定将编号为' + r.id + '的书籍永久删除, 是否继续?', '提示', {
    106. confirmButtonText: '确定',
    107. cancelButtonText: '取消',
    108. type: 'warning'
    109. }).then(() => {
    110. //获取配置的书籍删除方法请求地址
    111. let url = this.axios.urls.SYSTEM_BookDel;
    112. //请求后端地址进行书籍的新增或修改
    113. this.axios.post(url, {
    114. id: r.id
    115. }).then(d => {
    116. this.$message({
    117. type: 'success',
    118. message: '书籍删除成功!'
    119. });
    120. this.query({});
    121. }).catch(e => {});
    122. }).catch(() => {
    123. this.$message({
    124. type: 'info',
    125. message: '已取消删除'
    126. });
    127. });
    128. },
    129. submit() {
    130. //获取值
    131. let params = {
    132. id: this.book.id,
    133. bookname: this.book.bookname,
    134. price: this.book.price,
    135. booktype: this.book.booktype
    136. }
    137. console.log(params);
    138. //获取配置的方法请求地址
    139. let url = this.axios.urls.SYSTEM_BookAdd;
    140. //如果是书籍编辑就将请求地址修改为书籍修改的请求地址
    141. if (this.title == '书籍编辑') {
    142. url = this.axios.urls.SYSTEM_BookEdit;
    143. }
    144. //请求前必须通过表单验证
    145. this.$refs['book'].validate((valid) => {
    146. console.log(valid);
    147. if (valid) {
    148. //请求后端地址进行书籍的新增或修改
    149. this.axios.post(url, params).then(d => {
    150. // console.log(url);
    151. // console.log(d);
    152. this.close();
    153. this.query({});
    154. }).catch(e => {});
    155. } else {
    156. this.$message('有必输入项或者没有按要求输入,请正确填写!!');
    157. return false;
    158. }
    159. });
    160. },
    161. //弹出窗取消,值初始化
    162. close() {
    163. this.book = {
    164. id: '',
    165. bookname: '',
    166. price: '',
    167. booktype: ''
    168. };
    169. this.dialogFormVisible = false;
    170. },
    171. //打开弹出窗,进行书籍的编辑
    172. open(index, row) {
    173. this.dialogFormVisible = true;
    174. if (row) {
    175. this.title = '书籍编辑';
    176. this.book.id = row.id;
    177. this.book.bookname = row.bookname
    178. this.book.price = row.price;
    179. this.book.booktype = row.booktype;
    180. }
    181. },
    182. handleSizeChange(r) {
    183. //当页大小发生变化
    184. let params = {
    185. bookname: this.bookname,
    186. rows: r,
    187. page: this.page
    188. }
    189. // console.log(params)
    190. this.query(params);
    191. },
    192. handleCurrentChange(p) {
    193. //当前页码大小发生变化
    194. let params = {
    195. bookname: this.bookname,
    196. rows: this.rows,
    197. page: p
    198. }
    199. // console.log(params)
    200. this.query(params);
    201. },
    202. query(params) {
    203. //获取后台请求书籍数据的地址
    204. let url = this.axios.urls.SYSTEM_BookList;
    205. this.axios.get(url, {
    206. params: params
    207. }).then(d => {
    208. // console.log(url)
    209. this.tableData = d.data.rows;
    210. this.total = d.data.total;
    211. }).catch(e => {});
    212. },
    213. onSubmit() {
    214. let params = {
    215. bookname: this.bookname
    216. }
    217. // console.log(params)
    218. this.query(params);
    219. this.bookname = ''
    220. }
    221. },
    222. created() {
    223. this.query({});
    224. //初始书籍类型的书籍
    225. this.types = [{
    226. tid: 1,
    227. tname: '刘勾八'
    228. }, {
    229. tid: 2,
    230. tname: '渣渣辉'
    231. }, {
    232. tid: 3,
    233. tname: '耗子'
    234. }, {
    235. tid: 4,
    236. tname: '叼毛'
    237. }, {
    238. tid: 5,
    239. tname: '耗子'
    240. }];
    241. }
    242. }
    243. script>
    244. <style>
    245. style>

              好啦今天就分享到这了,希望能帮到你哦!!!

  • 相关阅读:
    计算机组成原理-华科版本
    java中对象和类应用实例
    SQL注入简介
    一小时快速上手win10 docker 、vscode、MobaXterm、git协作开发
    前端工程化 之 Node 基础
    TypeScript - 枚举类型 -字符型枚举
    简单聊聊异常体系
    OCR是什么意思,有哪些好用的OCR识别软件?
    实现list的简单模型
    Visual Studio 2022的安装 - 编程手把手系列文章
  • 原文地址:https://blog.csdn.net/m0_74915426/article/details/133720433