快速实现前后端表单交互(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: "当日早到岗检查单已存在",
            });
          }
        });
      }
    },
相关推荐
橙子家8 小时前
浏览器缓存之【基础键值存储】:Local storage 和 Session storage
前端
星星在线10 小时前
MusicFree:一个「All in One」的个人音乐服务器,让听歌回归简单
前端·后端
IT_陈寒11 小时前
Redis的SETNX并发问题让我加了三天班
前端·人工智能·后端
demo007x12 小时前
Docling 文档转换以及技术架构分析
前端·后端·程序员
京东云开发者12 小时前
京东市民服务又“上新”!这次是黑龙江“龙易办”
前端
袋鱼不重13 小时前
我的神奇同事,AI 用多了居然写了个 Open In Codex
前端·后端·ai编程
Fireworks13 小时前
深入vue3源码解读 -- 1、响应式的基础概念
前端
程序员黑豆13 小时前
JDK 下载安装与配置详细教程
java·前端·ai编程
hunterandroid14 小时前
文件存储:内部存储与外部存储
前端
NorBugs14 小时前
飞机大战 Low 版 (Made in AI)
前端