目录

关于绘制表格内容的问题

问题现象

如何绘制单元格内容可能含有子表格的表格?表格需要有分隔线和圆角,不绘制子表格的外框线。

效果预览

背景知识

解决方案

表格的实现可以使用Flex容器,设置为多行列排列;外层表格Flex容器和子组件按需要分别设置border边框样式,实现表格分隔线,且Flex容器设置clip属性,避免子组件内容超出范围;子表格可以使用clipShape属性裁去外框线。

外层表格数据源的类型调整为Array<Object>,或Object[],使数组内可以插入混合类型的数据,包含Array数组;通过typeof判断元素的类型,并根据不同类型渲染表格的单元格内容。

具体实现如下:

  1. 使用Flex容器横向排列,并设置为多行列排列与交叉轴方向拉伸填充,实现内容高度不一致时统一拉伸至当前行最大高度;由于表格同列宽度需保持一致,可以根据表格需要的列数为子组件设置宽度;通过border属性设置子组件边框样式,实现表格的分割线。
    Flex({
      direction: FlexDirection.Row,
      wrap: FlexWrap.Wrap,
      justifyContent: FlexAlign.Center,
      alignItems: ItemAlign.Stretch
    }) {
      ForEach(this.itemsArr, (item: ItemAttr) => {
        if (typeof item.content === 'string') {
          Text(item.content)
            .fontSize('16fp')
            .width(item.width)
            .padding(10)
            .textAlign(item.isHeader ? TextAlign.Center : TextAlign.Start)
            .backgroundColor(item.backgroundColor)
            .border({
              width: 1,
              color: '#eccc68'
            });
        } else if (typeof item.content === 'object') {
          MultiItem({ itemArr: item.content as string[] })
            .width(item.width)
            .padding(10)
            .border({
              width: 1,
              color: '#eccc68'
            });
        }
      });
    }
    .height('auto')
    .border({
      width: 1,
      color: '#eccc68',
      radius: 20
    })
    .clip(true);
  2. Flex容器单独设置边框圆角borderRadius,且增加clip属性,使子组件超出容器的内容被裁剪,实现圆角表格。
  3. 通过clipShape属性,指定裁剪形状为矩形,且裁剪位置从表格外框线内开始,通过调整裁剪的尺寸,将外框线裁去,实现无外框的表格。
  4. 将数据源的类型调整为Array<Object>或Object[],通过typeof判断元素的类型,并根据不同类型渲染表格的单元格内容。
完整示例参考如下:
import { RectShape } from '@kit.ArkUI';

const widthArr: Length[] = ['20%', '20%', '60%'];

class ItemAttr {
  content: Object = '';
  width: Length = 'auto';
  backgroundColor: ResourceColor = Color.White;
  isHeader: boolean = false;

  constructor(content: Object, width?: Length, bgColor?: ResourceColor, isHeader?: boolean) {
    this.content = content;
    this.width = width ?? 'auto';
    this.backgroundColor = bgColor ?? Color.White;
    this.isHeader = isHeader ?? false;
  }
}

@Entry
@Component
struct ContainerSheet {
  headerContentArr: string[] = ['title one', 'title two', 'title three'];
  itemContentArr: Object[] = ['Class A', 'Item One',
    'Introductory: This is an example of a table implementation using a Flex container.',
    'Class B', 'Item Two',
    ['item 1-1', 'item 1-2', 'item 1-3', 'item 2-1', 'item 2-2', 'item 2-3']
  ];
  headerArr: ItemAttr[] = [];
  @State itemsArr: ItemAttr[] = [];

  aboutToAppear(): void {
    for (let i = 0; i < this.headerContentArr.length; i++) {
      this.headerArr.push(new ItemAttr(this.headerContentArr[i], widthArr[i], '#eccc68', true));
    }

    for (let i = 0; i < this.itemContentArr.length; i++) {
      let idx = i % this.headerContentArr.length;
      this.itemsArr.push(new ItemAttr(this.itemContentArr[i], widthArr[idx]));
    }

    this.itemsArr = this.headerArr.concat(this.itemsArr);
  }

  build() {
    Flex({
      direction: FlexDirection.Row,
      wrap: FlexWrap.Wrap,
      justifyContent: FlexAlign.Center,
      alignItems: ItemAlign.Stretch
    }) {
      ForEach(this.itemsArr, (item: ItemAttr) => {
        if (typeof item.content === 'string') {
          Text(item.content)
            .fontSize('16fp')
            .width(item.width)
            .padding(10)
            .textAlign(item.isHeader ? TextAlign.Center : TextAlign.Start)
            .backgroundColor(item.backgroundColor)
            .border({
              width: 1,
              color: '#eccc68'
            });
        } else if (typeof item.content === 'object') {
          MultiItem({ itemArr: item.content as string[] })
            .width(item.width)
            .padding(10)
            .border({
              width: 1,
              color: '#eccc68'
            });
        }
      });
    }
    .height('auto')
    .border({
      width: 1,
      color: '#eccc68',
      radius: 20
    })
    .clip(true);

  }
}

@Component
struct MultiItem {
  @State itemArr: string[] = [];
  @State containerHeight: number = 0;
  @State containerWidth: number = 0;

  build() {
    Column() {
      Flex({
        direction: FlexDirection.Row,
        wrap: FlexWrap.Wrap,
        justifyContent: FlexAlign.Center,
        alignItems: ItemAlign.Stretch
      }) {
        ForEach(this.itemArr, (item: string, index: number) => {
          Column() {
            Text(item)
              .fontSize('16fp')
              .width('100%')
              .textAlign(TextAlign.Center);
          }
          .width(index % 3 === 2 ? '40%' : '30%')
          .justifyContent(FlexAlign.Center)
          .border({
            width: 1,
            color: '#f1c40f'

          });
        });
      }
      .width('100%')
      .onAreaChange((oldValue, newValue) => {
        this.containerHeight = newValue.height as number;
        this.containerWidth = newValue.width as number;
      });
    }
    .alignItems(HorizontalAlign.Center)
    .clip(true)
    .clipShape(new RectShape({
      height: this.containerHeight - 2,
      width: this.containerWidth - 2
    }).position({ x: 1, y: 1 }));
  }
}

总结