英文:
How to pass a fixed width to a column in a Vuetify HTML table?
问题
我正在使用Vuetify表格,根据配置,我想设置固定的列宽。我认为将配置中的宽度分配给width
CSS属性应该可以。
第一个示例(Playground)应显示一个总宽度为350px的表格。当查看示例时,您会发现表格占用整个屏幕宽度。
<template>
<v-table>
<thead>
<tr>
<th style="width: 300px">Col 1</th>
<th style="width: 50px">Col 2</th>
</tr>
</thead>
<tbody>
<tr>
<td>Item 1</td>
<td>Item 2</td>
</tr>
</tbody>
</v-table>
</template>
第二个示例(Playground)应显示一个总宽度为6300px的表格。第一列应该太大以适应屏幕,因此应该有一个水平滚动条。但是表格会将第一列缩小到较小的宽度,以适应屏幕宽度。
<template>
<v-table>
<thead>
<tr>
<th style="width: 6000px">Col 1</th>
<th style="width: 300px">Col 2</th>
</tr>
</thead>
<tbody>
<tr>
<td>Item 1</td>
<td>Item 2</td>
</tr>
</tbody>
</v-table>
</template>
如何禁用这种行为?表格应该具有精确的列宽(基于配置值)。我尝试了以下方式,但不幸的是没有解决问题:
<v-table style="min-width: none; max-width: none">
没有变化<v-table style="overflow: auto">
没有变化<v-table style="width: max-content">
如果表格小于屏幕尺寸,则有效,但会防止滚动条出现<v-table style="width: min-content">
太小<v-table style="width: auto">
没有变化
您有任何想法吗?
英文:
I'm using a Vuetify table and based on a configuration I want to set fixed column widths. I thought it would be fine to assign the width from the configuration to the width
CSS attribute.
The first example ( Playground ) should display a table with a total width of 350px. When having a look at the playground you will see that the table takes the whole screen width instead.
<template>
<v-table>
<thead>
<tr>
<th style="width: 300px">Col 1</th>
<th style="width: 50px">Col 2</th>
</tr>
</thead>
<tbody>
<tr>
<td>Item 1</td>
<td>Item 2</td>
</tr>
</tbody>
</v-table>
</template>
The second example ( Playground ) should display a table with a total width of 6300px. The first column should be too big for the screen so there should be a horizontal scrollbar. Instead the table shrinks the first column down to a smaller width so the whole table fits the screen width.
<template>
<v-table>
<thead>
<tr>
<th style="width: 6000px">Col 1</th>
<th style="width: 300px">Col 2</th>
</tr>
</thead>
<tbody>
<tr>
<td>Item 1</td>
<td>Item 2</td>
</tr>
</tbody>
</v-table>
</template>
How can I disable this behaviour? The table should have exact columns widths ( based on the configuration values ). I tried
<v-table style="min-width: none; max-width: none">
no changes<v-table style="overflow: auto">
no changes<v-table style="width: max-content">
works if table is smaller than screen size, prevents scrollbar<v-table style="width: min-content">
is too small<v-table style="width: auto">
no changes
but unfortunately that didn't fix it. Do you have any ideas?
答案1
得分: 1
你可以使用CSS来实现这个效果。首先,你必须为<table>
标签使用以下CSS:
.v-table__wrapper table {
table-layout: fixed;
width: 0 !important;
}
编辑:
根据你在评论中提供的新示例,涉及到colspan和rowspan,你需要明确设置所有列的固定宽度。
旧回答:
然后,你可以使用nth-child()
CSS选择器来定义列的宽度:
.v-table__wrapper table th:nth-child(1),
.v-table__wrapper table td:nth-child(1) {
width: 300px;
}
.v-table__wrapper table th:nth-child(2),
.v-table__wrapper table td:nth-child(2) {
width: 50px;
}
英文:
You can use CSS to achieve this. First you must use this CSS for the <table>
tag:
.v-table__wrapper table {
table-layout: fixed;
width: 0 !important;
}
EDIT:
From the new example you provided in the comment, which involves colspan and rowspan, you need to set all the fixed width explicitly.
Old answer:
And then you can define the width for the columns using nth-child()
CSS selector:
.v-table__wrapper table th:nth-child(1),
.v-table__wrapper table td:nth-child(1) {
width: 300px;
}
.v-table__wrapper table th:nth-child(2),
.v-table__wrapper table td:nth-child(2) {
width: 50px;
}
答案2
得分: 1
以下是翻译好的部分:
要实现这个要求,只需调整表格和行元素的styles
(样式)。
当前样式(默认):
.v-table > .v-table__wrapper > table {
width: 100%;
border-spacing: 0;
}
根据要求修改的样式**:**
.v-table > .v-table__wrapper > table {
width: 0;
border-spacing: 0;
table-layout: fixed;
text-wrap: nowrap;
}
.v-table__wrapper > table > tbody > tr > td {
overflow: auto;
}
英文:
You can simply achieve this requirement by adjusting the styles
for table and the row elements.
Current styles (Default) :
.v-table > .v-table__wrapper > table {
width: 100%;
border-spacing: 0;
}
Modified style as per the requirement :
.v-table > .v-table__wrapper > table {
width: 0;
border-spacing: 0;
table-layout: fixed;
text-wrap: nowrap;
}
.v-table__wrapper > table > tbody > tr > td {
overflow: auto;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论