最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to set multiple conditions inside of the :disabled property in vuetify? - Stack Overflow

programmeradmin2浏览0评论

I am working on a feature to allow users to upload files. I need to disable the 'Add file' button when

1) the field is empty

2) when the file size exceeds 100MB

This is the button:

<v-btn rounded :disabled="!uploadedFiles || fileSizeValidation" @click="confirmFileAdd">Add</v-btn>

This is what is inside of data:

    data: () => ({
    uploadedFiles: null,
    fileSizeValidation: [
        files => !files || !files.some(file => file.size > 100000000) || 'File size should be less than 100 MB!'
    ],
}),

Using either

:disabled="!uploadedFiles || fileSizeValidation" or :disabled="!uploadedFiles && fileSizeValidation"

unfortunately does not work.

|| actually yields an error:

How can I make sure the button is disabled for both conditions?

I am working on a feature to allow users to upload files. I need to disable the 'Add file' button when

1) the field is empty

2) when the file size exceeds 100MB

This is the button:

<v-btn rounded :disabled="!uploadedFiles || fileSizeValidation" @click="confirmFileAdd">Add</v-btn>

This is what is inside of data:

    data: () => ({
    uploadedFiles: null,
    fileSizeValidation: [
        files => !files || !files.some(file => file.size > 100000000) || 'File size should be less than 100 MB!'
    ],
}),

Using either

:disabled="!uploadedFiles || fileSizeValidation" or :disabled="!uploadedFiles && fileSizeValidation"

unfortunately does not work.

|| actually yields an error:

How can I make sure the button is disabled for both conditions?

Share Improve this question asked Apr 28, 2020 at 20:13 Beginner_HelloBeginner_Hello 3671 gold badge7 silver badges19 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

You can use a puted property. This property will repute every time uploadedFiles changes.

  puted: {
    fileSizeValidation() {
      return (
        !this.uploadedFiles ||
        this.uploadedFiles.length <= 0 ||
        this.uploadedFiles.some(file => !file.size ||file.size > 100000000)
      );
    }
  }

And then use it like that

<v-btn rounded :disabled="!this.uploadedFiles || this.uploadedFiles.length <= 0 || fileSizeValidation @click="confirmFileAdd">Add</v-btn>

Example :

Vue.config.devtools = false;
Vue.config.productionTip = false;

var app = new Vue({
  el: '#app',
  data: {
    numberInput: 10,
    uploadedFiles: []
  },
  methods: {
    test() {
      this.msg = "Hello World !";
    }
  },
  puted: {
    fileSizeValidation() {
      return this.uploadedFiles.some(file => !file.size || file.size > 100000000);
    }
  }
})
<script src="https://cdnjs.cloudflare./ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <input type="number" v-model="numberInput" />
  <button @click="uploadedFiles.push({ size : numberInput })">Add</button> {{ uploadedFiles }}
  <button :disabled="!this.uploadedFiles || this.uploadedFiles.length <= 0 || fileSizeValidation">Submit</button>
</div>

https://v2.vuejs/v2/guide/puted.html#Computed-Properties

发布评论

评论列表(0)

  1. 暂无评论