Table View 表格展现

1.2.0 及以上版本才有此功能

通过表格的方式来展现数据,和 table 的不同之处:

  • 数据源要求不同
    • table 的数据源需要是多行的数据,最典型的就是来自某个数据库的表
    • table view 的数据源可以来自各种固定的数据,比如单元格的某一列是来自某个变量
  • 功能不同
    • table 只能用来做数据表的展现
    • table view 除了展现复杂的报表,还能用来进行布局
  • 合并单元格方式不同
    • table 的合并单元格需要依赖数据
    • table view 的合并单元格是手动指定的,因此可以支持不规则的数据格式

基本用法

```schema: scope=”body” { “type”: “service”, “data”: { “beijing”: “20”, “tianjing”: “19” }, “body”: [ { “type”: “table-view”, “trs”: [ { “background”: “#F7F7F7”, “tds”: [ { “body”: { “type”: “tpl”, “tpl”: “地区” } }, { “body”: { “type”: “tpl”, “tpl”: “城市” } }, { “body”: { “type”: “tpl”, “tpl”: “销量” } } ] }, { “tds”: [ { “rowspan”: 2, “body”: { “type”: “tpl”, “tpl”: “华北” } }, { “body”: { “type”: “tpl”, “tpl”: “北京” } }, { “body”: { “type”: “tpl”, “tpl”: “${beijing}” } } ] }, { “tds”: [ { “body”: { “type”: “tpl”, “tpl”: “天津” } }, { “body”: { “type”: “tpl”, “tpl”: “${tianjing}” } } ] } ] } ] }

  1. 可以看到 table view 需要手动进行单元格合并,因此更适合使用可视化编辑器进行编辑。
  2. ## 设置项
  3. table view 的设置项有三层,可以分别对表格级别、行级别、单元格级别进行设置。
  4. ### 表格设置项
  5. | 属性名 | 类型 | 默认值 | 说明 |
  6. | ----------- | --------------- | ----------------------------------------------------- | ---------------- |
  7. | width | `number/string` | '100%' | |
  8. | padding | `number/string` | 'var(--TableCell-paddingY) var(--TableCell-paddingX)' | 单元格默认内间距 |
  9. | border | `boolean` | true | 是否显示边框 |
  10. | borderColor | `string` | `var(--borderColor)` | 边框颜色 |
  11. | trs | | | 参考下面的行设置 |
  12. ### 行设置
  13. | 属性名 | 类型 | 默认值 | 说明 |
  14. | ---------- | --------------- | ------ | -------------------- |
  15. | height | `number/string` | | |
  16. | background | `string` | | 行背景色 |
  17. | tds | | | 参考下面的单元格设置 |
  18. ### 单元格设置
  19. | 属性名 | 类型 | 默认值 | 说明 |
  20. | ---------- | ----------------------------------------- | -------------- | ---------------------------------------------------------------- |
  21. | background | `string` | | 单元格背景色 |
  22. | color | `string` | | 单元格文字颜色 |
  23. | bold | `boolean` | false | 单元格文字是否加粗 |
  24. | width | `number/string` | | 单元格宽度,只需要设置第一行 |
  25. | padding | `number/string` | 集成表格的设置 | 单元格内间距 |
  26. | align | `string` | `left` | 单元格内的水平对齐,可以是 `left``center``right` |
  27. | valign | `string` | `middle` | 单元格内的垂直对齐,可以是 `top``middle``bottom``baseline` |
  28. | colspan | `number` | | 单元格水平跨几行 |
  29. | rowspan | `number` | | 单元格垂直跨几列 |
  30. | body | [SchemaNode](../../docs/types/schemanode) | | 其它 amis 设置 |
  31. ### 列设置项
  32. 列设置项主要是用于控制整列的样式,比如
  33. ```schema: scope="body"
  34. {
  35. "type": "table-view",
  36. "cols": [
  37. {
  38. },
  39. {
  40. "style": {
  41. "background": "#F7F7F7"
  42. }
  43. }
  44. ],
  45. "trs": [
  46. {
  47. "tds": [
  48. {
  49. "rowspan": 2,
  50. "body": {
  51. "type": "tpl",
  52. "tpl": "华北"
  53. }
  54. },
  55. {
  56. "body": {
  57. "type": "tpl",
  58. "tpl": "北京"
  59. }
  60. }
  61. ]
  62. },
  63. {
  64. "tds": [
  65. {
  66. "body": {
  67. "type": "tpl",
  68. "tpl": "天津"
  69. }
  70. }
  71. ]
  72. }
  73. ]
  74. }
属性名 类型 默认值 说明
span number 这是个跨几列的设置项
style object 列样式

标题设置

可以通过 caption 来添加段标题文本,并通过 captionSide 来控制显示在底部还是顶部。

```schema: scope=”body” { “type”: “table-view”, “caption”: “标题”, “captionSide”: “bottom”, “trs”: [ { “tds”: [ { “rowspan”: 2, “body”: { “type”: “tpl”, “tpl”: “华北” } }, { “body”: { “type”: “tpl”, “tpl”: “北京” } }, { “body”: { “type”: “tpl”, “tpl”: “${beijing}” } } ] }, { “tds”: [ { “body”: { “type”: “tpl”, “tpl”: “天津” } }, { “body”: { “type”: “tpl”, “tpl”: “${tianjing}” } } ] } ] }

  1. ## 作为布局方法
  2. table-view 除了可以用来展现表格类型的数据,还能用来实现复杂布局效果,只需要将 `border` 隐藏就行,除了拆分单元格还能通过嵌套的方式实现布局,比如:
  3. ```schema: scope="body"
  4. {
  5. "type": "table-view",
  6. "border": false,
  7. "trs": [
  8. {
  9. "background": "#feceea",
  10. "tds": [
  11. {
  12. "colspan": 2,
  13. "align": "center",
  14. "body": {
  15. "type": "tpl",
  16. "tpl": "头部"
  17. }
  18. }
  19. ]
  20. },
  21. {
  22. "tds": [
  23. {
  24. "rowspan": 2,
  25. "background": "#fef1d2",
  26. "width": 200,
  27. "body": {
  28. "type": "tpl",
  29. "tpl": "侧边"
  30. }
  31. },
  32. {
  33. "align": "center",
  34. "background": "#a9fdd8",
  35. "body": {
  36. "type": "tpl",
  37. "tpl": "右上"
  38. }
  39. }
  40. ]
  41. },
  42. {
  43. "height": 200,
  44. "tds": [
  45. {
  46. "align": "center",
  47. "background": "#d7f8ff",
  48. "body": {
  49. "type": "table-view",
  50. "border": false,
  51. "trs": [
  52. {
  53. "tds": [
  54. {
  55. "align": "center",
  56. "body": {
  57. "type": "tpl",
  58. "tpl": "栏目 1"
  59. }
  60. },
  61. {
  62. "align": "center",
  63. "body": {
  64. "type": "tpl",
  65. "tpl": "栏目 2"
  66. }
  67. },
  68. {
  69. "align": "center",
  70. "body": {
  71. "type": "tpl",
  72. "tpl": "栏目 3"
  73. }
  74. }
  75. ]
  76. }
  77. ]
  78. }
  79. }
  80. ]
  81. },
  82. {
  83. "tds": [
  84. {
  85. "colspan": 2,
  86. "align": "center",
  87. "background": "#cec5fa",
  88. "body": {
  89. "type": "tpl",
  90. "tpl": "底部"
  91. }
  92. }
  93. ]
  94. }
  95. ]
  96. }