【问题标题】:Bootstrap Vue change background for each cell in b-tableBootstrap Vue 更改 b 表中每个单元格的背景
【发布时间】:2021-04-21 19:02:43
【问题描述】:

我正在使用 bootstrap vue b-table 来显示我的项目:

<b-table striped bordered :tbody-tr-class="row_class" :fields="fields" :items="items" head-variant="light" class="o-list"
    thead-class="o-gradient-header"
    @row-selected="onRowSelected" selectable
    no-local-sorting @sort-changed="change_sort" :sort-by="list_options.sort"
    :sort-desc="list_options.order==='desc'">

...

</b-table>

我想根据一个函数调用来改变每个单元格的背景颜色:

row_class(item) {
    // Insert conditionals
    // return CSS class string 
}

但这会改变整行的样式。我不想要那个。我只想更改单元格的样式?有没有办法改变每个单元格的背景颜色?

【问题讨论】:

    标签: javascript css vue.js bootstrap-vue


    【解决方案1】:

    您可以使用 tbody-tr-class 属性设置每一行的样式

    <b-table striped hover caption-top
              :items="items"
              :fields="fields"
              :tbody-tr-class="rowClass"
        >
        </b-table>
    

    脚本

    new Vue({
        el: "#app",
        data() {
          return {
            fields: [
              {
                key: "name",
                label: "Name",
                sortable: true
              },
              {
                key: "email",
                label: "Email",
                sortable: true
              },
              {
                key: "age",
                label: "Old",
                sortable: true
              }
            ],
            items: [
              { age: 40, name: "admin1", email: "hoge@for.jp" },
              { age: 21, name: "admin2", email: "huga@for.jp" },
              { age: 89, name: "admin3", email: "piyo@for.jp" },
              { age: 38, name: "admin4", email: "aaaaa@for.jp" }
            ]
          };
        },
        methods: {
          rowClass(item, type) {
            if (!item || type !== 'row') return
            if (item.age > 30) return 'table-success'
          }
        }
      });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-08
      • 2018-07-19
      • 1970-01-01
      • 2013-01-14
      • 1970-01-01
      • 1970-01-01
      • 2021-11-05
      • 2013-06-15
      相关资源
      最近更新 更多