当前位置:   article > 正文

harmonyos arkts 开发商品页面_arkts商城页面

arkts商城页面

1.结果展示

2. 实现分层组件

1.1 实现搜索栏

1.2 代码

这段代码是一个构建搜索框组件的方法,具体功能包括:

- 创建一个Search组件,设置初始值为this.keyword,placeholder为'请输入书名...'
- 添加一个搜索按钮,并设置按钮文本为'搜索'
- 设置Search组件的宽度为100%,高度为50
- 设定背景颜色为'#F5F5F5'
- 设置placeholder的颜色为灰色
- 设置placeholder的字体样式为大小14、粗细400
- 设置输入文字的字体样式为大小20、粗细400
- 绑定onSubmit事件处理函数,当用户点击搜索按钮时调用this.searchBooks(value),其中value为用户输入的搜索关键词
- 绑定onChange事件处理函数,当用户改变搜索框内容时将this.keyword更新为最新的输入值

这个方法实现了一个简单的搜索框功能,用户可以在输入框中输入关键词进行搜索,并且支持点击搜索按钮或者直接回车键执行搜索操作。同时也会实时更新this.keyword的数值,以便在搜索时传递正确的关键词参数。

  1. @Builder buildSearch() {
  2. Search({ value: this.keyword, placeholder: '请输入书名...' })
  3. .searchButton('搜索')
  4. .width('100%')
  5. .height(50)
  6. .backgroundColor('#F5F5F5')
  7. .placeholderColor(Color.Grey)
  8. .placeholderFont({ size: 14, weight: 400 })
  9. .textFont({ size: 20, weight: 400 })
  10. .onSubmit((value: string) => {
  11. this.searchBooks(value)
  12. })
  13. .onChange((value: string) => {
  14. this.keyword = value
  15. })
  16. }

2.1 实现商品卡片

2.2 卡片代码

这段代码定义了一个名为ItemCard的组件结构,主要用于展示书籍信息。下面是该组件的功能逻辑描述:

- ItemCard组件包含一个私有属性book,用于存储书籍信息。

- build方法用于构建整个书籍信息展示的结构,包括左侧容器和右侧容器。

- 左侧容器部分使用Row布局,包含一个文本框,展示书名this.book.bookname,设置字体样式为斜体,字体大小为15,背景颜色为透明紫红色,字体颜色为白色,宽度占30%,高度为60,边框圆角为10,文本居中显示,左外边距为5。

- 右侧容器部分使用Column布局,包含价格信息和作者、出版社信息。价格信息展示为'价格为:¥' + this.book.price.toFixed(3),字体颜色为蓝色,字体大小为20。作者和出版社信息分别显示为"作者:" + this.book.author 和 "出版社:" + this.book.publisher,字体颜色为黑色,字体大小为12。这两个信息在同一行显示,宽度占右侧容器的68%,并且内容左右对齐。

- 整个ItemCard组件的样式设置为宽度占97%,高度为80,内容左右对齐,背景颜色为淡粉色,边框圆角为20。

总体来说,ItemCard组件通过左右两个容器分别展示书名、价格以及作者、出版社等信息

  1. @Component
  2. struct ItemCard{
  3. private book
  4. build(){
  5. Row() {
  6. // 左侧容器
  7. Text(this.book.bookname)
  8. .fontStyle(FontStyle.Italic)
  9. .fontSize(15)
  10. .backgroundColor("#ff404aa9")
  11. .fontColor('white')
  12. .width("30%")
  13. .height('60')
  14. .borderRadius(10)
  15. .textAlign(TextAlign.Center)
  16. .margin({ left: '5' })
  17. // 右侧容器
  18. Column() {
  19. Text('价格为:¥' + this.book.price.toFixed(3)).fontColor('blue').fontSize(20)
  20. Row(){
  21. Text("作者:" + this.book.author).fontColor('black').fontSize(12)
  22. Text("出版社:" + this.book.publisher).fontColor('black').fontSize(12)
  23. }.width('68%').justifyContent(FlexAlign.SpaceBetween)
  24. }
  25. .width("70%")
  26. .borderRadius(10)
  27. }
  28. .width("97%")
  29. .height("80")
  30. .justifyContent(FlexAlign.SpaceBetween)
  31. .backgroundColor("#fff3f3fc")
  32. .borderRadius(20)
  33. }
  34. }

2.3 实现收藏效果

2.4 收藏效果代码

  1. @Builder SaveBtn(bookno:string){
  2. Button("收藏").width("60").height('40')
  3. .backgroundColor('#fff3f6f5')
  4. .fontColor("#ff181a19")
  5. .onClick(()=> {
  6. this.saveBook(bookno)
  7. })
  8. }

3. 全部代码(书籍数据来源于接口,可以自己适当的自定义一串json数据作为接口数据)

  1. pageSize: number = 10
  2. @State page:number =0 //总页面
  3. httpUtil: http.HttpRequest
  4. // todo 传递用户id
  5. @State userID:string=""
  6. searchBooks(keyword:string) {
  7. this.httpUtil.request(`192.168.**.***/books/${this.cur}/${this.pageSize}`,
  8. {
  9. method: http.RequestMethod.GET,
  10. extraData: { 'k': "bookname", 'v': this.keyword }
  11. }
  12. ).then(res => {
  13. let jsonResult = res.result.toString()
  14. let responseObject = JSON.parse(jsonResult)
  15. this.try= responseObject.data
  16. // 当前页面
  17. this.cur=responseObject.cur
  18. // 总共页面
  19. this.page=responseObject.pages
  20. }).catch(err => {
  21. console.log('数据传输http错误')
  22. })
  23. }
  24. async saveBook(bookno:string){
  25. // todo 收藏逻辑
  26. // todo 1.找到用户与书籍信息,直接save
  27. const res = await this.httpUtil.request(`localhost/save/insert?rno=${this.userID}&bno=${bookno}`,
  28. {
  29. method: http.RequestMethod.POST,
  30. }
  31. )
  32. console.log(res.result.toString())
  33. // todo 2.收藏成功 prompt传递弹窗
  34. Prompt.showToast({message:"收藏成功"})
  35. }
  36. aboutToAppear() {
  37. let httpRequest = http.createHttp()
  38. this.httpUtil = httpRequest
  39. // todo 传递用户编号
  40. this.userID = AppStorage.Get("info")
  41. // todo 查询全部图书
  42. this.searchBooks("")
  43. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/木道寻08/article/detail/757267
推荐阅读
相关标签
  

闽ICP备14008679号