Is it possible to utilize Javascript object attributes as a designated HTML class?

I have a project where I am utilizing Javascript objects to store data in a table structure, similar to the example below:

{
 name: toast,
 id: 15,
 style: small,
},
{
 name: bagel,
 id: 17,
 style: medium,
},

Is there a way to use the style attribute as an HTML class that can be styled using CSS?

I am also incorporating Vue.js into my project, allowing me to implement features like this:

<tr v-for="bread in breadBox" :key="bread.id">
 <td> {{bread.name}} </td>
</tr>

The main objective is to assign each style as the class of the <tr> element so that styles can be applied accordingly.

Answer №1

Utilize the :class attribute to bind your classes

new Vue({
  el: '#demo',
  data() {
    return {
      breadBox: [{name: 'toast', id: 15, style: 'small',}, {name: 'bagel', id: 17, style: 'medium',},]
    }
  }
})
.small {
  font-size: 1em;
}
.medium {
  font-size: 1.5em;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="demo">
  <table>
    <tr v-for="bread in breadBox" :key="bread.id" :class="bread.style">
      <td>{{ bread.name }}</td>
    </tr>
  </table>
</div>

Answer №2

simply complete

<tr :class="bread.style">

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is it possible to create a fading effect on an image using a border-radius of 50%?

Struggling with fading out an image that's circular in shape due to the border-radius: 50%. <section id="main"> <h1>Non-Important-Text</h1> <h4> it's all fun and games until you can't find a damn sol ...

Express.js: Communicating with internal microservices

I'm still learning about node.js, express.js and REST APIs. Here's the situation I'm facing: I have a requirement to retrieve user information from my database (using mongoDB) in various scenarios. What would be the recommended approach i ...

What does dist entail?

I am currently utilizing gulp to create a distribution folder (dist) for my Angular application. After consolidating all the controllers/services JS files and CSS, I am now faced with handling the contents of the bower folder. In an attempt to concatenat ...

Guide on dynamically updating a div in PHP with a mix of text output and images at regular intervals

My current project involves creating a browser-based card game primarily using PHP, with potentially incorporating other languages to help me enhance and test my PHP skills. However, I've encountered difficulties while attempting to implement various ...

What is the best way to manage asynchronous data within AngularJS directives?

Having encountered similar challenges to this specific scenario, I am currently facing issues with handling asynchronous data within my directives. The main issue arises when trying to pass asynchronously fetched data into these directives. Initially, I at ...

Could the inner object's value be retrieved from the wrapper object in a Vuex getter prior to mounting?

Currently grappling with a challenge while working on Vuex and VueJS involving the Computed property. To sum it up, I have a nested object within the Vuex state, attempting to retrieve a key from this inner object in a Vuex getter to display the value in ...

Acquire feedback from PHP using SweetAlert notifications

I need to update my HTML form to function like this: <script> function getName() { var name = $('#name').val(); var url_send = 'send.php'; $.ajax({ url: url_send, data: 'name=' + name, ...

Adjust image placement when resizing the window or rotating a mobile device

When the window is maximized or the phone is in portrait position, the magnifying glass stays in its place. However, when I resize the window or rotate the mobile phone, the position of the magnifying glass changes. I've tried using different units ...

Influence of External Style Sheet not reflected in HTML

Just as the title suggests, I have an external style sheet connected to my HTML document, and it appears that the footer element may be incorporating the changes, but none of the other content is. Being new to this, I'm trying to pinpoint where I went ...

designing a vertical HTML5 range input

Attempting to customize a vertical range input, here is what has been implemented: input[type='range'] { -webkit-appearance: slider-vertical; background-color: #ccc; height: 158px; width: 2px; margin: 8px auto; } input[type= ...

Tips for Deleting Navigation History in Nativescript-Vue Manual Routing

I find myself in a situation where the navigation calls are stacking up continuously, as illustrated in the image below. https://i.sstatic.net/evlFx.png This poses a problem, especially when I try to logout as it only adds another entry to the navigation ...

Utilize D3.js to display topoJSON data on your website

My project involves creating a visualization of the India map using d3 and GeoJSON. However, I am facing difficulties in properly displaying each Indian state on the map. Any assistance in identifying and resolving the issue would be greatly appreciated. T ...

Does ColdFusion go through a compilation process?

I recently received a request from a client who is interested in adding a third-party lead generation popup script to a couple of her websites. One of these sites was built using ColdFusion, a language I have not had much experience with before. My questio ...

Node.js require function not functioning as anticipated

After using Node and node express generator to create node express code successfully, I encountered an issue when attempting to deploy it to the server. By default, there are a couple of files present: .bin/www ( containing var app = require('../app ...

Verify if a specific image file is present in Wordpress using PHP. If not found, utilize a default banner instead

Can I achieve a setup similar to the Wordpress header/theme, where there is a default image banner that will only be used if the specified image file does not exist? The idea is for the logic to function as follows: if (the specified image file does not e ...

Unable to retrieve the Date Range Picker value in Vuejs

I am new to VueJS and I am having trouble retrieving the value of the selected date range. When I click the button, the console prints out a value of null. Can someone please assist me with this issue? Below is the code snippet: App.vue <template> ...

Inquiring about the design of an app that retrieves data from an in-memory SQLite database

As I work on integrating a dynamic <div> into my (fairly low-traffic) website, my main goal is to extract specific data from a memory-based SQLite database operated by a Python program on the server. With limited experience in web technology, I am un ...

commenting system through ajax across multiple web pages

Experimenting with the webcodo comment system has led me to this URL: Database table: "comments" CREATE TABLE IF NOT EXISTS `comments` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(40) NOT NULL, `email` varchar(60) NOT NULL, `comment` te ...

Populating and dynamically sorting a v-select component in Vuetify

I am working on populating and sorting an array dynamically for use in a v-select component. However, I am encountering the es-lint warning 'unexpected side-effect in computed property' because I am modifying objects within that function call. Is ...

Steps to achieve a hover effect like the App Icon on Google's Homepage

As a beginner in HTML & CSS, I've been working on cloning the Google Homepage. However, I have encountered some difficulty in achieving the hover effect on the App Icon. If you're unsure which App Icon I'm referring to, you can view the pic ...