Update Text in B-table Cell using Boostrap Vue

I need to change the text color of each cell in a column within a b-table. I have successfully changed the text color of the column header, but what about the individual cell texts?

Here is the current b-table code:

<b-card title="Total">
  <b-table sticky-header="600px" hover :items="total" :fields="groupByFields"></b-table>
</b-card>

The fields where the column header color changes are defined below:

groupByFields: [
    {
      key: 'name',
      sortable: true,
    },
    {
      label: 'Total',
      key: 'count',
    },
    {
      key: 'interested',
      thStyle: { color: '#3eef33' },
      sortByFormatted: false,
      formatter: (value) => {
        const res = value;
        return (`($${res})`);
      },
    },
  ],

The thStyle color attribute only affects the header text color, not the values in the cells of that column. How can I ensure the cell text matches the header text color of that column too?

Answer №1

Here is a demonstration using the #cell() slot:

new Vue({
  el: '#app',
  data: () => ({
    items: [],
    fields: [
      'id',
      { key: 'title', style: { fontStyle: 'italic' } },
      { key: 'price', style: { color: 'red', textAlign: 'right' } }
    ]
  }),
  mounted() {
    fetch('https://dummyjson.com/products')
     .then(_ => _.json())
     .then(_ => this.items = _.products);
  }
})
<link href="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="06646969727572746776463228302837">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ceaca1a1babdbabcafbee3b8bbab8efce0fcfce0fe">[email protected]</a>/dist/bootstrap-vue.css" rel="stylesheet"/>
<script src="https://unpkg.com/babel-polyfill/dist/polyfill.min.js"></script>
<script src="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3046455570021e061e0102">[email protected]</a>/dist/vue.min.js"></script>
<script src="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="264449495255525447560b50534366140814140816">[email protected]</a>/dist/bootstrap-vue.js"></script>

<div id="app">
  <b-table :items="items" :fields="fields">
    <template #cell()="{field, value}">
      <div :style="field.style" v-text="value" />
    </template>
  </b-table>
</div>

If you only wish to add styles to certain columns, consider utilizing specific #cell({key}) slots.

Detailed instructions can be found here.


One popular strategy is to assign classes to cells. This approach allows for precise control over styling while maintaining flexibility.

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

Loading STL files from a buffer instead of a path within Three.js

I'm struggling to showcase a user-uploaded STL file in Three.js. The issue arises when my file is delivered to the front-end through a GET request: res.sendFile(path). Unfortunately, I can't directly utilize this raw data to load the file withou ...

Link varying columns to a table with the power of AngularJS

I'm currently facing a challenge with displaying data on a table that I fetch from an external service. The issue lies in the fact that the data received from the service varies in terms of columns, sometimes there are 5 columns and other times there ...

Leveraging AJAX for managing multiple selection options without the need for a submit button

Currently, I have a page featuring two select buttons. Whenever both are selected, the page should update automatically. Furthermore, each time a new option is chosen, the page needs to keep updating on its own. Below is my HTML code: <form action="" m ...

Is there a way for me to connect a radio button to the app component?

Currently, I am utilizing radio buttons to choose the Month & Year options. However, I am uncertain about how to link the button selection in order to determine which chart should be displayed. //template <b-form-radio-group class="mr-3" i ...

What is the best way to incorporate an image that appears when a user clicks on a

My goal is to dynamically place an image exactly where a user clicks on the webpage. Currently, I have the following code, but it only adds the image at the top and continues to do so repeatedly...not appearing at the clicked location. <html> ...

The mobile web app on iOS is stuck in a never-ending loop of showing the

My mobile app is built using angular.js, Twitter Bootstrap, and grunt with a .NET back end. After logging in, the loading spinner keeps showing up constantly in the top nav next to the time and battery status. We make server calls at login through a factor ...

Displaying a modal in Vue by clicking and populating it with information retrieved

Working with JSON data: "8":{ "name": "Dog", "age": 2, "showModal": false }, "9":{ "name": "Cat", "age": 7, "showModal": false }, Using v-for loop to display the data: <div v-for="(animal, index) in animals" :key="index"> ...

Building a Fullscreen Modal with Bootstrap in React is a great way to

Utilizing the Modal component from bootstrap for React, as seen here. To create a Modal, the following code is used: import React, {Component} from 'react'; import {BaseComponent} from 'BaseComponent'; import { Modal, Button } from &ap ...

Optimizing row display for each page with Material UI's pagination feature

I've been attempting to utilize Material UI table pagination to show 5 rows per page, but for some reason it displays all items on the page and doesn't switch between pages. I'm having trouble figuring out where I'm going wrong. To help ...

Troubleshooting a JSON error encountered while utilizing the mongoimport tool

Currently, I am utilizing the mongoimport utility to import data from a json file into mongodb with the following command: mongoimport --db city --collection inspections ./city_inspections.json #mongo import utility The json data structure looks like this ...

Subpar resolution of PNG images displayed in HTML canvas

My attempt to draw a PNG image onto the canvas is resulting in poor quality. I am using the drawImage method as shown below: src = folder+self.cur+".png"; imageObj.src = src; imageObj.onload = function() { context.clearRect(0, 0, cv, ch), context.drawImag ...

Create an interactive Chart.js displaying real-time data fetched through ajax requests, designed to be fully responsive. Addressing

Chart.js (http://www.chartjs.org/docs/) is the tool I'm using for charting purposes. I am looking to retrieve data from an Ajax request while ensuring that the chart is responsive. Within my HTML markup, I've included a canvas element like this ...

Looking for a way to dynamically adjust the height based on window resizing

I am trying to figure out how to adjust the height of a scrollable grid dynamically. I came across a solution on this website However, the provided solution is not suitable for me as it has: styles: [` html, body, my-app { height: 100%; } '] ...

Is it possible to load the factory in Angular JS just once?

I have set up a static json file to act as my server and fetch an array of orders from it. These orders are displayed in a table on my HTML page, with the ability to delete them individually. However, every time I refresh the HTML page, the full list is re ...

How to load images in TreePanel in Ext JS

While creating an Ext.TreePanel, I wanted to include an image in the node along with the text containing the URL to the image. However, I encountered an issue where the image was not loading on the page, only the text was visible. Is there a way to display ...

Managing dynamic input texts in React JS without using name properties with a single onChange function

Dealing with multiple onChange events without a predefined name property has been challenging. Currently, one text input controls all inputs. I have come across examples with static inputs or single input functionality, but nothing specifically addressin ...

The AJAX function triggers repeatedly upon each click

There is a form with two buttons: save & continue and save and exit. Each button has its own id, save_cont and save_exit respectively. When clicking the second button, the ajax action of the first button is triggered as well, preventing the URL redire ...

Looking to include a sub element in my menu items using jQuery and CSS

Using jQuery and CSS, I have created a menu item. How can I navigate from one menu item to another sub menu item, as illustrated in the image below? Below is my HTML markup: <link rel="stylesheet" href="css/style_j.css" type="text/css" media="screen"/ ...

Optimal Placement for FB.Event.subscribe

Here is the code I have implemented on my website for the Facebook Like and Share buttons. The functionality works seamlessly. When I click the Like button, a notification is promptly displayed on my Facebook profile page. Additionally, Facebook automatic ...

What is the best way to connect a router link and dialog box in a dropdown menu using Vue.js?

https://i.sstatic.net/JahMG.pngI am working on implementing a dropdown menu that includes a router link to another component and a dialog component for a dialog box. Here is the code snippet for reference: <td align="center"> <v-btn ...