Implementing a fully bordered Vuetify HTML table: a step-by-step guide
P粉373596828
2023-09-03 18:46:38
<p>I have a Vue app using Vuetify tables, which does not support border tables (but see https://github.com/vuetifyjs/vuetify/issues/16336). That's why I tried using my own CSS to add the missing border. </p>
<p>Give the following example (copy link)</p>
<pre class="brush:php;toolbar:false;"><template>
<v-app>
<v-main>
<v-table>
<thead>
<tr>
<th>Col 1</th>
<th>Col 2</th>
</tr>
</thead>
<tbody>
<tr v-for="(row, rowIndex) in tableMatrix" :key="rowIndex">
<template v-for="(cell, columnIndex) in row" :key="columnIndex">
<td v-if="cell.isCoveredByPreviousCell" class="d-none" />
<td v-else :rowspan="cell.rowspan">
<template v-if="cell.content">
{{ cell.content }}
</template>
</td>
</template>
</tr>
</tbody>
</v-table>
</v-main>
</v-app>
</template>
<script setup lang="ts">
import { ref, Ref } from 'vue';
interface Cell { isCoveredByPreviousCell: boolean; rowspan: number; content?: string; }
type TableMatrix = Cell[][];
const childCell: Cell = { isCoveredByPreviousCell: false, rowspan: 1, content: "cell with rowspan 1" };
const tableMatrix: Ref<TableMatrix> = ref([
[{ isCoveredByPreviousCell: false, rowspan: 2, content: "cell with rowspan 2" },{ ...childCell }],
[{ isCoveredByPreviousCell: true, rowspan: 1, content: "covered by parent" },{ ...childCell }],
[{ ...childCell },{ ...childCell }],
[{ ...childCell }, { isCoveredByPreviousCell: false, rowspan: 2, content: "cell with rowspan 2" }],
[{ ...childCell }, { isCoveredByPreviousCell: true, rowspan: 1, content: "covered by parent" }],
[{ isCoveredByPreviousCell: false, rowspan: 2, content: "cell with rowspan 2" },{ ...childCell }],
[{ isCoveredByPreviousCell: true, rowspan: 1, content: "covered by parent" },{ ...childCell }],
])
</script>
<style>
table { border: 1px solid #e6e6e6; }
table th { border-top: 1px solid #e6e6e6; }
table th th { border-left: 1px solid #e6e6e6; }
table td td { border-left: 1px solid #e6e6e6; }
</style></pre>
<p>If the last cell's row span is greater than 1, you can see that it has a thicker border</p>
<p>Does anyone know which CSS "rule" is missing to fix table borders in this case? </p>
Use slot=item and then apply the style based on the scope slot.
Here is an example:
Show code snippet
Use slot=item.name.
Just add the
border-collapse:collapse;
attribute to the table.