目录

如何对SelectDialog的单选项进行增减

问题现象

如图所示,纯列表弹出框提供如下的示意图,如何自定义单选项的数量,使得弹窗属性title的内容是从string数组foreach遍历获取?

问题效果预览:

效果预览

背景知识

SelectDialog:选择类弹出框,弹框中以列表或网格的形式提供可选的内容。

解决方案

在aboutToAppear中使用for循环动态初始化SelectDialog的radioContent。

import { SelectDialog } from '@kit.ArkUI';

@Entry
@Component
struct SelectDialogDemo {
  // title数组
  titleList: string[] = [];
  // SelectDialog的radioContent进行初始化
  radioContent: Array<SheetInfo> = [];
  // 设置默认选中radio的index
  radioIndex = 0;
  dialogControllerList: CustomDialogController = new CustomDialogController({
    builder: SelectDialog({
      title: '文本标题',
      selectedIndex: this.radioIndex,
      confirm: {
        value: '取消',
        action: () => {
        },
      },
      // 将初始化后的radioContent赋值给SelectDialog的radioContent属性
      radioContent: this.radioContent
    }),
  });

  build() {
    Row() {
      Stack() {
        Column() {
          Button('纯列表弹出框')
            .width(96)
            .height(40)
            .onClick(() => {
              this.titleList.push('文本');
              this.titleList.push('文本文本');
              this.titleList.push('文本文本文本');
              this.titleList.push('文本文本文本文本');
              this.titleList.push('文本文本文本文本文本');
              this.titleList.push('文本文本文本文本文本文本');

              // 赋值给radioContent
              this.titleList.forEach((value: string, index: number) => {
                let sheetInfo: SheetInfo = {
                  title: value,
                  action: () => {
                    this.radioIndex = index;
                  }
                };
                this.radioContent.push(sheetInfo);
              });
              this.dialogControllerList.open();
            });
        }.margin({ bottom: 300 });
      }
      .align(Alignment.Bottom)
      .width('100%')
      .height('100%');
    }
    .backgroundImageSize({ width: '100%', height: '100%' })
    .height('100%');
  }
}