• ElementUI之CUD+表单验证


    目录

    前言:

    增删改查

    表单验证


    前言:

    继上篇博客来写我们的增删改以及表单验证

    增删改查

    首先先定义接口

     数据样式,我们可以去elementUI官网去copy我们喜欢的样式

    1. <!-- 编辑窗体 -->
    2. <el-dialog :title="title" :visible.sync="dialogFormVisible" @close="clear">
    3. <el-form :model="book" :rules="rules" ref="book">
    4. <el-form-item label="书籍编号" :label-width="formLabelWidth" prop="id">
    5. <el-input v-model="book.id" autocomplete="off"></el-input>
    6. </el-form-item>
    7. <el-form-item label="书籍名称" :label-width="formLabelWidth" prop="bookname">
    8. <el-input v-model="book.bookname" autocomplete="off"></el-input>
    9. </el-form-item>
    10. <el-form-item label="书籍价格" :label-width="formLabelWidth" prop="price">
    11. <el-input v-model="book.price" autocomplete="off"></el-input>
    12. </el-form-item>
    13. <el-form-item label="书籍类别" :label-width="formLabelWidth" prop="booktype">
    14. <el-select v-model="book.booktype" placeholder="请选择活动区域">
    15. <el-option v-for="t in types" :label="t.name" :value="t.name" :key="'key_'+t.id"></el-option>
    16. </el-select>
    17. </el-form-item>
    18. </el-form>
    19. <div slot="footer" class="dialog-footer">
    20. <el-button @click="dialogFormVisible = false">取 消</el-button>
    21. <el-button type="danger" @click="dosub">确 定</el-button>
    22. </div>
    23. </el-dialog>
    1. export default {
    2. data() {
    3. return {
    4. bookname: '',
    5. tableData: [],
    6. rows: 10,
    7. total: 0,
    8. page: 1,
    9. title: '新增窗体',
    10. dialogFormVisible: false,
    11. formLabelWidth: '100px',
    12. types: [],
    13. book: {
    14. id: '',
    15. bookname: '',
    16. price: '',
    17. booktype: ''
    18. },
    19. rules: {
    20. bookname: [{
    21. required: true,
    22. message: '请输入书籍名称',
    23. trigger: 'blur'
    24. }],
    25. price: [{
    26. required: true,
    27. message: '请输入书籍名称',
    28. trigger: 'blur'
    29. }],
    30. booktype: [{
    31. required: true,
    32. message: '请输入书籍名称',
    33. trigger: 'blur'
    34. }]
    35. }
    36. }
    37. },
    38. methods: {
    39. del(idx, row) {
    40. this.$confirm('此操作将永久删除id为' + row.id + '的数据, 是否继续?', '提示', {
    41. confirmButtonText: '确定',
    42. cancelButtonText: '取消',
    43. type: 'warning'
    44. }).then(() => {
    45. let url = this.axios.urls.SYSTEM_BookDEL;
    46. this.axios.post(url, {
    47. id: row.id
    48. }).then(d => {
    49. console.log(d);
    50. this.$message({
    51. type: 'success',
    52. message: '删除成功!'
    53. });
    54. this.query({});
    55. this.tableData = d.data.rows;
    56. this.total = d.data.total;
    57. }).catch(e => {
    58. })
    59. }).catch(() => {
    60. this.$message({
    61. type: 'info',
    62. message: '已取消删除'
    63. });
    64. });
    65. },
    66. dosub() {
    67. this.$refs['book'].validate((valid) => {
    68. if (valid) {
    69. alert('submit!');
    70. let url = this.axios.urls.SYSTEM_BookADD;
    71. if (this.title == '编辑窗体') {
    72. url = this.axios.urls.SYSTEM_BookUPD;
    73. }
    74. let params = {
    75. id: this.book.id,
    76. bookname: this.book.bookname,
    77. price: this.book.price,
    78. booktype: this.book.booktype
    79. }
    80. this.axios.post(url, params).then(d => {
    81. console.log(d);
    82. this.clear();
    83. this.query({});
    84. this.tableData = d.data.rows;
    85. this.total = d.data.total;
    86. }).catch(e => {
    87. })
    88. } else {
    89. console.log('error submit!!');
    90. return false;
    91. }
    92. });
    93. },
    94. clear() {
    95. //初始化窗体
    96. this.dialogFormVisible = false;
    97. this.title = '新增窗体';
    98. this.book = {
    99. id: '',
    100. bookname: '',
    101. price: '',
    102. booktype: ''
    103. }
    104. },
    105. open(idx, row) {
    106. //打开窗体的方法
    107. this.dialogFormVisible = true;
    108. if (row) {
    109. this.title = '编辑窗体';
    110. this.book.id = row.id;
    111. this.book.bookname = row.bookname;
    112. this.book.price = row.price;
    113. this.book.booktype = row.booktype;
    114. }
    115. },
    116. handleSizeChange(r) {
    117. //当页大小发生变化
    118. let params = {
    119. bookname: this.bookname,
    120. rows: r,
    121. page: this.page
    122. }
    123. // console.log(params)
    124. this.query(params);
    125. },
    126. handleCurrentChange(p) {
    127. //当前页码大小发生变化
    128. let params = {
    129. bookname: this.bookname,
    130. rows: this.rows,
    131. page: p
    132. }
    133. // console.log(params)
    134. this.query(params);
    135. },
    136. query(params) {
    137. //获取后台请求书籍数据的地址
    138. let url = this.axios.urls.SYSTEM_BookList;
    139. this.axios.get(url, {
    140. params: params
    141. }).then(d => {
    142. console.log(url)
    143. this.tableData = d.data.rows;
    144. this.total = d.data.total;
    145. }).catch(e => {});
    146. },
    147. onSubmit() {
    148. let params = {
    149. bookname: this.bookname
    150. }
    151. console.log(params)
    152. this.query(params);
    153. this.bookname = ''
    154. }
    155. },
    156. created() {
    157. this.query({});
    158. this.types = [{
    159. id: 1,
    160. name: '玄幻'
    161. }, {
    162. id: 2,
    163. name: '动作'
    164. }, {
    165. id: 3,
    166. name: '爱情'
    167. }, {
    168. id: 4,
    169. name: '伦理'
    170. }, {
    171. id: 5,
    172. name: '搞笑'
    173. }];
    174. }
    175. }

     上效果图:

    表单验证

    表单验证我们也可以去elementUI官网去copy一份

     指定验证需要添加该有的属性

    写我们的正则方法

    1. rules: {
    2. bookname: [{
    3. required: true,
    4. message: '请输入书籍名称',
    5. trigger: 'blur'
    6. }],
    7. price: [{
    8. required: true,
    9. message: '请输入书籍价格',
    10. trigger: 'blur'
    11. }],
    12. booktype: [{
    13. required: true,
    14. message: '请输入书籍类型',
    15. trigger: 'blur'
    16. }]
    17. }

     最后就是我们的验证正则

    1. dosub() {
    2. this.$refs['book'].validate((valid) => {
    3. if (valid) {
    4. let url = this.axios.urls.SYSTEM_BookADD;
    5. if (this.title == '编辑窗体') {
    6. url = this.axios.urls.SYSTEM_BookUPD;
    7. }
    8. let params = {
    9. id: this.book.id,
    10. bookname: this.book.bookname,
    11. price: this.book.price,
    12. booktype: this.book.booktype
    13. }
    14. this.axios.post(url, params).then(d => {
    15. console.log(d);
    16. this.clear();
    17. this.query({});
    18. this.tableData = d.data.rows;
    19. this.total = d.data.total;
    20. }).catch(e => {
    21. })
    22. } else {
    23. console.log('error submit!!');
    24. return false;
    25. }
    26. });
    27. },

    效果图

  • 相关阅读:
    jsencrypt 公私钥解加密
    力扣第77题 组合 c++ 回溯经典题 注释加优化 代码
    38. 多态中的静态联编和动态联编
    软件测试/测试开发丨接口测试学习笔记-常见的接口协议
    劳动节福利~ 我给大家写了个博客园快速发文工具
    SpringSecurity 完整认证流程
    虹科示波器 | 汽车免拆检修 | 2015款奔驰G63AMG车发动机偶尔自动熄火
    【GO】LGTM_Grafana_gozero_配置trace(4)_代码实操及追踪
    如何拿到BAT面试offer的?全靠这份附精华宝典+求职秘籍,查漏补缺!
    48MySQL数据库基础
  • 原文地址:https://blog.csdn.net/m0_74934282/article/details/133358220