• SPA项目开发之CRUD+表单验证


    目录

    一、表单验证

    效果

    二、crud(增删改查)

    Articles.vue

    新增效果

     编辑效果

     删除效果

    一、表单验证

    所需表单

    1. <el-dialog :title="title" :visible.sync="editFormVisible" width="30%" @before-close="closeDialog">
    2. <el-form label-width="120px" :model="editForm" :rules="rules" ref="editForm">
    3. <el-form-item label="文章标题" prop="title">
    4. <el-input size="small" v-model="editForm.title" auto-complete="off" placeholder="请输入文章标题">el-input>
    5. el-form-item>
    6. <el-form-item label="文章内容" prop="body">
    7. <el-input size="small" v-model="editForm.body" auto-complete="off" placeholder="请输入文章内容">el-input>
    8. el-form-item>
    9. el-form>
    10. <div slot="footer" class="dialog-footer">
    11. <el-button size="small" @click="closeDialog">取消el-button>
    12. <el-button size="small" type="primary" class="title" @click="submitForm('editForm')">保存el-button>
    13. div>
    14. el-dialog>

     表单验证的规则(校验) 都放入Articles.vue

    1. data() {
    2. return {
    3. listData: [],
    4. formInline: {
    5. page: 1,
    6. rows: 10,
    7. title: ''
    8. },
    9. total: 0,
    10. title: '',
    11. editFormVisible: false,
    12. editForm: {
    13. title: '',
    14. body: '',
    15. id: 0
    16. },
    17. rules: {
    18. title: [{
    19. required: true,
    20. message: '请输入活动名称',
    21. trigger: 'blur'
    22. },
    23. {
    24. min: 3,
    25. max: 5,
    26. message: '长度在 3 到 5 个字符',
    27. trigger: 'blur'
    28. }
    29. ],
    30. body: [{
    31. required: true,
    32. message: '请输入活动名称',
    33. trigger: 'blur'
    34. }]
    35. }
    36. };
    37. },

    效果:

    二、crud(增删改查)

    Articles.vue

    1. <template>
    2. <div>
    3. <el-form :inline="true" :model="formInline" class="user-search">
    4. <el-form-item label="搜索:">
    5. <el-input
    6. size="small"
    7. v-model="formInline.title"
    8. placeholder="输入文章标题"
    9. >el-input>
    10. el-form-item>
    11. <el-form-item>
    12. <el-button
    13. size="small"
    14. type="primary"
    15. icon="el-icon-search"
    16. @click="search"
    17. >搜索
    18. >
    19. <el-button
    20. size="small"
    21. type="primary"
    22. icon="el-icon-plus"
    23. @click="handleEdit()"
    24. >添加
    25. >
    26. el-form-item>
    27. el-form>
    28. <el-table
    29. size="small"
    30. :data="listData"
    31. element-loading-text="拼命加载中"
    32. style="width: 100%;"
    33. >
    34. <el-table-column align="center" type="selection" min-width="1">
    35. el-table-column>
    36. <el-table-column sortable prop="id" label="文章ID" min-width="1">
    37. el-table-column>
    38. <el-table-column sortable prop="title" label="文章标题" min-width="3">
    39. el-table-column>
    40. <el-table-column sortable prop="body" label="文章内容" min-width="5">
    41. el-table-column>
    42. <el-table-column align="center" label="操作" min-width="6">
    43. <template slot-scope="scope">
    44. <el-button size="mini" @click="doEdit(scope.$index, scope.row)"
    45. >编辑
    46. >
    47. <el-button
    48. size="mini"
    49. type="danger"
    50. @click="deleteUser(scope.$index, scope.row)"
    51. >删除
    52. >
    53. template>
    54. el-table-column>
    55. el-table>
    56. <el-pagination
    57. style="margin-top: 20px;"
    58. @size-change="handleSizeChange"
    59. @current-change="handleCurrentChange"
    60. :current-page="formInline.page"
    61. :page-sizes="[10, 20, 30, 50]"
    62. :page-size="100"
    63. layout="total, sizes, prev, pager, next, jumper"
    64. :total="total"
    65. >
    66. el-pagination>
    67. <el-dialog
    68. :title="title"
    69. :visible.sync="editFormVisible"
    70. width="30%"
    71. @before-close="closeDialog"
    72. >
    73. <el-form
    74. label-width="120px"
    75. :model="editForm"
    76. :rules="rules"
    77. ref="editForm"
    78. >
    79. <el-form-item label="文章标题" prop="title">
    80. <el-input
    81. size="small"
    82. v-model="editForm.title"
    83. auto-complete="off"
    84. placeholder="请输入文章标题"
    85. >el-input>
    86. el-form-item>
    87. <el-form-item label="文章内容" prop="body">
    88. <el-input
    89. size="small"
    90. v-model="editForm.body"
    91. auto-complete="off"
    92. placeholder="请输入文章内容"
    93. >el-input>
    94. el-form-item>
    95. el-form>
    96. <div slot="footer" class="dialog-footer">
    97. <el-button size="small" @click="closeDialog">取消el-button>
    98. <el-button
    99. size="small"
    100. type="primary"
    101. class="title"
    102. @click="submitForm('editForm')"
    103. >保存
    104. >
    105. div>
    106. el-dialog>
    107. div>
    108. template>
    109. <script>
    110. export default {
    111. data() {
    112. return {
    113. listData: [],
    114. title: "",
    115. editFormVisible: false,
    116. formInline: {
    117. page: 1,
    118. rows: 10,
    119. title: ""
    120. },
    121. editForm: {
    122. title: "",
    123. body: "",
    124. id: 0
    125. },
    126. rules: {
    127. title: [
    128. {
    129. required: true,
    130. message: "请输入文章标题",
    131. trigger: "blur"
    132. },
    133. {
    134. min: 3,
    135. max: 5,
    136. message: "长度在 3 到 5 个字符",
    137. trigger: "blur"
    138. }
    139. ],
    140. body: [
    141. {
    142. required: true,
    143. message: "请选择文字内容",
    144. trigger: "blur"
    145. }
    146. ]
    147. },
    148. total: 0
    149. };
    150. },
    151. methods: {
    152. handleSizeChange(rows) {
    153. this.formInline.page = 1;
    154. this.formInline.rows = rows;
    155. this.search();
    156. },
    157. handleCurrentChange(page) {
    158. this.formInline.page = page;
    159. this.search();
    160. },
    161. closeDialog() {
    162. //关闭对话框
    163. this.clearData();
    164. },
    165. handleEdit() {
    166. //添加
    167. this.editFormVisible = true;
    168. this.title = "新增窗体";
    169. },
    170. doEdit(index, row) {
    171. this.editForm.id = row.id;
    172. this.editForm.title = row.title;
    173. this.editForm.body = row.body;
    174. this.editFormVisible = true;
    175. this.title = "编辑窗体";
    176. },
    177. clearData() {
    178. this.title = "";
    179. this.editForm.id = 0;
    180. this.editForm.title = "";
    181. this.editForm.body = "";
    182. this.editFormVisible = false;
    183. },
    184. deleteUser(index, row) {
    185. // //删除
    186. // let url = this.axios.urls.SYSTEM_ARTICLE_DEL;
    187. // this.axios
    188. // .post(url, { id: row.id })
    189. // .then(response => {
    190. // //console.log(response);
    191. // this.$message({
    192. // message: response.data.msg,
    193. // type: 'success'
    194. // });
    195. // this.clearData();
    196. // this.search();
    197. // })
    198. // .catch(function(error) {
    199. // console.log(error);
    200. // });
    201. this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
    202. confirmButtonText: "确定",
    203. cancelButtonText: "取消",
    204. type: "warning"
    205. })
    206. .then(() => {
    207. this.$message({
    208. type: "success",
    209. message: "删除成功!"
    210. });
    211. let url = this.axios.urls.SYSTEM_ARTICLE_DEL;
    212. this.axios
    213. .post(url, { id: row.id })
    214. .then(response => {
    215. //console.log(response);
    216. this.clearData();
    217. this.search();
    218. })
    219. .catch(function(error) {
    220. console.log(error);
    221. });
    222. })
    223. .catch(() => {
    224. this.$message({
    225. type: "info",
    226. message: "已取消删除"
    227. });
    228. });
    229. },
    230. submitForm(formName) {
    231. //验证
    232. this.$refs[formName].validate(valid => {
    233. if (valid) {
    234. let url;
    235. if (this.editForm.id == 0) {
    236. url = this.axios.urls.SYSTEM_ARTICLE_ADD;
    237. } else {
    238. url = this.axios.urls.SYSTEM_ARTICLE_EDIT;
    239. }
    240. // let url = 'http://localhost:8080/T216_SSH/vue/userAction_login.action';
    241. this.axios
    242. .post(url, this.editForm)
    243. .then(response => {
    244. //console.log(response);
    245. // this.$message({
    246. // message: response.data.msg,
    247. // type: 'success'
    248. // });
    249. this.clearData();
    250. this.search();
    251. })
    252. .catch(function(error) {
    253. console.log(error);
    254. });
    255. } else {
    256. console.log("error submit!!");
    257. return false;
    258. }
    259. });
    260. },
    261. doSearch(params) {
    262. let url = this.axios.urls.SYSTEM_ARTICLE_LIST;
    263. // let url = 'http://localhost:8080/T216_SSH/vue/userAction_login.action';
    264. this.axios
    265. .post(url, params)
    266. .then(response => {
    267. console.log(response);
    268. this.listData = response.data.result;
    269. this.total = response.data.pageBean.total;
    270. })
    271. .catch(function(error) {
    272. console.log(error);
    273. });
    274. },
    275. search() {
    276. this.doSearch(this.formInline); //按照条件进行查询
    277. }
    278. },
    279. created() {
    280. this.doSearch({});
    281. }
    282. };
    283. script>
    284. <style>style>

    新增效果

     编辑效果

     

     删除效果

    id为169的就没有了 

  • 相关阅读:
    新型高强度铝合金焊条
    暑假加餐|有钱人和你想的不一样(第20天)+改进的多目标差分进化算法在电力系统环境经济调度中的应用(Python代码实现)
    Systrace系列11 —— Triple Buffer 解读
    ARM_LIB_HEAP 与 RTT_HEAP的区别
    Arduino Cloud 现已支持乐鑫 ESP32-S2、S3 和 C3
    springBoot组件注册
    【马士兵】Python基础--07
    go学习笔记
    阿里内部2400页的Java面试手册开源啦,突袭大厂面试?
    1335_FreeRTOS中xQueueReceiveFromISR函数的实现
  • 原文地址:https://blog.csdn.net/weixin_62735525/article/details/126834969