207 lines
5.3 KiB
Vue
207 lines
5.3 KiB
Vue
<template>
|
|
<div class="view-kv-table-wrap">
|
|
<table class="view-kv-table" :class="`view-kv-table--${mode}`">
|
|
<tbody>
|
|
<template v-if="mode === 'pair'">
|
|
<tr
|
|
v-for="field in fields"
|
|
:key="field.label"
|
|
:class="{ 'row-long': isLong(field) }"
|
|
>
|
|
<th>{{ field.label }}</th>
|
|
<td>
|
|
<ViewKvTableCell :field="field" />
|
|
</td>
|
|
</tr>
|
|
</template>
|
|
<template v-else-if="mode === 'dual'">
|
|
<tr
|
|
v-for="(row, idx) in dualRows"
|
|
:key="dualRowKey(row, idx)"
|
|
:class="{ 'row-long': row.type === 'long' }"
|
|
>
|
|
<template v-if="row.type === 'long'">
|
|
<th>{{ row.field.label }}</th>
|
|
<td colspan="3">
|
|
<ViewKvTableCell :field="row.field" />
|
|
</td>
|
|
</template>
|
|
<template v-else>
|
|
<th>{{ row.left.label }}</th>
|
|
<td>
|
|
<ViewKvTableCell :field="row.left" />
|
|
</td>
|
|
<template v-if="row.right">
|
|
<th>{{ row.right.label }}</th>
|
|
<td>
|
|
<ViewKvTableCell :field="row.right" />
|
|
</td>
|
|
</template>
|
|
<template v-else>
|
|
<th></th>
|
|
<td></td>
|
|
</template>
|
|
</template>
|
|
</tr>
|
|
</template>
|
|
<template v-else>
|
|
<tr
|
|
v-for="(row, idx) in tripleRows"
|
|
:key="tripleRowKey(row, idx)"
|
|
:class="{ 'row-long': row.type === 'long' }"
|
|
>
|
|
<template v-if="row.type === 'long'">
|
|
<th>{{ row.field.label }}</th>
|
|
<td colspan="5">
|
|
<ViewKvTableCell :field="row.field" />
|
|
</td>
|
|
</template>
|
|
<template v-else>
|
|
<template v-for="slot in 3" :key="slot">
|
|
<template v-if="row.fields[slot - 1]">
|
|
<th>{{ row.fields[slot - 1].label }}</th>
|
|
<td>
|
|
<ViewKvTableCell :field="row.fields[slot - 1]" />
|
|
</td>
|
|
</template>
|
|
<template v-else>
|
|
<th></th>
|
|
<td></td>
|
|
</template>
|
|
</template>
|
|
</template>
|
|
</tr>
|
|
</template>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</template>
|
|
|
|
<script setup lang="ts">
|
|
import { computed } from 'vue'
|
|
import type { ViewField } from './types'
|
|
import { formatViewValue } from './types'
|
|
import ViewKvTableCell from './ViewKvTableCell.vue'
|
|
import {
|
|
buildViewKvTableDualRows,
|
|
buildViewKvTableTripleRows,
|
|
getViewFieldDisplayText,
|
|
isViewFieldFullSpan,
|
|
isViewFieldLong,
|
|
type ViewKvTableRow,
|
|
type ViewKvTableTripleRow
|
|
} from '@/utils/view-field-layout'
|
|
|
|
const props = withDefaults(
|
|
defineProps<{
|
|
fields: ViewField[]
|
|
/** pair:一行一组;dual:一行两组(四列);triple:一行三组(六列) */
|
|
mode?: 'pair' | 'dual' | 'triple'
|
|
}>(),
|
|
{
|
|
mode: 'pair'
|
|
}
|
|
)
|
|
|
|
const dualRows = computed(() => buildViewKvTableDualRows(props.fields, formatViewValue))
|
|
const tripleRows = computed(() => buildViewKvTableTripleRows(props.fields, formatViewValue))
|
|
|
|
function isLong(field: ViewField) {
|
|
const text = getViewFieldDisplayText(field, formatViewValue)
|
|
return isViewFieldLong(field, text) || isViewFieldFullSpan(field, text)
|
|
}
|
|
|
|
function dualRowKey(row: ViewKvTableRow, idx: number) {
|
|
if (row.type === 'long') return row.field.label
|
|
return `${row.left.label}-${row.right?.label ?? idx}`
|
|
}
|
|
|
|
function tripleRowKey(row: ViewKvTableTripleRow, idx: number) {
|
|
if (row.type === 'long') return row.field.label
|
|
return row.fields.map((f) => f.label).join('-') || String(idx)
|
|
}
|
|
</script>
|
|
|
|
<style scoped lang="scss">
|
|
.view-kv-table-wrap {
|
|
border: 1px solid var(--border, #d9e8e2);
|
|
border-radius: 8px;
|
|
overflow: hidden;
|
|
background: #fff;
|
|
}
|
|
|
|
.view-kv-table {
|
|
width: 100%;
|
|
border-collapse: collapse;
|
|
font-size: 13px;
|
|
table-layout: fixed;
|
|
|
|
th {
|
|
padding: 10px 12px;
|
|
background: var(--table-head, #f8fafc);
|
|
color: var(--text-muted, #64748b);
|
|
font-size: 11px;
|
|
font-weight: 500;
|
|
text-align: left;
|
|
vertical-align: top;
|
|
border-bottom: 1px solid #eef2f6;
|
|
border-right: 1px solid #eef2f6;
|
|
white-space: normal;
|
|
word-break: break-word;
|
|
overflow-wrap: anywhere;
|
|
line-height: 1.4;
|
|
}
|
|
|
|
td {
|
|
padding: 10px 14px;
|
|
font-weight: 600;
|
|
color: var(--text, #1e293b);
|
|
border-bottom: 1px solid #eef2f6;
|
|
vertical-align: top;
|
|
word-break: break-word;
|
|
line-height: 1.45;
|
|
}
|
|
|
|
tr:last-child th,
|
|
tr:last-child td {
|
|
border-bottom: none;
|
|
}
|
|
}
|
|
|
|
.view-kv-table--pair th {
|
|
width: 128px;
|
|
}
|
|
|
|
.view-kv-table--dual th {
|
|
width: 12%;
|
|
}
|
|
|
|
.view-kv-table--dual td {
|
|
width: 38%;
|
|
}
|
|
|
|
.view-kv-table--dual tr.row-long th {
|
|
width: 12%;
|
|
}
|
|
|
|
.view-kv-table--dual tr.row-long td {
|
|
width: auto;
|
|
}
|
|
|
|
.view-kv-table--triple th {
|
|
width: 10%;
|
|
}
|
|
|
|
.view-kv-table--triple td {
|
|
width: 23.33%;
|
|
}
|
|
|
|
.view-kv-table--triple tr.row-long th {
|
|
width: 10%;
|
|
}
|
|
|
|
.view-kv-table--triple tr.row-long td {
|
|
width: auto;
|
|
}
|
|
</style>
|