spa项目之CUD+表单验证

2023-10-15 03:30
文章标签 表单 项目 验证 cud spa

本文主要是介绍spa项目之CUD+表单验证,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

CUD+表单验证

    • 表单验证
    • 增删改功能实现
        • 新增和修改
        • 删除

表单验证

Form组件提供了表单验证的功能,只需要通过 rules 属性传入约定的验证规则,
并将Form-Item的prop属性设置为需校验的字段名即可

		<el-dialog :title="title" :visible.sync="editFormVisible" width="30%" @before-close="closeDialog"><el-form label-width="120px" :model="editForm" :rules="rules" ref="editForm"><el-form-item label="文章标题" prop="title"><el-input size="small" v-model="editForm.title" auto-complete="off" placeholder="请输入文章标题"></el-input></el-form-item><el-form-item label="文章内容" prop="body"><el-input size="small" v-model="editForm.body" auto-complete="off" placeholder="请输入文章内容"></el-input></el-form-item></el-form><div slot="footer" class="dialog-footer"><el-button size="small" @click="closeDialog">取消</el-button><el-button size="small" type="primary" class="title" @click="submitForm('editForm')">保存</el-button></div></el-dialog>
data() {return {listData: [],formInline: {page: 1,rows: 10,title: ''},total: 0,title: '',editFormVisible: false,editForm: {title: '',body: '',id: 0},rules: {title: [{required: true,message: '请输入活动名称',trigger: 'blur'},{min: 3,max: 5,message: '长度在 3 到 5 个字符',trigger: 'blur'}],body: [{required: true,message: '请输入活动名称',trigger: 'blur'}]}};},

增删改功能实现

新增和修改
submitForm(formName) {/* 表单验证 */this.$refs[formName].validate((valid) => {if (valid) {//移交后台数据let url;/* 判断调用新增或修改方法 */if(this.editForm.id == 0){url = this.axios.urls.SYSTEM_ARTICLE_ADD;}else{url = this.axios.urls.SYSTEM_ARTICLE_EDIT;}this.axios.post(url,this.editForm).then((response) => {console.log(response);//操作成功后关闭框,并自动刷新this.clearData();this.search();}).catch(function(error) {console.log(error);});		} else {console.log('error submit!!');return false;}});}
删除
handleDelete(index,row){let url = this.axios.urls.SYSTEM_ARTICLE_DELthis.axios.post(url, {id:row.id}).then((response) => {console.log(response);alert('删除成功');//操作成功后关闭框,并自动刷新this.clearData();this.search();}).catch(function(error) {console.log(error);});}

全部代码

<template><div><el-form :inline="true" :model="formInline" class="user-search"><el-form-item label="搜索:"><el-input size="small" v-model="formInline.title" placeholder="输入文章标题"></el-input></el-form-item><el-form-item><el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索</el-button><el-button size="small" type="primary" icon="el-icon-plus" @click="handleEdit()">添加</el-button></el-form-item></el-form><!--列表--><el-table size="small" :data="listData" element-loading-text="拼命加载中" style="width: 100%;"><el-table-column align="center" type="selection" min-width="1"></el-table-column><el-table-column sortable prop="id" label="文章ID" min-width="1"></el-table-column><el-table-column sortable prop="title" label="文章标题" min-width="2"></el-table-column><el-table-column sortable prop="body" label="文章内容" min-width="5"></el-table-column><el-table-column align="center" label="操作" min-width="1"><template slot-scope="scope"><el-button size="mini" @click="doEdit(scope.$index, scope.row)">编辑</el-button><el-button size="mini" type="danger" @click="deleteUser(scope.$index, scope.row)">删除</el-button></template></el-table-column></el-table><el-pagination style="margin-top: 20px;" @size-change="handleSizeChange" @current-change="handleCurrentChange":current-page="formInline.page" :page-sizes="[10, 20, 30, 50]" :page-size="100" layout="total, sizes, prev, pager, next, jumper":total="total"></el-pagination><!-- 编辑界面 --><el-dialog :title="title" :visible.sync="editFormVisible" width="30%" @before-close="closeDialog"><el-form label-width="120px" :model="editForm" :rules="rules" ref="editForm"><el-form-item label="文章标题" prop="title"><el-input size="small" v-model="editForm.title" auto-complete="off" placeholder="请输入文章标题"></el-input></el-form-item><el-form-item label="文章内容" prop="body"><el-input size="small" v-model="editForm.body" auto-complete="off" placeholder="请输入文章内容"></el-input></el-form-item></el-form><div slot="footer" class="dialog-footer"><el-button size="small" @click="closeDialog">取消</el-button><el-button size="small" type="primary" class="title" @click="submitForm('editForm')">保存</el-button></div></el-dialog></div>
</template><script>export default {data() {return {listData: [],formInline: {page: 1,rows: 10,title: ''},total: 0,title: '',editFormVisible: false,editForm: {title: '',body: '',id: 0},rules: {title: [{required: true,message: '请输入活动名称',trigger: 'blur'},{min: 3,max: 5,message: '长度在 3 到 5 个字符',trigger: 'blur'}],body: [{required: true,message: '请输入活动名称',trigger: 'blur'}]}};},methods: {handleSizeChange(rows) {this.formInline.page = 1;this.formInline.rows = rows;this.search();},handleCurrentChange(page) {this.formInline.page = page;this.search();},doSearch(params) {let url = this.axios.urls.SYSTEM_ARTICLE_LIST;// let url = 'http://localhost:8080/T216_SSH/vue/userAction_login.action';this.axios.post(url, params).then((response) => {console.log(response);this.listData = response.data.result;this.total = response.data.pageBean.total;}).catch(function(error) {console.log(error);});},search() {this.doSearch(this.formInline);},closeDialog() {this.clearData();},handleEdit() {this.editFormVisible = true;this.title = '新增窗体';},doEdit(index, row) {this.editForm.id = row.id;this.editForm.title = row.title;this.editForm.body = row.bodythis.editFormVisible = true;this.title = '编辑窗体';},clearData() {this.title = '';this.editForm.id = 0;this.editForm.title = '';this.editForm.body = '';this.editFormVisible = false;},deleteUser(index, row) {let url = this.axios.urls.SYSTEM_ARTICLE_DEL;this.axios.post(url, {id:row.id}).then((response) => {console.log(response);this.clearData();this.search();}).catch(function(error) {console.log(error);});},submitForm(formName) {this.$refs[formName].validate((valid) => {if (valid) {let url;if (this.editForm.id == 0) {url = this.axios.urls.SYSTEM_ARTICLE_ADD;} else {url = this.axios.urls.SYSTEM_ARTICLE_EDIT;}// let url = 'http://localhost:8080/T216_SSH/vue/userAction_login.action';this.axios.post(url, this.editForm).then((response) => {console.log(response);this.clearData();this.search();}).catch(function(error) {console.log(error);});} else {console.log('error submit!!');return false;}});}},created() {this.doSearch({});}}
</script><style></style>

效果图
在这里插入图片描述

这篇关于spa项目之CUD+表单验证的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/215056

相关文章

SpringBoot项目中Redis存储Session对象序列化处理

《SpringBoot项目中Redis存储Session对象序列化处理》在SpringBoot项目中使用Redis存储Session时,对象的序列化和反序列化是关键步骤,下面我们就来讲讲如何在Spri... 目录一、为什么需要序列化处理二、Spring Boot 集成 Redis 存储 Session2.1

springboot+vue项目怎么解决跨域问题详解

《springboot+vue项目怎么解决跨域问题详解》:本文主要介绍springboot+vue项目怎么解决跨域问题的相关资料,包括前端代理、后端全局配置CORS、注解配置和Nginx反向代理,... 目录1. 前端代理(开发环境推荐)2. 后端全局配置 CORS(生产环境推荐)3. 后端注解配置(按接口

Vue 2 项目中配置 Tailwind CSS 和 Font Awesome 的最佳实践举例

《Vue2项目中配置TailwindCSS和FontAwesome的最佳实践举例》:本文主要介绍Vue2项目中配置TailwindCSS和FontAwesome的最... 目录vue 2 项目中配置 Tailwind css 和 Font Awesome 的最佳实践一、Tailwind CSS 配置1. 安

Spring Boot项目打包和运行的操作方法

《SpringBoot项目打包和运行的操作方法》SpringBoot应用内嵌了Web服务器,所以基于SpringBoot开发的web应用也可以独立运行,无须部署到其他Web服务器中,下面以打包dem... 目录一、打包为JAR包并运行1.打包为可执行的 JAR 包2.运行 JAR 包二、打包为WAR包并运行

Nginx部署React项目时重定向循环问题的解决方案

《Nginx部署React项目时重定向循环问题的解决方案》Nginx在处理React项目请求时出现重定向循环,通常是由于`try_files`配置错误或`root`路径配置不当导致的,本文给大家详细介... 目录问题原因1. try_files 配置错误2. root 路径错误解决方法1. 检查 try_f

解决Maven项目报错:failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.13.0的问题

《解决Maven项目报错:failedtoexecutegoalorg.apache.maven.plugins:maven-compiler-plugin:3.13.0的问题》这篇文章主要介... 目录Maven项目报错:failed to execute goal org.apache.maven.pl

在.NET平台使用C#为PDF添加各种类型的表单域的方法

《在.NET平台使用C#为PDF添加各种类型的表单域的方法》在日常办公系统开发中,涉及PDF处理相关的开发时,生成可填写的PDF表单是一种常见需求,与静态PDF不同,带有**表单域的文档支持用户直接在... 目录引言使用 PdfTextBoxField 添加文本输入域使用 PdfComboBoxField

Python开发文字版随机事件游戏的项目实例

《Python开发文字版随机事件游戏的项目实例》随机事件游戏是一种通过生成不可预测的事件来增强游戏体验的类型,在这篇博文中,我们将使用Python开发一款文字版随机事件游戏,通过这个项目,读者不仅能够... 目录项目概述2.1 游戏概念2.2 游戏特色2.3 目标玩家群体技术选择与环境准备3.1 开发环境3

SpringBoot项目中报错The field screenShot exceeds its maximum permitted size of 1048576 bytes.的问题及解决

《SpringBoot项目中报错ThefieldscreenShotexceedsitsmaximumpermittedsizeof1048576bytes.的问题及解决》这篇文章... 目录项目场景问题描述原因分析解决方案总结项目场景javascript提示:项目相关背景:项目场景:基于Spring

解决Maven项目idea找不到本地仓库jar包问题以及使用mvn install:install-file

《解决Maven项目idea找不到本地仓库jar包问题以及使用mvninstall:install-file》:本文主要介绍解决Maven项目idea找不到本地仓库jar包问题以及使用mvnin... 目录Maven项目idea找不到本地仓库jar包以及使用mvn install:install-file基