简体   繁体   English

如何更改 vuetify 数据表中的字体大小和/或样式?

[英]How to change font size and/or style in the vuetify data-table?

I have the following code, which renders the expected table:我有以下代码,它呈现预期的表:

<v-data-table
    dense
    :headers="table_headers"
    :items="table_data"
    :items-per-page="table_rows"
    :page="table_page"
    :hide-default-footer=true
    :loading="table_loading"
    class="elevation-2"
>
</v-data-table>

I was hoping that appending the display-2 class will do the trick for me, but it seems it doesn't have any effect:我希望附加display-2 class 对我有用,但它似乎没有任何效果:

<v-data-table
    dense
    :headers="table_headers"
    :items="table_data"
    :items-per-page="table_rows"
    :page="table_page"
    :hide-default-footer=true
    :loading="table_loading"
    class="elevation-2 display-2"
>
</v-data-table>

I also tried the following, but it simply renders a bunch of empty row elements.我也尝试了以下方法,但它只是呈现了一堆空row元素。 This is not shocking, as I suppose vuetify now expects that I will provide a complete template for each of the rows?这并不令人震惊,因为我想 vuetify 现在期望我将为每一行提供一个完整的模板?

<v-data-table
    dense
    :headers="table_headers"
    :items="table_data"
    :items-per-page="table_rows"
    :page="table_page"
    :hide-default-footer=true
    :loading="table_loading"
    class="elevation-2"
>
    <template v-slot:item="props">
        <tr class="display-2"></tr>
    </template>
</v-data-table>

In my last attempt my code rendered more td elements per row than I expected, also all the columns were empty:在我最后一次尝试中,我的代码每行呈现的td元素比我预期的要多,而且所有列都是空的:

<v-data-table
    dense
    :headers="table_headers"
    :items="table_data"
    :items-per-page="table_rows"
    :page="table_page"
    :hide-default-footer=true
    :loading="table_loading"
    class="elevation-2"
>
    <template v-slot:body="{ items }">
        <tbody>
          <tr v-for="row in items">
            <td v-for="col in row"
                class="display-2">
                {{ col }}
            </td>
          </tr>
        </tbody>
    </template>
</v-data-table>

What am I doing wrong, is there a simpler way?我在做什么错,有没有更简单的方法?

BTW: I'm not a JavaScript dev.顺便说一句:我不是 JavaScript 开发人员。

If you don't have too many columns, you can use a slot for each and wrap them in a div with a custom class like below:如果您没有太多列,则可以为每个列使用一个插槽并将它们包装在一个带有自定义 class 的 div 中,如下所示:

<template v-slot:item.name="{ item }">
   <div class="customStyle">{{ item.name }}</div>
</template>

edit: In your case when you get columns dynamically, you can use body slot as you already tried but then you will have a custom styling for the whole table body.编辑:在您动态获取列的情况下,您可以像已经尝试过的那样使用主体插槽,但是您将拥有整个表格主体的自定义样式。 Give it a try as below, it is untested.试一试如下,它未经测试。

<template v-slot:body="{ items }">
  <tr v-for="item in items" :key="item.id" class="customClass">
    <td v-for="col in cols">
      {{ item.col }}
    </td>
  </tr>
</template>

'cols' should be an array of column names as string that you are fetching dynamically. 'cols' 应该是一个列名数组,作为您动态获取的字符串。

It's important you don't use the style 'scoped'.重要的是不要使用“范围”样式。

    <style>
        .my_class td{
            font-size: small!important;
            height: 0!important;
            padding: 1px!important;
        }
    </style>

The v-data-table must be the specified class 'my-class' v-data-table 必须是指定的 class 'my-class'

    <v-data-table class="my-class" :items="my_output"  >
    </v-data-table>

It works for me don't use the style scoped.它对我有用,不要使用范围内的样式。

.v-data-table > .v-data-table__wrapper > table > thead > tr > th{
     font-size: 16px !important; 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM