表格中指定一列,内容按照数据显示为进度图,并显示百分比文字。如何在表格组件中实现这个效果?
直接上效果:
我是用纯属html写的,方式有可多,可以用js,vue,css看你的项目,解决具体用什么,但原理一样。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.bg{background:#78D96D;line-height: 40px;text-align: right;}
</style>
</head>
<body>
<table border="1" width="30%">
<tr><td>
<div class="bg" style="width: 50%">50%</div>
</td></tr>
<tr><td>
<div class="bg" style="width: 100%">100%</div>
</td></tr>
<tr><td>
<div class="bg" style="width: 10%">10%</div>
</td></tr>
<tr><td><div class="bg" style="width: 33%">33%</div></td></tr>
</table>
</body>
</html>
使用ui框架实现的吗,一般table组件都支持自定义列模板,以iview为例
<Table border :columns="columns12" :data="data6">
<template slot-scope="{ row }" slot="name">
<div>进度</div>
</template>
</Table>
columns12: [
{
title: 'Name',
slot: 'name'
},
]
这里使用开源表格组件VTable来实现这个功能。可以通过在columns
中将cellType
设置为progressbar
,指定该列为progressbar
类型(进度图)单元格;通过配置columns
中的style
,可以配置进度图的样式:
{
field: "value",
title: "progress",
cellType: "progressbar",
style: {
barColor: DEFAULT_BAR_COLOR,
barBgColor: "#ddd",
barHeight: 30,
barBottom: 4,
textAlign: "right"
},
fieldFormat: (data: any) => {
return data.value + "%";
},
width: 250
}
在style
中:
通过fieldFormat
,可以修改单元格中的文字内容,显示百分比文字。
通过修改barType
,可以将进度图改为简单的柱图,可以用来显示同时存在正负数据的内容。
代码参考 Code Example
const columns = [
{
field: "id",
title: "ID",
width: 80
},
{
field: "value",
title: "progress",
cellType: "progressbar",
style: {
barColor: DEFAULT_BAR_COLOR,
barBgColor: "#ddd",
barHeight: 30,
barBottom: 4,
textAlign: "right"
},
fieldFormat: (data: any) => {
return data.value + "%";
},
width: 250
},
{
field: "value1",
title: "axis",
cellType: "progressbar",
barType: "negative",
min: -50,
max: 50,
style: {
barHeight: 30,
barBottom: 4,
textAlign: "right"
},
width: 250
}
];
const option: TYPES.ListTableConstructorOptions = {
records,
columns
};
在线效果参考:https://codesandbox.io/s/vtable-progress-bar-l69jtk
进度图demo:https://visactor.io/vtable/demo/cell-type/progressbar
进度图教程:https://visactor.io/vtable/guide/cell_type/progressbar
10 回答11.2k 阅读
5 回答4.9k 阅读✓ 已解决
4 回答3.2k 阅读✓ 已解决
2 回答2.8k 阅读✓ 已解决
3 回答5.2k 阅读✓ 已解决
1 回答3.3k 阅读✓ 已解决
3 回答2.4k 阅读✓ 已解决
可以使用背景径向渐变做到这个效果。