表单默认空白,无数据时赋默认值,新增时赋默认值

文章描述了一个Vue.js应用中处理表单数据的方法,包括初始化和新增时设置默认值,使用Map避免重复添加,以及根据ID判断是更新还是新增数据的逻辑。同时,文章展示了如何通过接口进行数据保存,并处理保存后的反馈,如成功或失败的消息提示。此外,还包括了删除操作和状态管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一.首先定义一个赋默认值的方法,然后初始化时调用,新增时调用

二.新增与更新,入参一致,所以可以共用一个接口,一个方法,根据是否有id判断

三.生成表单尽量用map不用push,因为push可能会出现重复添加的情况,但map每次会生成新的数组不用考虑这种情况

  <el-button type="primary" @click="switcSave()">保存</el-button>
    <el-button type="primary" @click="doPdf">生成报表</el-button>
    <el-button type="primary" @click="switcSave('add')">新增</el-button>
    <el-button type="danger" :disabled="this.flag" plain @click="doDelete">删除</el-button>
    data(){
    flag:false
    }
// 新增保存
export function switchFormData(data) {
  if(data.id){
    return request({
      url: "/api/TaskFormData/Update",
      method: "put",
      data
    });
  }else{
    return request({
      url: "/api/TaskFormData/Insert",
      method: "post",
      data
    });
  }
}
 getFrom() {
      this.form = {
        id: "",
        remark01:"",
        remark02: "",
        remark03:"",
        remark07: "",
        remark08: "",
        remark09: "",
        remark10: "",
        remark11: "",
        remark12: "",
        remark13: "",
        remark14: "",
        remark16: moment(new Date()).format('YYYY-MM-DD'),
        user_remark10: ""
      }
    },
    async init() {
      const { data } = await GetFormData({ taskId: this.conditions.remark01, formName: this.formTableName })
      // 如果没有数据就清空form,重新加载
      if (data.data.length == 0) {
        this.getFrom()
        this.formList = []
        this.flag = true
        this.formList.push(this.form)
        return false
      }
      if (data.statusCode == 200) {
        this.flag = false
        // 获取到当前的表单
        data.data.forEach((el, index) => {
          if (this.currentIndex == index) {
            this.form = el.value
            this.form.id = el.id
          }
        })
           // 生成新的列表
          const values = data.data.map(item => item.value)
          this.formList = values
          console.log(  this.form ,"  this.form ");
          console.log("formList",this.formList);
      }

    },
    async switcSave(val) {
     
      if(val=='add'){
        this.getFrom()
      }
      this.form.remark01 = this.conditions.remark01;
      this.form.remark03 = this.conditions.remark03;
      let isFullBoxRemark = this.isFullBox == 0 ? "空箱" : "满箱"
      let formData = {
        id: this.form.id,
        taskId: this.taskId,
        formName: this.formTableName,
        formNameRemark: `${this.conditions.remark03}_${isFullBoxRemark}_` + `测量_关键参数设置表`,
        key: {
          isFullBox: this.isFullBox,
          XYZDirection: this.conditions.remark03,
          step: 'X'
        },
        value: this.form,
      };
      this.form.id?formData['updatedUserId']=this.loginUserId:formData['createdUserId']=this.loginUserId
      const{data}= await switchFormData(formData)
      if (data.statusCode == 200) {
          this.$message.success(this.form.id?'保存成功':' 新增成功');
          this.init()
        } else {
          this.$message.error(this.form.id?'保存失败':' 新增失败');
        }
    },
好的,下面是对如何使用 Thymeleaf 完成数据的页面展示的具体步骤及说明: --- ### **1. 创建 Spring Boot 项目并引入 Thymeleaf** 通过 [Spring Initializr](https://start.spring.io/) 创建一个新的 Spring Boot 项目。 在选择依赖项,添以下两个关键依赖: - `Spring Web`:用于支持 Web 开发。 - `Thymeleaf`:作为模板引擎。 下载生成的项目解压后导入到 IDE 中(如 IntelliJ IDEA 或 Eclipse),完成后即可开始下一步操作。 --- ### **2. 配置 application.properties 文件** 打开项目的根路径下的 `src/main/resources/application.properties` 文件,在其中可以设置一些关于 Thymeleaf 的基本配置选项,例如是否启用缓存等。以下是常见的配置示例: ```properties # 禁用模板缓存以便实查看修改后的效果(开发环境下推荐) spring.thymeleaf.cache=false # 设置编码格式,默认UTF-8 spring.thymeleaf.encoding=UTF-8 # 模板前缀和后缀(默认值无需修改) spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix=.html ``` > 注释掉不需要的内容或将它们保持为默认值也可以正常运行程序。 --- ### **3. 创建控制器 LoginController 类** 接下来我们将在新建包名间内定义一个控制层组件类 (`@Controller`) 来负责处理请求以及向视图传递模型数据。具体做法如下: #### (a)创建包结构 确保按照题目要求建立了正确的包名称路径: `com.itheima.controller` #### (b)编写 Controller 实现代码样例 ```java package com.itheima.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; @Controller // 标识这是一个控制器组件 public class LoginController { @GetMapping("/login") // 映射 /login 请求地址 public String login(Model model) { // 向模型中添属性 "message" 和对应的字符串内容 model.addAttribute("message", "欢迎来到登录页面!"); return "login"; // 返回视图名为“login”,对应 templates/login.html 文件 } } ``` 在这里实现了简单的 GET 接口 `/login` 并将消息信息放入 Model 对象里供后续渲染 HTML 使用。 --- ### **4. 编写 Thymeleaf 页面 (login.html)** 进入 `resources/templates/` 目录下新增名为 `login.html` 的静态资源文件,并编辑其内容如下所示: ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"/> <title>登录界面</title> </head> <body> <h1 th:text="${message}"></h1> <!-- 动态插入从后台传来的 message 值 --> <form action="#" method="post"> <!-- 示例表单 --> 用户名:<input type="text" name="username"/><br/> 密码:<input type="password" name="password"/><br/><br/> <button type="submit">提交</button> </form> </body> </html> ``` 上述代码片段展示了如何利用 `${}` 表达式语法获取由服务器端提供的变量值,并将其嵌入 HTML 元素之中显示出来。 --- ### **5. 测试功能** 启动应用程序之后访问浏览器输入 URL 地址 http://localhost:8080/login 即可看到最终呈现出来的结果——包含自动生成文本 “**欢迎来到登录页面!**” 及其他空白字段等待填充填写的部分。 如果一切顺利的话,则完成了整个基于 Thymeleaf 数据绑定与前端交互演示的过程! --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值