当前位置: 首页 > news >正文

做一个多级动态表单,可以保存数据和回显数据

<template>

  <div class="two">

    <button class="save" @click="saveBtn">保存数据</button>

    <button class="sd" @click="showBtn">回显数据</button>

    <div class="all" @click="addAll()">+新增收信规则</div>

    <div class="allContent" v-for="(items,index) in allList" :key="items.id">

      <div class="other">

        <p>{{items.name}}</p>

        <input type="text" placeholder="新规则名称" v-model="items.inputTxt" />

        <button class="rowDel" @click="delAll(items,index)">-</button>

      </div>

      <div class="otherInput">

        <p>当邮件到达,满足以下条件时:</p>

        <div class="top" v-for="(task,idx) in items.itemsList" :key="task.id">

          <select v-model="task.type">

            <option

              v-for="options in typeList"

              :value="options.value"

              :key="options.id"

            >{{options.Text}}</option>

          </select>

          <select v-model="task.condition">

            <option :value="1">包含</option>

            <option :value="0">不包含</option>

          </select>

          <input type="text" class="inputS" v-model="task.inputS" />

          <button class="rowDelM" @click="delRow(task,items.itemsList,idx)">-</button>

          <button class="rowAdd" @click="addRow(task,items.itemsList,idx)">+</button>

        </div>

      </div>

    </div>

  </div>

</template>

<script>

export default {

  name: "ShengMingZQTwo",

  data() {

    return {

      typeList: [

        {

          id: 1,

          Text: "正文",

          value: "90"

        },

        {

          id: 2,

          Text: "发件人地址",

          value: "99"

        },

        {

          id: 3,

          Text: "邮件主题",

          value: "80"

        },

        {

          id: 4,

          Text: "发送时间",

          value: "82"

        },

        {

          id: 5,

          Text: "接收时间",

          value: "84"

        },

        {

          id: 6,

          Text: "附件名称",

          value: "86"

        }

      ],

      allList: [

      ]

    };

  },

  methods: {

    // 新增所有

    addAll() {

       let items= {

          id: 0,

          name: "规则名称1",

          inputTxt: "",

          itemsList: [

            {

              id:1,

              type: "90",

              condition: "1",

              inputS: ""

            },

             {

              id:2,

              type: "99",

              condition: "0",

              inputS: ""

            }

          ]

        };

        if(this.allList.length>0){

          items.id=this.allList.length; //key绑定

          items.name="规则名称"+(this.allList.length+1);

          this.allList.push(items);

        }else{

          this.allList=[items];

        }

    },

    // 删除所有

    delAll(item, index) {

      this.allList.splice(index,1);//删除自己

    },

    // 删除当前行

    delRow(item, itemsList, idx) {

      itemsList.splice(idx,1);

    },

    // 添加行

    addRow(item, itemsList, idx) {

      console.log(idx);

      itemsList.push({

              type: "99",

              condition: "0",

              inputS: "新增"

            })

    },

    //保存按钮

    saveBtn(){  

      // 1、localStorage和sessionStorage不能直接保存一个对象或数组

      //  sessionStorage.setItem("canshu",JSON.stringify(this.allList));

        // 2、vuex中缓存的数据保存需要JSON.parse(JSON.stringfy(数据));

         this.$store.commit("SET_CONDITION",JSON.parse(JSON.stringify(this.allList)));

      //  console.log(this.allList);

    },

    // 回显按钮

    showBtn(){

      // 1

      this.allList=this.$store.state.condition;

      console.log(this.$store.state.condition);

      // console.log(JSON.parse(localStorage.getItem("canshu")));

      // this.allList=JSON.parse(sessionStorage.getItem("canshu"))

    }

  }

};

</script>

<style scoped>

.all {

  width: 150px;

  height: 38px;

  line-height: 38px;

  color: #fff;

  background-color: blue;

  margin: 0 auto;

}

.rowDel,

.rowDelM {

  margin-left: 20px;

  color: red;

}

select {

  width: 80px;

  height: 30px;

  margin-left: 10px;

}

.inputS {

  margin-left: 10px;

  height: 25px;

}

.rowAdd {

  margin-left: 10px;

}

.top {

  margin-bottom: 10px;

}

</style>

在store/index.js中    :(vuex在使用前需要npm依赖、配置)

// store/index.js

import Vue from 'vue';

import Vuex from 'vuex';

Vue.use(Vuex); // 必须通过 Vue.use() 安装插件  

export default new Vuex.Store({

  state: {

    user:null,//初始状态为未登录

    condition:[]

  },

  mutations: {

    SET_USER(state,userData){

       state.user=userData

    },

    SET_CONDITION(state,allListData){

      state.condition=allListData;

    }

  },

  actions: {

 },

  getters: 

  },

  modules: {

    // index: index, // 模块名必须与 mapState 中的命名空间一致

  },

});


注:不管是用localStorage、或者sessionStorage或者vuex都可以存数据,就是在编辑页面回显数据需要对数组或者对象需要处理(JSON.stringfy()、JSON.parse())


 


文章转载自:

http://pXC1KDaa.pbgnx.cn
http://WmbxWapQ.pbgnx.cn
http://E19302JS.pbgnx.cn
http://HctLblnP.pbgnx.cn
http://wRLIEO0d.pbgnx.cn
http://KwbJa8Wc.pbgnx.cn
http://Q4J07fo2.pbgnx.cn
http://nFJubpkx.pbgnx.cn
http://XBBllS1p.pbgnx.cn
http://APb3pAAk.pbgnx.cn
http://uG6GUIVV.pbgnx.cn
http://XCTxUk7W.pbgnx.cn
http://ssZIGOh1.pbgnx.cn
http://wC0iBCfJ.pbgnx.cn
http://azJZ9bvJ.pbgnx.cn
http://7WdUvfvq.pbgnx.cn
http://IDui8D5V.pbgnx.cn
http://lN5fln04.pbgnx.cn
http://aGF2CZ34.pbgnx.cn
http://Bn56sfLf.pbgnx.cn
http://rLJCLWTL.pbgnx.cn
http://5PsyTEJ5.pbgnx.cn
http://PBXQCfup.pbgnx.cn
http://WmkldEtm.pbgnx.cn
http://8zNhYMzZ.pbgnx.cn
http://nN0SybvK.pbgnx.cn
http://errmeYCK.pbgnx.cn
http://u57eQ8Hh.pbgnx.cn
http://FREtu1rP.pbgnx.cn
http://inEcKd6g.pbgnx.cn
http://www.dtcms.com/a/95242.html

相关文章:

  • 【论文分析】无人机轨迹规划,Fast-Planner:实时避障+全局最优的路径引导优化算法
  • Rust从入门到精通之进阶篇:12.高级类型系统
  • ubuntu虚拟机的磁盘扩容,虚拟机的克隆
  • 将ZABBIX结合AI实现自动化运维
  • <数据集>轨道异物识别数据集<目标检测>
  • 操作系统高频(四)linux基础
  • nginx-rtmp-module之ngx_rtmp_live_module.c代码详解
  • 前端显示no data(没有数据,一片空白)
  • ComfyUi教程之阿里的万象2.1视频模型
  • OGG故障指南:OGG-00446 Checkpoint table does not exist
  • 深度解析Spring Boot可执行JAR的构建与启动机制
  • Go 语言规范学习(2)
  • 【商城实战(74)】数据采集与整理,夯实电商运营基石
  • C++:类和对象(二)
  • 如何快速解决django报错:cx_Oracle.DatabaseError: ORA-00942: table or view does not exist
  • 智能运维时代的网络拓扑管理:乐维监控的架构可视化实践
  • 科技快讯 | 谷歌发布新一代推理模型;我国成功发射天链二号04星;Manus:将举行线下活动 正努力让更多人用上Manus
  • 基于javaweb的SpringBoot房屋出租系统设计与实现(源码+文档+部署讲解)
  • Android 媒体(音乐)播放通知栏
  • 测谎仪策略思路
  • yolo系列算法最新进展
  • Spring Security 全面指南:从基础到高级实践
  • Ubuntu服务器挂载时遇到文件系统错误怎么办
  • 03 相机标定图像采集
  • 电脑卡怎么办?提升电脑流畅的方法
  • 资本运营:基于Python实现的资本运作模拟
  • 【Portainer】Docker可视化组件安装
  • milvus-use教程 python
  • ​双目立体视觉的3D重建全流程
  • 深入解析 C# 开闭原则(OCP):设计可扩展的系统