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

本文主要介绍了在SPA(单页应用程序)项目中进行CRUD操作及表单验证的步骤。首先,详细阐述了如何使用el-form组件创建表单,以及通过点击新增/编辑按钮弹出表单窗口并设置验证规则。接着,讨论了增删改功能的实现,包括确保编辑窗体正常弹出、新增和修改文章的操作,以及删除功能的完整流程。整个过程结合具体的代码片段进行了说明。
摘要由CSDN通过智能技术生成

目录

一、表单验证

1.出现表单组件 el-form

2.通过点击 新增/编辑将表单对应窗口弹出

​编辑3.给表单设置规则 rules

Articles.vue

​编辑4.当表单提交时要校验规则

二、增删改

1、保障编辑窗体能够正常弹出

2、新增与修改

 3、删除


一、表单验证

1.出现表单组件 el-form

<!-- 编辑界面 -->
<el-dialog :title="title" :visible.sync="editFormVisible" width="30%" @click="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>

2.通过点击 新增/编辑将表单对应窗口弹出

默认false

点击添加改为true

handleEdit() {
      // 展示新增文章的表单
      this.editFormVisible = true;
    },

效果


3.给表单设置规则 rules

Articles.vue

<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" highlight-current-row style="width: 100%;">
      <el-table-column align="center" type="selection" min-width="60">
      </el-table-column>
      <el-table-column sortable prop="id" label="文章ID" min-width="300">
      </el-table-column>
      <el-table-column sortable prop="title" label="文章标题" min-width="300">
      </el-table-column>
      <el-table-column sortable prop="body" label="文章内容" min-width="300">
      </el-table-column>
      <el-table-column align="center" label="操作" min-width="300">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(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="formInline.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 {
  name: 'Articles',
  data() {
    return {
      title: '',
      editFormVisible: false,
      editForm: {
        title: '',
        body: ''
      
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值