How to add a class to a label tag of an input dynamically selected by Vue.js?

Asked

Viewed 208 times

0

I am using Boostrap and have a sector in the form where the client will choose some items, and when clicking on the item the bootstrap adds the class active in the selected item, and with css I make the item change color.

Getting kind of like this:

<label class="btn bg-grey active">
  <input type="checkbox" name="tagslist[]" value="valor"> Titulo
</label>

I am using the Vue.js to create items dynamically through json data. And to automatically select checkboxes according to the open record.

But my problem is that in Vue.js when it starts leaving checked the items I need, it only does it in the checkbox, but does not add the active class in the label, thus staying:

<label class="btn bg-grey">
  <input type="checkbox" name="tagslist[]" value="valor" checked> Titulo
</label>

How do I get him to add this class to the parent tag of this checkbox input?

Follow below the code working, see that when starting the checked items do not change color, only change when click on top, and still a bug that ends up changing color even if the item is not checked.

new Vue({
  el: '#v-for-tags',
  data: {
     seltags: ["feminino","masculino"],
     tagsitens: {"codes":{"feminino":{"tagtitulo":"Mulheres","tagslug":"women"},"infantil":{"tagtitulo":"Crianças","tagslug":"childs"},"masculino":{"tagtitulo":"Homens","tagslug":"men"}},"list":["feminino","infantil","masculino"]}
  }
});
.btn.bg-grey.active {
    background-color: #673ab7 !important;
}

.iconsdinamic {
    float: left;
    margin-left: 10px;
}
<div class="btn-group-toggle" data-toggle="buttons" id="v-for-tags">
    <div v-for="(option, keyoption) in tagsitens.codes" class="iconsdinamic">
        <label class="btn bg-grey">
          <input type="checkbox" name="tagslist[]" autocomplete="off" v-bind:value="keyoption" v-model="seltags"> {{ option.tagtitulo }}
        </label>
   </div>
</div>

<!-- --------------------------------------------- -->

<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.min.js"></script>

Please help me out. Thank you

1 answer

1


You can do it like this:

v-bind:class="{ active: seltags.includes(keyoption) }"

The code above:

  • Add the code to label

     <label class="btn bg-grey">
    
  • Checks whether the object seltags contains keyoption if ever, return true if not false

  • the return will determine if the class active will be added.

To solve the problem cited via comment:

if you press on the checkbox square, it changes color showing this selected but does not check the checkbox.

just remove the attribute: data-toggle="buttons"

Working example

new Vue({
  el: '#v-for-tags',
  data: {
     seltags: ["feminino","masculino"],
     tagsitens: {"codes":{"feminino":{"tagtitulo":"Mulheres","tagslug":"women"},"infantil":{"tagtitulo":"Crianças","tagslug":"childs"},"masculino":{"tagtitulo":"Homens","tagslug":"men"}},"list": ["feminino","infantil","masculino"]}
  },
});
.btn.bg-grey.active {
    background-color: #673ab7 !important;
}

.iconsdinamic {
    float: left;
    margin-left: 10px;
}
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.min.js"></script>

<div class="btn-group-toggle" id="v-for-tags">
    <div v-for="(option, keyoption) in tagsitens.codes" class="iconsdinamic">
        <label class="btn bg-grey" v-bind:class="{ active: seltags.includes(keyoption) }">
          <input type="checkbox" name="tagslist[]" autocomplete="off" v-bind:value="keyoption" v-model="seltags"> {{ option.tagtitulo }}
        </label>
   </div>
</div>

Reference

  • Thank you, I just have one more question. that way you put it does a if the selected data is equal to the value ne so it creates the dynamic data? Is there any way that if monitor when the checkbox is active even after it is created? So, if you press on the checkbox box, it changes the color showing this selected but does not check the checkbox.

  • Yes, yes, I know. Your answer already helps me a lot, but I asked if there was a way to monitor the click by checkbox check, it would be even better. I asked more to know. But if there’s no way it’s okay anyway. ^^

  • I didn’t think to use Boostrap-Vue, because I’d have to change everything that’s ever been created, wouldn’t I? The site is almost ready with boostrap and Vue separated. I’m afraid of changing and end up harming the whole site.

  • It’s I think he just monitors the click on the button by the class btn-group-toggle. I think boostrap did it to use this active on anything that uses this class. I think if I could make you monitor the checkbox check, I could take the toggle out of the boostrap and use it just for Vue. I don’t know if it would work.

  • 1

    @Samantasilva see now how this! I believe that now everything is right.

  • Thanks buddy, everything worked out, yeah, perfect, helped me a lot to solve the two problems that I was breaking my head. Thank you for your patience in helping me ♥

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.