1

I need to enable/disable specific inputs on button click.

My problem here is when I click my button, ALL the inputs enable/disable I am having a hard time targeting a single one. I use props “articles” which returns the “id”, “title” and “body” of my articles.

<div v-for="(article, index) in articles" v-bind:key="article.id">
  <div class="flex">
    <div class="flex-1 px-2 py-2">
      <input
        v-model="article.title"
        type="text"
        :disabled="isDiabled"
        class="w-full disabled:opacity-75 bg-gray-300 focus:bg-white"
      />
    </div>
    <div class="flex-1 px-2 py-2">
      <input
        v-model="article.body"
        type="text"
        :disabled="isDiabled"
        class="w-full disabled:opacity-75 bg-gray-300 focus:bg-white"
      />
    </div>
    <div class="px-2 py-2">
      <button
        class="px-2 py-2 border-gray-400 bg-gray-800 rounded text-white hover:border-gray-300 items-end"
        @click="enableEdit"
      >
        Edit
      </button>
    </div>
  </div>
</div>

<script>
  export default {
    props: ["articles"],

    data() {
      return {
        isDiabled: true,
      };
    },

    methods: {
      enableEdit() {
        this.isDiabled = false;
      },
    },
  };
</script>

Every article has his own button and I want only the two inputs of the button that is clicked to be enabled/disabled and not all of them.

2 Answers 2

1

As being pointed you need to add also that property to your articles... of course you don't need to do this in the database, still using it as a property you need also to emit this change of the flag to the parent.

You could also use a local version of the articles where you add also this isDisabled property, you fill this new variable at the creation of the component based of course on the property you received from the parent,

created(){
  this.local_articles = this.articles.map(e => { 
        return {...e, isDisabled: true} }
  )
},

this way you don't need to propagate nothing to the parent as you can handle all internally.

This fiddle gives a solution to your problem.

Sign up to request clarification or add additional context in comments.

1 Comment

YES!! thank you! I was having a hard time adding new values to the articles array. Not only you fixed my problem you also opened my eyes to "created()".
1

Make a boolean property for each article, then change the binding of disabled to that property.

3 Comments

So I would have to store the disabled property in my database? How can I do it without storing it to my database. Just a simple toggle on button click
You don't need to. You can add it in JS after you receive the initial one. You can also have another array with true/false by index. Whatever you prefer
Thank you for you help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.