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

相关文章

Spring Security中用户名和密码的验证完整流程

《SpringSecurity中用户名和密码的验证完整流程》本文给大家介绍SpringSecurity中用户名和密码的验证完整流程,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定... 首先创建了一个UsernamePasswordAuthenticationTChina编程oken对象,这是S

如何在Spring Boot项目中集成MQTT协议

《如何在SpringBoot项目中集成MQTT协议》本文介绍在SpringBoot中集成MQTT的步骤,包括安装Broker、添加EclipsePaho依赖、配置连接参数、实现消息发布订阅、测试接口... 目录1. 准备工作2. 引入依赖3. 配置MQTT连接4. 创建MQTT配置类5. 实现消息发布与订阅

springboot项目打jar制作成镜像并指定配置文件位置方式

《springboot项目打jar制作成镜像并指定配置文件位置方式》:本文主要介绍springboot项目打jar制作成镜像并指定配置文件位置方式,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录一、上传jar到服务器二、编写dockerfile三、新建对应配置文件所存放的数据卷目录四、将配置文

怎么用idea创建一个SpringBoot项目

《怎么用idea创建一个SpringBoot项目》本文介绍了在IDEA中创建SpringBoot项目的步骤,包括环境准备(JDK1.8+、Maven3.2.5+)、使用SpringInitializr... 目录如何在idea中创建一个SpringBoot项目环境准备1.1打开IDEA,点击New新建一个项

springboot项目中整合高德地图的实践

《springboot项目中整合高德地图的实践》:本文主要介绍springboot项目中整合高德地图的实践,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一:高德开放平台的使用二:创建数据库(我是用的是mysql)三:Springboot所需的依赖(根据你的需求再

一文详解如何在idea中快速搭建一个Spring Boot项目

《一文详解如何在idea中快速搭建一个SpringBoot项目》IntelliJIDEA作为Java开发者的‌首选IDE‌,深度集成SpringBoot支持,可一键生成项目骨架、智能配置依赖,这篇文... 目录前言1、创建项目名称2、勾选需要的依赖3、在setting中检查maven4、编写数据源5、开启热

SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志

《SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志》在SpringBoot项目中,使用logback-spring.xml配置屏蔽特定路径的日志有两种常用方式,文中的... 目录方案一:基础配置(直接关闭目标路径日志)方案二:结合 Spring Profile 按环境屏蔽关

MySQL版本问题导致项目无法启动问题的解决方案

《MySQL版本问题导致项目无法启动问题的解决方案》本文记录了一次因MySQL版本不一致导致项目启动失败的经历,详细解析了连接错误的原因,并提供了两种解决方案:调整连接字符串禁用SSL或统一MySQL... 目录本地项目启动报错报错原因:解决方案第一个:第二种:容器启动mysql的坑两种修改时区的方法:本地

springboot项目中使用JOSN解析库的方法

《springboot项目中使用JOSN解析库的方法》JSON,全程是JavaScriptObjectNotation,是一种轻量级的数据交换格式,本文给大家介绍springboot项目中使用JOSN... 目录一、jsON解析简介二、Spring Boot项目中使用JSON解析1、pom.XML文件引入依

使用vscode搭建pywebview集成vue项目实践

《使用vscode搭建pywebview集成vue项目实践》:本文主要介绍使用vscode搭建pywebview集成vue项目实践,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录环境准备项目源码下载项目说明调试与生成可执行文件核心代码说明总结本节我们使用pythonpywebv