Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vuejs disabled selected dropdown options?

Tags:

jquery

vue.js

I have an array selected which holds a set of selected options. I'd like to use that array to add a disabled attribute on those options.

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title> Vue-js Control </title>
    </head>
    <body>
        <div id="demo">
            <select v-model="selected" multiple>
              <option v-for="option in options" v-bind:value="option.value">
                {{ option.text }}
              </option>
            </select>
            <span>Selected: {{ selected }}</span>
        </div>

    <script type="text/javascript" src='http://cdnjs.cloudflare.com/ajax/libs/vue/1.0.16/vue.min.js'></script>
    <script type="text/javascript">
    new Vue({
        el: '#demo',
        data: {
            selected: ['A','B','D'],
            options: [
              { text: 'One', value: 'A' },
              { text: 'Two', value: 'B' },
              { text: 'Three', value: 'C' },
              { text: 'Four', value: 'D' },
              { text: 'Five', value: 'E' },
              { text: 'Six', value: 'F' },
              { text: 'Seven', value: 'G' },
            ]
        }
    });
    </script>
</body>

like image 446
Renish Khunt Avatar asked Feb 24 '16 06:02

Renish Khunt


People also ask

How to disable a dropdown Vue?

DropdownButton component can be enabled/disabled by giving disabled property. It can be disabled by setting disabled property as true .

How do I get the selected value of dropdown in Vue?

Get Selected Value of Select Dropdown in VueCreated a select box inside the template syntax. Added an onChange() event handler. Created an options list, cars name primarily. Used the on-change method to grab the selected value using the event object.

How do you turn off selection in HTML?

disabled: The <select> disabled attribute is used to specify the select element is disabled. A disabled drop-down list is un-clickable and unusable. It is a boolean attribute.

How do I create a dropdown on Vue?

Dropdown. The most common way of making a dropdown in Vue has always been this way: First, you define a toggler (usually a <button> or an <a> ) with a click event that will call a toggle method. You define the menu with a v-if directive that is bound to an active state.


1 Answers

You want to disable all options that are in the selected array?

<option v-for="option in options" :disabled="selected.includes(option.value)" :value="option.value">
like image 192
Jeff Avatar answered Oct 02 '22 05:10

Jeff