首页 > 其他分享 >element-ui 中upload组件与表单组件的结合使用

element-ui 中upload组件与表单组件的结合使用

时间:2022-12-05 23:34:10浏览次数:40  
标签:upload item trigger ui attachment file 组件 message

背景

有的时候我们需要在表单中携带一些上传的附件传给服务器

搭建基本表单结构

<template>
  <div>
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="活动名称" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item label="活动区域" prop="region">
        <el-select v-model="ruleForm.region" placeholder="请选择活动区域">
          <el-option label="区域一" value="shanghai"></el-option>
          <el-option label="区域二" value="beijing"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="上传附件" prop="attachment" class="upload-item">
        <el-upload
          ref="upload"
          action=""
          :on-remove="handleRemove"
          :file-list="fileList"
          :auto-upload="false"
        >
          <el-button slot="trigger" size="small" type="primary"
            >选取文件</el-button
          >
          <div slot="tip" class="el-upload__tip">
            只能上传jpg/png文件,且不超过500kb
          </div>
        </el-upload>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >立即创建</el-button
        >
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        name: '',
        region: '',
        attachment: '',
      },
      rules: {
        name: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' },
        ],
        region: [
          { required: true, message: '请选择活动区域', trigger: 'change' },
        ],
        attachment: [
          { required: true, message: '请上传附件', trigger: 'blur' },
        ],
      },
      fileList: [],
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          alert('submit!')
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    handleRemove(file, fileList) {
      console.log(file, fileList)
    },
  },
}
</script>

<style scoped>

</style>

问题1:upload的label不垂直居中

添加如下样式即可解决。

.upload-item {
  display: flex;
}
/deep/ .upload-item .el-form-item__label {
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: orange;
}
/deep/ .upload-item .el-form-item__content {
  margin-left: 0 !important;
}

效果如下:

问题2:文件已经上传,但是表单组件校验不通过

问题效果:

解决办法:使用表单组件中的自定义规则

data() {
    var validateAttachment = (rule, value, callback) => {
      if (value == '') {
        return callback(new Error('上传的附件不能为空'))
      } else {
        console.log('有上传附件')
        callback()
      }
    }
    return {
      ruleForm: {
        name: '',
        region: '',
        attachment: [],
      },
      rules: {
        name: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' },
        ],
        region: [
          { required: true, message: '请选择活动区域', trigger: 'change' },
        ],
        attachment: [{ validator: validateAttachment, trigger: 'change' }],
      },
    }

优化

需求一:上传文件后,当移除时提示"附件必填"错误的消息;

完整代码

<template>
  <div>
    {{ ruleForm }}
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="活动名称" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item label="活动区域" prop="region">
        <el-select v-model="ruleForm.region" placeholder="请选择活动区域">
          <el-option label="区域一" value="shanghai"></el-option>
          <el-option label="区域二" value="beijing"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        label="上传附件"
        prop="attachment"
        required
        class="upload-item"
      >
        <el-upload
          ref="upload"
          action=""
          :on-remove="
            (file, fileList) => {
              handleRemove('attachment', file, fileList)
            }
          "
          :file-list="ruleForm.attachment"
          :auto-upload="false"
          :on-change="
            (file, fileList) => {
              handleUploadChange('attachment', file, fileList)
            }
          "
        >
          <el-button slot="trigger" size="small" type="primary"
            >选取文件</el-button
          >
          <div slot="tip" class="el-upload__tip">
            只能上传jpg/png文件,且不超过500kb
          </div>
        </el-upload>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >立即创建</el-button
        >
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    var validateAttachment = (rule, value, callback) => {
      if (value == '') {
        return callback(new Error('上传的附件不能为空'))
      } else {
        console.log('有上传附件')
        callback()
      }
    }
    return {
      ruleForm: {
        name: '',
        region: '',
        attachment: [],
      },
      rules: {
        name: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' },
        ],
        region: [
          { required: true, message: '请选择活动区域', trigger: 'change' },
        ],
        attachment: [{ validator: validateAttachment, trigger: 'change' }],
      },
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          alert('submit!')
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    handleRemove(field, file, fileList) {
      // console.log(file, fileList)
      this.ruleForm[field] = []
      this.$refs.ruleForm.validateField('attachment')
    },
    handleUploadChange(field, file, fileList) {
      this.ruleForm[field].push(file)
    },
  },
}
</script>

<style scoped>
/* 解决表单中嵌入上传组件时label不居中的问题 */
.upload-item {
  display: flex;
}
/deep/ .upload-item .el-form-item__label {
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: orange;
}
/deep/ .upload-item .el-form-item__content {
  margin-left: 0 !important;
}
</style>

效果如下:

标签:upload,item,trigger,ui,attachment,file,组件,message
From: https://www.cnblogs.com/it774274680/p/16953903.html

相关文章

  • SwiftUI 基础 —— 状态管理机制
    @State被@State包装的值发生改变时,UI将被同时改变structContentView:View{@Statevartitle="HelloWorld"varbody:someView{VStack{......
  • kubernetes集群节点多网卡,calico/flannel组件如何指定网卡
    kubernetes集群节点多网卡,calico/flannel组件如何指定网卡1、calico如果有节点是多网卡,所以需要在deploy的env指定内网网卡  spec:containers:-env:-......
  • element ui的selecte的选择框多选,出hover提示,文字和数量展示一行
     <el-tooltipclass="itemhello":disabled="isTooltipDisabled":content="selectTooltipArr.join(',')"      placement="top-start">      ......
  • StringBuffer类和StringBuilder类
    1.StringBuffer类基本介绍java.lang.StringBuffer代表可变的字符序列,可以对字符串内容进行增删。很多方法与String相同,但StringBuffer是可变长度的。StringBuffer是一......
  • burpsuite和浏览器配置正确抓不到火狐的数据包的解决办法
    按照下图步骤在burpsuite中导出CA证书点击DER格式的证书,然后点击下一步点击选择文件在弹出的框中设置文件名与文件目录点击保存后来到下图,点击下一步证书导出完成,在你设置的......
  • burpsuite抓不到127.0.0.1和localhost的数据包
    各种配置都做了,还是抓不到包怎么办呢打开命令行Windows打开cmd,linux打开终端查一下本机ip,由于我是连接的wifi,所以看的是wlan将burpsuite和火狐浏览器上的127.0.0.1换成这......
  • XiaoFeng组件
    XiaoFeng组件C#NETCORE帮助类库基础类库、数据库中间件、网络通讯中间件、Redis、Socket、HttpHelper、QueryableX、Cache、FTP、JSON、XML、IO、配置、各种加密码算......
  • 探究lombok-03-@Builder使用
    Java8org.springframework.boot2.7.3lombok1.18.24EclipseVersion:2022-09(4.25.0)--ben发布于博客园 上一篇:探究lombok-02-类继承https://www.cnblogs.com/......
  • Vue组件
    组件的使用使用import语法导入需要的组件使用components节点注册组件以标签形式使用刚才注册的组件全局组件:在main.js入口文件中,通过Vue.component()方法注册全局组......
  • Vue2(笔记21) - 组件 - 组件的嵌套
    组件的嵌套官方的图中,就可以看出,组件之间是可以嵌套的;没有嵌套的组件为了方便,就直接把前面写好的拿来用了;<divid="root"><h1>{{msg}}</h1><hr><school></schoo......