自学鸿蒙HarmonyOS的ArkTS语言<九>自定义弹窗组件CustomDialog及二次封装自定义弹窗

一、自定义弹窗
@CustomDialog
struct CustomDialogBuilder {
  controller: CustomDialogController = new CustomDialogController({ // 注意写法
    builder: CustomDialogBuilder({})
  })

  // controller: CustomDialogController // 这种预览会报错
  cancel?: () => void
  confirm?: () => void
  text?: string

  build() {
    Column() {
      Text(this.text)
        .padding(20)
      Flex({ justifyContent: FlexAlign.SpaceAround }) {
        Button('取消')
          .buttonStyle(ButtonStyleMode.TEXTUAL)
          .onClick(() => {
            this.controller.close() // 关闭弹窗
            if (this.cancel) {
              this.cancel()
            }
          })
        Button('确定')
          .buttonStyle(ButtonStyleMode.TEXTUAL)
          .onClick(() => {
            if (this.confirm) {
              this.confirm()
            }
        })
      }.padding(10)

    }
  }
}

@Entry
@Component
struct Index6 {
  @State text: string = '我是来自父组件的内容'
  dialogController: CustomDialogController | null = new CustomDialogController({
    // builder->自定义弹窗内容构造器
    builder: CustomDialogBuilder({
      cancel: () => { this.onCancel() }, // 不能写成 () => this.onCancel()
      confirm: () => { this.onAccept() },
      text: this.text
    }),
    alignment: DialogAlignment.Center,
    autoCancel: true,
    cancel: () => {
      console.log('点击遮罩层')
    },
    onWillDismiss: (res: DismissDialogAction) => { // 有了onWillDismiss不会触发cancel
      console.log('onWillDismiss:', JSON.stringify(res))
    }
    // ... 还可以设置backgroundColor, cornerRadius等
  })

  // 在自定义组件即将析构销毁时将dialogController置空
  aboutToDisappear() {
    this.dialogController = null // 将dialogController置空
  }

  onCancel() {
    console.log('我是父组件中的onCancel')
  }

  onAccept() {
    console.log('我是父组件中的onAccept')
  }

  build() {
    Button('点击我可以获取一个自定义弹窗')
      .onClick(() => {
        if (this.dialogController !== null) { // dialogController有null类型,必须判断否则报错
          this.text = '我是来自父组件中的内容2'
          this.dialogController.open() // 打开弹窗
        }
      })
  }
}

在这里插入图片描述

注意点:
1、自定义弹窗用 @CustomDialog 装饰
2、和普通组件不一样的是自定义弹窗是通过实例化CustomDialogController类显示的,其内容是builder参数(自定义弹窗内容构造器)
3、构造器里面传参的写法
在这里插入图片描述
4、自定义函数中controller用下面这种方式,预览会报错 ??? 不明白
在这里插入图片描述
在这里插入图片描述

二、二次封装自定义弹窗组件
@CustomDialog
struct CustomDialogBuilder {
  @Link visible: boolean
  controller: CustomDialogController = new CustomDialogController({ // 注意写法
    builder: CustomDialogBuilder({
      visible: $visible
    })
  })

  // controller: CustomDialogController // 这种预览会报错
  cancel?: () => void
  confirm?: () => void
  text?: string

  build() {
    Column() {
      Text(this.text)
        .padding(20)
      Flex({ justifyContent: FlexAlign.SpaceAround }) {
        Button('取消')
          .buttonStyle(ButtonStyleMode.TEXTUAL)
          .onClick(() => {
            this.visible = false
            this.controller.close() // 关闭弹窗
            if (this.cancel) {
              this.cancel()
            }
          })
        Button('确定')
          .buttonStyle(ButtonStyleMode.TEXTUAL)
          .onClick(() => {
            this.visible = false
            if (this.confirm) {
              this.confirm()
            }
        })
      }.padding(10)

    }
  }
}

// 二次封装
@Component
struct Dialog {
  @Watch('onChange') @Link visible: boolean // 必须在@link前,否则报错
  cancel?: () => void
  confirm?: () => void
  text?: string

  private controller = new CustomDialogController({
    builder: CustomDialogBuilder({
      cancel: this.cancel,
      confirm: this.confirm,
      visible: $visible,
      text: this.text
    }),
    alignment: DialogAlignment.Center,
    autoCancel: true,
    cancel: () => {
      console.log('点击遮罩层')
    },
    onWillDismiss: (res: DismissDialogAction) => {
      console.log('onWillDismiss:', JSON.stringify(res))
    }
  })

  onChange() {
    console.log('监听visible:', this.visible)
    if (this.visible) {
      this.controller.open()
    } else {
      this.controller.close()
    }
  }

  build() {
  }
}

@Entry
@Component
struct Index6 {
  @State visible: boolean = false

  onCancel() {
    console.log('我是来自最顶层的onCancel')
  }

  onConfirm() {
    console.log('我是来自最顶层的onConfirm')
  }

  build() {
    Column() {
      Button('点击我可以获取一个自定义弹窗')
        .onClick(() => {
          this.visible = !this.visible
          console.log(this.visible+'')
        })

      Dialog({
        visible: this.visible,
        cancel: () => {
          console.log('我是来自最顶层的oncancel')
        },
        confirm: () => {
          console.log('我是来自最顶层的onConfirm')
        },
        text: '我是来自最顶层的文字',

      })
    }
  }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,我会尽力回答您的问题。 首先,需要在Python中安装sqlite3和flask库,可以通过以下命令进行安装: ``` pip install sqlite3 pip install flask ``` 接下来,可以使用以下代码来连接test.db数据库,并查询学生数据表中的所有数据: ```python import sqlite3 # 连接数据库 conn = sqlite3.connect('test.db') # 获取游标 cursor = conn.cursor() # 查询学生数据表中的所有数据 cursor.execute('SELECT * FROM student') results = cursor.fetchall() # 关闭游标和连接 cursor.close() conn.close() ``` 接下来,可以使用flask库创建一个Web应用程序,并将查询到的学生数据以表格的形式展示出来。可以参考以下代码: ```python from flask import Flask, render_template import sqlite3 app = Flask(__name__) @app.route('/') def show_students(): # 连接数据库 conn = sqlite3.connect('test.db') # 获取游标 cursor = conn.cursor() # 查询学生数据表中的所有数据 cursor.execute('SELECT * FROM student') results = cursor.fetchall() # 关闭游标和连接 cursor.close() conn.close() # 将查询结果传递给HTML模板,并进行渲染 return render_template('students.html', students=results) if __name__ == '__main__': app.run() ``` 在上面的代码中,我们使用了Flask的`render_template`函数将查询到的学生数据传递给了一个名为`students.html`的HTML模板。下面是`students.html`模板的示例代码: ```html <!DOCTYPE html> <html> <head> <title>学生信息</title> <style> table { margin: 20px auto; border-collapse: collapse; width: 80%; } th, td { padding: 10px; border: 1px solid #ddd; text-align: center; } th { background-color: #f2f2f2; color: #333; font-weight: bold; } tr:nth-child(even) { background-color: #f2f2f2; } tr:hover { background-color: #ddd; } </style> </head> <body> <h1>学生信息</h1> <table> <tr> <th>学号</th> <th>姓名</th> <th>性别</th> <th>年龄</th> <th>班级</th> </tr> {% for student in students %} <tr> <td>{{ student[0] }}</td> <td>{{ student[1] }}</td> <td>{{ student[2] }}</td> <td>{{ student[3] }}</td> <td>{{ student[4] }}</td> </tr> {% endfor %} </table> </body> </html> ``` 在上面的代码中,我们使用了HTML的`<table>`标记来创建了一个表格,并使用CSS样式来设置了表格的样式。在Flask中,我们可以使用`{{ }}`来插入Python代码,并使用`{% %}`来插入控制语句,例如`{% for %}`循环。在上面的代码中,我们使用了`{% for %}`循环将查询到的学生数据逐行渲染到表格中。 最后,运行Python代码,并在浏览器中访问`http://localhost:5000/`即可查看学生数据信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Misha韩

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值