当前位置: 代码网 > it编程>前端脚本>Vue.js > vue elementui 动态追加下拉框、输入框功能

vue elementui 动态追加下拉框、输入框功能

2024年05月26日 Vue.js 我要评论
vue elementui 动态追加下拉框、输入框上代码:<template> <div> <el-dialog append-to-body

vue elementui 动态追加下拉框、输入框

上代码:

<template>
  <div>
    <el-dialog
      append-to-body
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      width="65%"
      @close="onclose"
      :modal-append-to-body="true"
      title="新建"
      custom-class="dialogbox"
      :visible.sync="dialogvisible"
      :lock-scroll="true"
      :destroy-on-close="true">
      <el-form :model="combinationinfo" label-width="90px" ref="combinationform" :rules="rules" :inline="true" size="small">
        <el-row>
          <el-col :span="12">
            <el-form-item prop="benchmarks" label="名称">
              <div style="color: #fb6b3f;width: 230px;">比例之和需为100%(当前
                <span>{{benchmarktotal}}</span>
                <span></span>%)
              </div>
              <div v-for="(item,index) in combinationinfo.benchmarks" :key="index">
                <el-select
                  style="margin-bottom: 10px;"
                  clearable
                  filterable
                  collapse-tags
                  placeholder="请选择"
                  class="benchmarkselectwidth"
                  @change="changebenchmark"
                  v-model="item.code"
                >
                  <el-option
                    v-for="(item1, idx) in list"
                    :key="idx"
                    :label="item1.name"
                    :value="item1.code"
                  >
                  </el-option>
                </el-select>
                <el-input v-model="item.percentage" @input="changepercentage" placeholder="请输入" class="benchmarkinputwidth"></el-input>
                <span style="padding-left: 2px;">%</span>
                <i v-if="index !== 0" style="font-size: 18px;cursor: pointer;padding: 0 0 0 10px;color: #f56c6c;" @click="deleteindex(index)" class="el-icon-remove-outline"></i>
              </div>
              <div v-if="benchmarkrule" style="color: #f56c6c;font-size: 12px;margin-top: -17px">请选择名称</div>
              <el-button @click="addindex" size="mini" type="primary" icon="el-icon-plus">添加</el-button>
            </el-form-item>
          </el-col>
          <el-col :span="12">
          </el-col>
        </el-row>
      </el-form>
      <div style="text-align: right;margin-top: 20px;">
        <el-button size="mini" @click="onclose()">取 消</el-button>
        <el-button
          size="mini"
          type="primary"
          @click="onconfirm()"
          >提 交</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>
data() {
    return {
      dialogvisible: false,
      combinationinfo: {
        benchmarks: [
          {
            code: '',
            name: '',
            percentage: '',
          }
        ]
      },
      rules: {
        benchmarks: [{ required: true }],
      },
      benchmarkrule: false,
      benchmarktotal: 0,
      list: [
        {
          name: 'aaa',
          code: '111',
        },
        {
          name: 'bbb',
          code: '222',
        },
        {
          name: 'ccc',
          code: '333',
        },
      ],
    }
  },
methods: {
 // 添加
    addindex () {
      this.combinationinfo.benchmarks.push({
        code: '',
        percentage: '',
      })
    },
    // 删除
    deleteindex (index) {
      this.combinationinfo.benchmarks.splice(index,1)
      this.changepercentage()
    },
    changebenchmark (val) {
      if (this.combinationinfo.benchmarks.length && this.combinationinfo.benchmarks.length > 1) {
        if (!this.isrepeat(this.combinationinfo.benchmarks,'code')) {
          this.$message.warning('所选名称不能重复!')
          return
        }
      }
    },
     // 判断数组中是否有重复数据(true 不存在;false 存在重复)
    isrepeat(arr, key) {
      var obj = {};
      for (let i = 0; i < arr.length; i ++) {
          if (obj[arr[i][key]]) {
              return false;    // 存在
          } else {
            obj[arr[i][key]] = arr[i];
          }
      }
      return true;
    },
    // 名称值变化时
    changepercentage (val) {
      this.benchmarktotal = 0
      if (this.combinationinfo.benchmarks.length && this.combinationinfo.benchmarks.length > 0) {
        for(let i = 0; i < this.combinationinfo.benchmarks.length; i++) {
          if (this.combinationinfo.benchmarks[i].percentage === '') {
            break
          }
          this.benchmarktotal+=parsefloat(this.combinationinfo.benchmarks[i].percentage)
        }
      }
    },
    // 提交
    onconfirm() {
      if (this.combinationinfo.benchmarks) {
          for(let i = 0; i< this.combinationinfo.benchmarks.length; i++) {
            if (this.combinationinfo.benchmarks[i].code) {
              this.benchmarkrule = false
            } else {
              this.benchmarkrule = true
              return
            }
          }
        }
        if (this.benchmarktotal !== 100) {
          this.$message.warning('名称比例之和需为100%!')
          return
         }
     },
     // 取消
    onclose() {
      this.benchmarkrule = false
      this.dialogvisible = false
    },
},

展示效果图:

在这里插入图片描述

到此这篇关于vue elementui 动态追加下拉框、输入框的文章就介绍到这了,更多相关vue elementui 追加下拉框内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com