vue实现链路表格
Vue 实现链路表格的方法
链路表格通常用于展示层级关系或依赖关系的数据,例如工作流、依赖链等。以下是几种常见的实现方式:
使用递归组件实现树形表格
递归组件适合展示具有层级结构的数据:
<template>
<table>
<thead>
<tr>
<th>名称</th>
<th>值</th>
</tr>
</thead>
<tbody>
<tree-row v-for="(item, index) in treeData" :key="index" :item="item" />
</tbody>
</table>
</template>
<script>
import TreeRow from './TreeRow.vue'
export default {
components: { TreeRow },
data() {
return {
treeData: [
{
name: '节点1',
value: '值1',
children: [
{ name: '子节点1', value: '子值1' }
]
}
]
}
}
}
</script>
递归组件 TreeRow.vue:
<template>
<tr>
<td>{{ item.name }}</td>
<td>{{ item.value }}</td>
</tr>
<tr v-if="item.children" v-for="child in item.children" :key="child.name">
<td style="padding-left: 20px">└─ {{ child.name }}</td>
<td>{{ child.value }}</td>
</tr>
</template>
使用第三方库实现复杂链路
对于更复杂的链路关系,可以使用专门的可视化库:

-
安装依赖:
npm install vis-network -
实现代码:
<template> <div ref="network" style="width: 100%; height: 500px"></div> </template>
export default { mounted() { this.renderNetwork() }, methods: { renderNetwork() { const nodes = new DataSet([ { id: 1, label: '节点1' }, { id: 2, label: '节点2' }, { id: 3, label: '节点3' } ])

const edges = new DataSet([
{ from: 1, to: 2, label: '依赖' },
{ from: 2, to: 3, label: '依赖' }
])
const container = this.$refs.network
const data = { nodes, edges }
const options = {
edges: {
arrows: 'to'
}
}
new Network(container, data, options)
}
} }
```使用CSS实现简单链路效果
对于简单的链路展示,可以通过纯CSS实现:
<template>
<div class="link-table">
<div v-for="(item, index) in items" :key="index" class="link-item">
<div class="node">{{ item.name }}</div>
<div v-if="index < items.length - 1" class="connector">→</div>
</div>
</div>
</template>
<script>
export default {
data() {
return {
items: [
{ name: '步骤1' },
{ name: '步骤2' },
{ name: '步骤3' }
]
}
}
}
</script>
<style>
.link-table {
display: flex;
align-items: center;
}
.link-item {
display: flex;
align-items: center;
}
.node {
padding: 8px 16px;
background: #eee;
border-radius: 4px;
}
.connector {
margin: 0 10px;
color: #999;
}
</style>
响应式链路表格实现
结合Vue的响应式特性,可以实现动态更新的链路表格:
<template>
<div>
<button @click="addNode">添加节点</button>
<table>
<tr v-for="(node, index) in nodes" :key="index">
<td>{{ node.name }}</td>
<td>
<span v-if="index > 0">← 来自 {{ nodes[index-1].name }}</span>
<span v-if="index < nodes.length - 1">→ 到 {{ nodes[index+1].name }}</span>
</td>
</tr>
</table>
</div>
</template>
<script>
export default {
data() {
return {
nodes: [
{ name: '初始节点' }
],
counter: 1
}
},
methods: {
addNode() {
this.nodes.push({
name: `节点${this.counter++}`
})
}
}
}
</script>
以上方法可以根据实际需求选择或组合使用,递归组件适合树形结构,第三方库适合复杂可视化,CSS方案适合简单链路展示,响应式方案适合需要动态更新的场景。






