快速实现前后端表单交互(minui)

(1)使用minui快速生成表单

(2)修改样式,将生成的html文件发送给后端

(3)前后端交互(后端如何处理不太清楚)

表单初始化:新增+修改(包括查看)

根据是否有id来选择发送什么请求:getCheckSheetHtml请求表示获取最原始的无数据的表单,getCheckContent表示获取有数据的表单。

javascript 复制代码
openInit() {
      //重置表单
      this.onResetForm();
      if (this.id) {
        if (this.shiftFlag == "look") {
          this.title = "查看早到岗检查单";
          //表单设为不可编辑
          document
            .getElementById("checkIframe")
            .contentWindow.document.getElementsByTagName(
              "input"
            ).disabled = true;
          this.disabled = true;
        } else {
          this.title = "修改早到岗检查单";
          this.disabled = false;
        }
      } else {
        this.title = "新增早到岗检查单";
        this.disabled = false;
      }
      if (this.id) {
        var param = {};
        param.id = this.id;
        param.checkType = "earlyArr";
        param.groupId = this.groupId;
        param.seatId = this.seatId;
        getCheckContent(param).then((res) => {
          var data = res.data.data;
          this.recordDate = data.recordDate;
          var checkContent = JSON.parse(data.checkContent);
          var checkboxValue = checkContent.checkboxValue;
          var checkSheet = checkContent.checkSheet;
          document
            .getElementById("checkIframe")
            .contentWindow.document.write(checkSheet);
          if (checkboxValue) {
            var doc =
              document.getElementById("checkIframe").contentWindow.document;
            var checkboxDoc = doc.getElementsByName("checkbox");
            for (var j = 0; j < checkboxDoc.length; j++) {
              var obj = checkboxDoc[j];
              if (obj.type == "checkbox") {
                obj.checked = checkboxValue[j];
              }
            }
          }
        });
      } else {
        getCheckSheetHtml({ id: "twr_early" }).then((res) => {
          document
            .getElementById("checkIframe")
            .contentWindow.document.getElementsByTagName("html")[0].innerHTML =
            res.data.data;
        });
      }
    },

    //重置表单
    onResetForm() {
      this.iframeSrc = "";
      this.recordDate = new Date();
      document.getElementById(
        "checkIframe"
      ).contentWindow.document.body.innerHTML = "";
    },

getCheckContent请求能获取数据(是个json),所以我们要把它渲染到页面。

新增的时候需要传入json(这就是修改查看接口能获取的json数据)

javascript 复制代码
 //确定
    onOK() {
      var vdata = {};
      //获取检查单checkbox值
      var doc = document.getElementById("checkIframe").contentWindow.document;
      var checkboxDoc = doc.getElementsByName("checkbox");
      if (checkboxDoc.length > 0) {
        var checkboxArray = []; //初始化空数组,用来存放checkbox值
        for (var i = 0; i < checkboxDoc.length; i++) {
          var obj = checkboxDoc[i];
          if (obj.type == "checkbox") {
            checkboxArray.push(obj.checked);
          }
        }
      }
      vdata.checkboxValue = checkboxArray;
      var iframe = this.$refs.checkIframe;
      vdata.checkSheet = iframe.contentDocument.documentElement.innerHTML;
      // 将勾选数组和html(包含了input这些输入的)存入到一个大的json里面
      vdata.checkContent = JSON.stringify(vdata);
      vdata.userId = this.userId;
      vdata.groupId = this.groupId;
      vdata.seatId = this.seatId;
      vdata.recordDate = this.recordDate;
      vdata.checkType = "earlyArr";

      if (this.id) {
        vdata.id = this.id;
        updateCheckContent(vdata).then((res) => {
          if (res.data.data) {
            this.$message({
              type: "success",
              message: "修改早到岗检查单成功",
            });
            this.$emit("handleDialog", false);
          } else {
            this.$message({
              type: "error",
              message: "修改早到岗检查单失败",
            });
          }
        });
      } else {
        addCheckContent(vdata).then((res) => {
          if (res.data.data) {
            this.$message({
              type: "success",
              message: "新增早到岗检查单成功",
            });
            this.$emit("handleDialog", false);
          } else {
            this.$message({
              type: "error",
              message: "当日早到岗检查单已存在",
            });
          }
        });
      }
    },
相关推荐
前端 贾公子14 分钟前
从入门到实践:前端 Monorepo 工程化实战(4)
前端
菩提小狗17 分钟前
Sqlmap双击运行脚本,双击直接打开。
前端·笔记·安全·web安全
前端工作日常28 分钟前
我学习到的AG-UI的概念
前端
韩师傅33 分钟前
前端开发消亡史:AI也无法掩盖没有设计创造力的真相
前端·人工智能·后端
XiaoYu20021 小时前
第12章 支付宝SDK
前端
双向331 小时前
RAG的下一站:检索增强生成如何重塑企业知识中枢?
前端
拖拉斯旋风1 小时前
从零开始:使用 Ollama 在本地部署开源大模型并集成到 React 应用
前端·javascript·ollama
asing1 小时前
🤯 为什么我的收银台在鸿蒙系统“第一次返回”死活拦不住?一次差点背锅的排查实录
前端·harmonyos
德育处主任1 小时前
『NAS』在群晖部署图片压缩工具-Squoosh
前端·javascript·docker
Hao_Harrision1 小时前
50天50个小项目 (React19 + Tailwindcss V4) ✨| ThreeDBackgroundBoxes(3D背景盒子组件)
前端·3d·typescript·react·tailwindcss·vite7