Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Vue3 dynamic classes

I want to give dynamic or different classes to different rows of an element plus table. Basically, I want to give different colors to each row. I want to iterate an array that has colors in it for example

const colors = ["blue","yellow","black"]

I have tried binding this to my row like this:

<el-table-column prop="owner">
          <template #header>
            <span class="title">所有者</span>
          </template>
          <template #default="scope">
            <div class="owner">
              <div :class="colors">
                {{ scope.row.owner.slice(0, 1) }}
              </div>
              <div>{{ scope.row.owner }}</div>
            </div>
          </template>
        </el-table-column>

But, it is not iterating through the array and gives only one class to all of the rows.
This is the CSS for classes:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

.blue {
  background-color: rgb(125, 125, 216);
}
.yellow {
  background-color: rgb(164, 164, 71);
}
.black {
  background-color: black;
}

Hope this explains my question, should I attach the full code for reference?

>Solution :

You could choose one color based on the row index mod 3 :

<div :class="colors[scope.$index % 3]">
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading