当前位置:   article > 正文

【Vue实用功能】elementUI 自定义表单模板组件_vue表单样式模板

vue表单样式模板

elementUI 实现一个自定义的表单模板组件
注:该功能基于elementUI
背景:在项目开发中,我们会遇到这种需求,在管理后台添加自定义表单,在指定的页面使用定义好的表单
在这里插入图片描述
直接上代码:

<template>
  <div>
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <div class="addBox">
        <el-form :model="form" label-width="90px" ref="form" :rules="rules">
          <el-form-item label="选项名称" lebel-width="80" prop="label">
            <el-input placeholder="请输入自定义选项名称" maxlength="20" clearable v-model="form.label" @input="change($event)">
            </el-input>
          </el-form-item>
          <el-form-item label="选项提示" lebel-width="80" prop="tips">
            <el-input @input="change($event)" placeholder="请输入提示内容" maxlength="20" clearable v-model="form.tips">
            </el-input>
          </el-form-item>
          <el-form-item label="选项类型" prop="value">
            <el-cascader clearable ref="cascader" v-model="value" :options="options" @change="handleChange">
            </el-cascader>
          </el-form-item>
          <el-form-item v-if="radioCount" label="最多选几项">
            <el-input-number v-model="form.radioCount" controls-position="right" :min="1"></el-input-number>
            <!-- <el-input type="number" v-model="form.radioCount"></el-input> -->
          </el-form-item>
          <el-form-item v-if="isRadio" v-for="(domain, index) in radioOptions" :key="domain.id"
            :label="'添加选项' + (index + 1)" prop="name">
            <el-row>
              <el-col :span="18">
                <el-input placeholder="请输入选择框选项" v-model="domain.name"></el-input>
              </el-col>
              <el-col :span="6">
                <el-button @click="addType" v-if="index == 0">新增</el-button>
              </el-col>
              <el-col :span="6">
                <el-button @click.prevent="removeType(domain)" v-if="index > 0">删除</el-button>
              </el-col>
            </el-row>
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
  export default {
    name: 'WorkspaceJsonProjectCopy',

    data() {
      return {
        title: '新增选项',
        isRadio: false,
        value: [],
        rules: {},
        open: true,
        radioCount: false,
        form: {
          radioOptions: [{}],
          label: ''
        },
        radioOptions: [{}],
        options: [{
            value: "input",
            label: "输入框",
            children: [{
                value: "phone",
                label: "手机号",
                children: [{
                    value: "0",
                    label: "非必填"
                  },
                  {
                    value: "1",
                    label: "必填"
                  }
                ]
              },
              {
                value: "idcard",
                label: "身份证号",
                children: [{
                    value: "0",
                    label: "非必填"
                  },
                  {
                    value: "1",
                    label: "必填"
                  }
                ]
              },
              {
                value: "link",
                label: "链接",
                children: [{
                    value: "0",
                    label: "非必填"
                  },
                  {
                    value: "1",
                    label: "必填"
                  }
                ]
              },
              {
                value: "email",
                label: "邮箱",
                children: [{
                    value: "0",
                    label: "非必填"
                  },
                  {
                    value: "1",
                    label: "必填"
                  }
                ]
              },
              {
                value: "other",
                label: "其他",
                children: [{
                    value: "0",
                    label: "非必填"
                  },
                  {
                    value: "1",
                    label: "必填"
                  }
                ]
              }
            ]
          },
          {
            value: "radio",
            label: "选择框",
            children: [{
                value: "danxuan",
                label: "单选",
                children: [{
                    value: "0",
                    label: "非必填"
                  },
                  {
                    value: "1",
                    label: "必填"
                  }
                ]
              },
              {
                value: "duoxuan",
                label: "多选",
                children: [{
                    value: "0",
                    label: "非必填"
                  },
                  {
                    value: "1",
                    label: "必填"
                  }
                ]
              }
            ]
          },
          {
            value: "image",
            label: "图片",
            children: [{
                value: "0",
                label: "非必填"
              },
              {
                value: "1",
                label: "必填"
              }
            ]
          },
          {
            value: "date",
            label: "日期—年月日",
            children: [{
                value: "0",
                label: "非必填"
              },
              {
                value: "1",
                label: "必填"
              }
            ]
          },
          {
            value: "datetime",
            label: "时间—时分秒",
            children: [{
                value: "0",
                label: "非必填"
              },
              {
                value: "1",
                label: "必填"
              }
            ]
          },
        ],
      };
    },

    mounted() {

    },

    methods: {
      change() {
        this.$forceUpdate();
      },
      // 弹窗选择选项类型
      handleChange(value) {
        this.value = value
        if (value[0] && value[0] == 'radio' && value[1] == 'danxuan') {
          this.radioOptions = [{}]
          this.isRadio = true
          this.radioCount = false
        }
        if (value[0] && value[0] == 'radio' && value[1] == 'duoxuan') {
          this.radioOptions = [{}]
          this.isRadio = true
          this.radioCount = true
        }
        if (value[0] && value[0] != 'radio') {

          this.radioOptions = [{}]
          this.isRadio = false
          this.radioCount = false
        }
      },
      // 重置弹窗
      reset() {
        this.form = {}
        this.value = []
        this.radioOptions = [{}]
        this.isRadio = false
        this.radioCount = false,
          this.isEdit = false
        const _cascader = this.$refs.cascader
        if (_cascader) {
          _cascader.$refs.panel.checkedValue = [];
          _cascader.$refs.panel.activePath = [];
          _cascader.$refs.panel.syncActivePath()
        }
        this.resetForm("form");
      },
      // 取消弹窗
      cancel() {
        this.open = false;
        this.form = {}
        this.reset()
      },
      // 新增添加选择框选项
      addType() {
        this.radioOptions.push({})
      },
      // 删除新增的选择框选项
      removeType(item) {
        var index = this.radioOptions.indexOf(item);
        if (index !== -1) {
          this.radioOptions.splice(index, 1);
        }
      },
      // 获取创建时间
      getDate() {
        var date = new Date()
        var Y = date.getFullYear() + '-'
        var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
        var D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' '
        var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':'
        var m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':'
        var s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds())
        return Y + M + D + h + m + s
      },
      submitForm: function () {
        var value = this.value
        this.$refs["form"].validate((valid) => {
          if (valid) {
            if (value[0] == 'input') {
              var submitData = JSON.stringify({
                createTime: this.getDate(),
                label: this.form.label,
                type: 'input',
                inputType: value[1],
                mustFill: value[2],
                tips: this.form.tips,
                radioCount: null,
                radioOption: null
              })

            } else if (value[0] == 'radio' && value[1] == 'danxuan') {
              var options = []
              this.form.radioOptions = this.radioOptions
              for (var item of this.form.radioOptions) {
                options.push(item.name)
              }
              var subOption = options.join(';')
              console.log(options.join(';'))
              var submitData = JSON.stringify({
                createTime: this.getDate(),
                label: this.form.label,
                type: 'radio',
                radioCount: 1,
                mustFill: value[2],
                radioOption: subOption,
                tips: this.form.tips,
                inputType: null
              })
              console.log(this.form.radioOptions)
            } else if (value[0] == 'radio' && value[1] == 'duoxuan') {
              var options = []
              this.form.radioOptions = this.radioOptions
              for (var item of this.form.radioOptions) {
                options.push(item.name)
              }
              var subOption = options.join(';')

              console.log(options.join(';'))
              var submitData = JSON.stringify({
                createTime: this.getDate(),
                label: this.form.label,
                type: 'radio',
                mustFill: value[2],
                radioOption: subOption,
                tips: this.form.tips,
                radioCount: this.form.radioCount,
                inputType: null
              })
            } else if (value[0] == 'image') {
              var submitData = JSON.stringify({
                createTime: this.getDate(),
                label: this.form.label,
                type: 'image',
                mustFill: value[1],
                id: this.id,
                tips: this.form.tips,
                inputType: null,
                radioCount: null,
                radioOption: null
              })
            } else if (value[0] == 'date') {
              var submitData = JSON.stringify({
                createTime: this.getDate(),
                label: this.form.label,
                type: 'date',
                mustFill: value[1],
                id: this.id,
                tips: this.form.tips,
                inputType: null,
                radioCount: null,
                radioOption: null
              })
            } else if (value[0] == 'datetime') {
              var submitData = JSON.stringify({
                createTime: this.getDate(),
                label: this.form.label,
                type: 'datetime',
                mustFill: value[1],
                id: this.id,
                tips: this.form.tips,
                inputType: null,
                radioCount: null,
                radioOption: null
              })
            }
            console.log(submitData)
          }
        })



      },
    },

  };
</script>
  
  • 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
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
  • 377
  • 378
  • 379
  • 380

在这里插入图片描述
在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/113756
推荐阅读
相关标签
  

闽ICP备14008679号