Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

122
Vistas
Vue js 3 use multiple custom classes on table dynamically

I am working on a project where i use vue for my frontend, but i am new on vue. And i am trying to add some dynamically class on my table. So for example where i check table-sm table will assing this class. Already i have done this but it work only with one class. I really dont know how to do this to work with an array. Can someone help ?

I want also to add another input for other classes EX: table-bordered and table-striped ...

<div class="form-check form-switch">
  <input v-model="smallTable.active" type="checkbox" class="form-check-input" id="smallTable" @change="tableStyles()">
  <label class="form-check-label" for="smallTable">
    Small Table
  </label>
</div>

//Table tag
<table class="table table-hover align-middle mb-0" :class="smallTable.smTable">
...

//vue component
data() {
        return {
        smallTable: {smTable: "", active: false,},
      }
}

tableStyles() {
            if (this.smallTable.active) {
                this.smallTable.smTable = "table-sm"
            }else {
                this.smallTable.smTable = ""
            }
            localStorage.setItem("tableStyle", JSON.stringify(this.smallTable));
        },

what i am loking for is something like this:

data() {
        return {
        smallTable: [
          {smTable: "", active: false,},
          {bordertable: "", active: false,}
         ]
      }
}
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

There are two approaches:

  1. Using a string of classes that separated by space
tableStyles() {
  this.smallTable.smTable = '';
  if (this.smallTable.active) {
    this.smallTable.smTable += ' table-sm';
  }
  if (this.checkbox1.active) {
    this.smallTable.smTable += ' table-bordered';
  }
  if (this.checkbox2.active) {
    this.smallTable.smTable += ' table-hover';
  }
  localStorage.setItem('tableStyle', JSON.stringify(this.smallTable));
},

  1. Using an object of classes by according boolean switch
// You are supposed to create an object
// whose keys are the class names and
// values are boolean showing how a class should apply or not.

tableStyles() {
  this.smallTable.smTable = {
    'table-sm':       this.smallTable.active,
    'table-bordered': this.checkbox1.active,
    'table-hover':    this.checkbox2.active,
  };
  localStorage.setItem('tableStyle', JSON.stringify(this.smallTable));
},

It gives you the idea of how to set classes dynamically. Please tune conditions according to your actual data.

about 4 years ago · Juan Pablo Isaza Denunciar

0

you can use ternary condition in class binding to binding multiple class for an element read more abou it here

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda