vue.js - 如何使用 PrimeVue 的 DataTable 组件为每个表条目添加链接?
问题描述
PrimeVue 库默认不支持此功能。该表目前看起来像这样。
这是我的产品系列:
products: [
{brand: "Volkswagen", year: 2012, color: "Orange", vin: "dsad231ff", url: "linkToSite"},
{brand: "Audi", year: 2011, color: "Black", vin: "gwregre345", url: "linkToSite"},
{brand: "Renault", year: 2005, color: "Gray", "vin": "h354htr", url: "linkToSite"},
]
这是默认的 PrimeVue DT 色谱柱设置:
columns: [
{field: 'brand', header: 'Brand'},
{field: 'year', header: 'Year'},
{field: 'color', header: 'Color'},
{field: 'vin', header: 'Vin'}
]
这是我的模板代码:
<DataTable :value="products">
<Column v-for="col of columns" :field="col.field" :header="col.header" :key="col.field"></Column>
</DataTable>
解决方案
使用 a<template #body="slotProps">
自定义 a 中单元格的外观<Column>
。
<DataTable ...>
<Column field="brand" header="Brand">
<template #body="slotProps">
<a :href="slotProps.data.url" v-text="slotProps.data.brand" />
</template>
</Column>
...
</DataTable>
文档中的更多示例。
在你的 v-for 里面(我没有测试过这个,但它应该可以工作):
<Column v-for="col of columns"
:field="col.field"
:header="col.header"
:key="col.field">
<template #body="slotProps" v-if="col.field === 'brand'">
<a :href="slotProps.data.url" v-text="slotProps.data.brand" />
</template>
</Column>
如果由于某种原因它不起作用,请使用您当前拥有的代码和box.io 或类似的东西创建一个mcve,我会看看为什么它不适合您。
推荐阅读
- python - 使用 selenium 和 python 从 DOM 中查找第二个元素
- python - Numpy.array(500x50),我如何跨行连接,使其变为(500x1)
- java - 在 2d 数组中循环,在 java 中已经有 2 个循环
- r - 如何在 sparklyr 管道中添加自定义函数
- python - 逃脱的字符。OperationalError:语法错误
- r - R:如何在函数中使用数据框变量作为参数?
- java - Lambda 表达式对象映射问题
- uwp - UWP Currency Formatter 强制使用符号 $ 而不是 USD
- azure - 我是否必须在 powershell runbook (azure) 中导入模块?
- sql - 如何在 Oracle 中显示“缺失”小时数