1

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,}
         ]
      }
}
3
  • It's easy! just put your classes in a row this.smallTable.smTable = "table-sm table-class2 tbl-class3" Commented Aug 10, 2022 at 7:48
  • No, manybe you missunderstood me .I dont want this. I want multiple input type="checkbox" where i can chose different classe i want to add on my table. One checkbox for table-sm one for table-bordered and some other class maybe Commented Aug 10, 2022 at 7:52
  • I fixed my answer. It gives you the idea. Use your real names instead, please. Commented Aug 10, 2022 at 7:59

2 Answers 2

1

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.

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

8 Comments

So if i do this, where i check the checkbox 3 this classes will add on my table tag. But this is not what i want. I want multiple checkbox, For example: 3 input checkbox will have each by this class, so when i click first checkobx i want to assing only first class, not all 3 cass
So, in this case, you need to use Radio Buttons instead of Checkboxes.
I dont waht to use radio couse maybe i need to check multiple classes, ex: table-sm and table-bordered in same time. But this should be with 2 differend checkboxes. I have updated my questions
Imagine a situation where every 3 checkboxes are active. Which classes do you want to apply to?
If 3 checkboxes are active: table-sm, table-bordered, table-hover. I think this is a good combination. I just want to let users to personalize his table, so this is the reason i save this on localstorage
|
0

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

2 Comments

Can you do an example with mu code? I am not understanding how it work. I need to add multiple input but for differenc classes
So I probably did not understand your question correctly

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.