• Vue3集成json-editor-vue3


    安装依赖

    npm install json-editor-vue3 --save
    
    • 1

    引入

    main.js

    import "jsoneditor";
    
    • 1

    具体模块

    import JsonEditorVue from 'json-editor-vue3';
    
    • 1

    代码实现

    <json-editor-vue ref="jsonEditor" class="editor" v-model="state.addFormField.powerTypes" @keyup="jsonValidate" />
    
    • 1

    完整代码

    <template>
      <div>
        <div class="card-wrap" style="padding: 0 12px;">
          <el-form :model="state.queryParams" ref="queryRef" :inline="true" class="form-search-wrap">
            <el-form-item label="code">
              <el-input v-model="state.queryParams.code" clearable style="width: 240px" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="getList()">Search</el-button>
              <el-button icon="Refresh" @click="resetSearchForm">Reset</el-button>
            </el-form-item>
          </el-form>
        </div>
        <div class="card-wrap" style="padding-bottom: 100px;">
          <div class="operation-wrap">
            <el-button type="primary" @click="handleEdit(null, 1)">Add</el-button>
            <el-button type="danger" @click="batchRemove" >Remove</el-button>
          </div>
          <el-table :data="state.tableData"
                    v-loading="state.loading"
                    @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" />
            <el-table-column prop="code" label="Code"></el-table-column>
            <el-table-column prop="value" label="Value"></el-table-column>
            <el-table-column prop="i18n" label="I18n"></el-table-column>
            <el-table-column label="Operation" align="center">
              <template #default="scope">
                <el-button type="primary" icon="Edit" @click="handleEdit(scope.row, 0)" circle />
                <el-button type="danger" icon="Delete" @click="remove(scope.row.id, 0)" circle />
              </template>
            </el-table-column>
          </el-table>
          <div class="pagination">
            <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="state.queryParams.page"
                :page-sizes="[10, 20, 50, 100]"
                :page-size="state.queryParams.size"
                layout="total, sizes, prev, pager, next, jumper"
                :total="state.queryParams.total">
            </el-pagination>
          </div>
        </div>
        <el-dialog :title="state.type?'add':'update'" v-model="state.dialogVisible" width="30%"  destroy-on-close @close='resetForm'>
          <el-form ref="addForm" :rules="state.rules" :model="state.addFormField" label-width="100px">
            <el-form-item label="code" prop="code">
              <el-input v-model="state.addFormField.code" style="width: 80%;" ></el-input>
            </el-form-item>
            <el-form-item label="value" prop="value">
              <el-input v-model="state.addFormField.value" style="width: 80%;" ></el-input>
            </el-form-item>
            <el-form-item label="i18n" prop="i18n">
              <el-input v-model="state.addFormField.i18n" style="width: 80%;" ></el-input>
            </el-form-item>
            <el-form-item label="numberOfEngine" prop="numberOfEngine">
              <el-input v-model="state.addFormField.numberOfEngine" style="width: 80%;" ></el-input>
            </el-form-item>
            <el-form-item label="powerTypes" prop="powerTypes">
              <json-editor-vue ref="jsonEditor" class="editor" v-model="state.addFormField.powerTypes" @keyup="jsonValidate" />
            </el-form-item>
            <el-form-item label="siebelCode" prop="siebelCode">
              <el-input v-model="state.addFormField.siebelCode" style="width: 80%;" ></el-input>
            </el-form-item>
          </el-form>
          <template #footer>
            <span class="dialog-footer">
              <el-button @click="state.dialogVisible = false">Cancel</el-button>
              <el-button type="primary" @click="add">Confirm</el-button>
            </span>
          </template>
        </el-dialog>
      </div>
    </template>
    
    <script setup>
    import { reactive, ref } from "vue";
    import { ElMessage, ElMessageBox } from "element-plus";
    import * as api from '@/api/dic/common';
    import JsonEditorVue from 'json-editor-vue3';
    
    const addForm = ref();
    const jsonEditor = ref();
    const moduleUrl = "boatType";
    const state = reactive({
      multipleSelection: [],
      dialogVisible: false,
      jsonEditFlag: true,
      num: 1,
      loading: false,
      type: "add",
      tableData: [],
      addFormField: {
        id: "",
        code: "",
        value: "",
        i18n: "",
        numberOfEngine: "",
        powerTypes: "",
        siebelCode: "",
      },
      queryParams: {
        code: undefined,
        page: 1,
        size: 10,
        total: 0,
      },
      rules: {
        code: [
          { required: "true", message: "code is required", trigger: ["change"] },
        ],
        value: [
          { required: "true", message: "value is required", trigger: ["change"] },
        ],
        i18n: [
          { required: "true", message: "i18n is required", trigger: ["change"] },
        ],
      },
    });
    
    async function jsonValidate () {
      if (jsonEditor.value) {
        const res = await jsonEditor.value.editor.validate();
        if(res.length != 0){
          state.jsonEditFlag = false;
        }else {
          state.jsonEditFlag = true;
        }
      }
    }
    
    
    function handleSelectionChange(val) {
      state.multipleSelection = val
    }
    
    function commonRemove(idList) {
      ElMessageBox.confirm('Are you sure to remove this data?')
          .then(() => {
            api
                .remove({idList: idList}, moduleUrl)
                .then((res) => {
                  ElMessage.success("success");
                })
                .finally(() => {
                  state.loading = false;
                });
            setTimeout(() => {
              getList();
            }, 300);
          })
    }
    
    function remove(id){
      let idList = [];
      idList.push(id)
      commonRemove(idList);
    }
    
    function batchRemove() {
      if(state.multipleSelection == undefined || state.multipleSelection.length <= 0){
        ElMessage.warning("Please select checkbox");
        return;
      }
      let idList = [];
      state.multipleSelection.forEach((value, index) => {
        idList.push(value.id)
      });
      commonRemove(idList);
    }
    
    function handleEdit(row, type){
      if(row){
        state.addFormField = JSON.parse(JSON.stringify(row));
      }
      if (type) {
        state.type = true;
      } else{
        state.type = false;
      }
      state.dialogVisible = true;
    }
    
    function resetForm(){
      state.addFormField = {
        id: undefined,
        code: undefined,
        value: undefined,
        i18n: undefined,
      }
    }
    
    function resetSearchForm(){
      state.queryParams.code = undefined;
    }
    
    function add(){
      addForm.value.validate((valid) => {
        if(!state.jsonEditFlag){
          ElMessage.error("JSON input wrong format");
          return ;
        }
        if (valid) {
          let id = state.addFormField.id;
          if (id) {
            api
                .update(state.addFormField, moduleUrl)
                .then((res) => {
                  ElMessage.success("success");
                })
                .finally(() => {
                  state.loading = false;
                });
          }else{
            api
                .add(state.addFormField, moduleUrl)
                .then((res) => {
                  ElMessage.success("success");
                })
                .finally(() => {
                  state.loading = false;
                });
          }
          resetForm();
          state.dialogVisible = false;
          setTimeout(() => {
            getList();
          }, 200);
        } else {
          return false;
        }
      });
    }
    
    function getList(){
      state.loading = true;
      let data = {};
      if(state.queryParams){
        data = JSON.stringify(state.queryParams).replace(/:/g, '=').replace(/,/g, '&').replace(/{/g, '?').replace(/}/g, '').replace(/"/g, '');
      }
      api
          .getList(data, moduleUrl)
          .then((res) => {
            state.tableData = res.items;
            state.queryParams.page = res.page;
            state.queryParams.size = res.size;
            state.queryParams.total = res.total;
          })
          .finally(() => {
            state.loading = false;
          });
    }
    
    function handleSizeChange(val){
      state.queryParams.size = val;
      getList();
    }
    
    function handleCurrentChange(val){
      state.queryParams.page = val;
      getList();
    }
    
    getList();
    </script>
    <style scoped >
    /deep/ .el-dialog{
      width: 50%;
    }
    /deep/ .el-form-item--default{
      margin-left: 30px;
    }
    .pagination {
      background: #fff;
      padding: 20px 10px 0;
      display: flex;
      justify-content: flex-end;
      box-sizing: border-box;
    }
    .card-wrap{
      padding: 15px 20px 20px 20px;
      border-radius: 4px;
      border: 1px solid #e6ebf5;
      background-color: #fff;
      overflow: hidden;
      color: #303133;
      -webkit-transition: .3s;
      transition: .3s;
      box-shadow: 0 2px 12px 0 rgb(0 0 0 / 10%);
      margin: 10px;
    }
    .operation-wrap{
      margin: 0 0 16px 0;
    }
    .form-search-wrap{
      margin: 10px 0 0 12px;
    }
    </style>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
  • 相关阅读:
    openEuler 22.03 制作openssh9.5p1rpm包
    6-9 二叉树的遍历 (二叉树的前历、后序、中序、层序遍历)
    解决vue element - ui 弹窗打开表单自动校验问题
    Java 复习笔记 - 集合进阶篇:List集合
    LinkedList(4):多线程LinkedList 不安全情况
    机器学习:李航-统计学习方法-代码实现
    C# 字符串string转换为double、float、int数据类型变量的方法
    【牛客刷题-算法】NC7 买卖股票的最好时机(一)
    PG SQL 问题:Character with value 0x0a must be escaped
    Java项目:房屋租赁管理系统(java+SpringBoot+Vue+Maven+Mysql)
  • 原文地址:https://blog.csdn.net/SpringHASh/article/details/134390794