HarmonyOS 月视图模式的日历组件参考

可以切换上一月(的一号)/下一月/切换到本月的今天 停留在某月时,可以切换选择日期。

HarmonyOS
2024-12-18 16:25:35
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
shlp
// import { Lunar } from 'lunar-javascript'
import dateClass from '../beans/DateClass'
import dayjs from 'dayjs'

@Entry
@Component
struct DatePickerDialogExample01 {
  weekList: Array<string> = ['一', '二', '三', '四', '五', '六', '日']
  @State show: boolean = false
  @State selectDate: dayjs.Dayjs = dayjs()
  @State selectMonth: string = dayjs().format('YYYY年MM月')
  @State dataList: Array<dateClass> = []
  @State monthParam: number = 0 //切换日历的参数

  build() {
    Column() {
      Button('弹出或关闭日历')
        .height(36)
        .fontSize(20)
        .fontColor(Color.White)
        .onClick(() => {
          this.show = !this.show
          this.selectDate = dayjs()
          this.monthParam = 0
          this.getDataList(this.monthParam)
        })

      if (this.show) {
        Row() {
          Text(this.selectMonth)
            .fontSize(16)
            .fontColor(Color.White)
          Row() {
            Row() {
              Image($r('app.media.startIcon'))
                .width(24)
                .height(12)
                .backgroundColor(Color.Black)
            }
            .height('100%')
            .width(48)
            .alignItems(VerticalAlign.Center)
            .justifyContent(FlexAlign.Center)
            .onClick(() => {
              this.monthParam--
              this.getDataList(this.monthParam)
            })

            Row() {
              Image($r('app.media.startIcon'))
                .width(24)
                .height(12)
                .backgroundColor(Color.Black)
            }
            .height('100%')
            .width(48)
            .alignItems(VerticalAlign.Center)
            .justifyContent(FlexAlign.Center)
            .onClick(() => {
              this.monthParam++
              this.getDataList(this.monthParam)
            })
          }
        }
        .height(48)
        .width('100%')
        .justifyContent(FlexAlign.SpaceBetween)

        Row() {
          ForEach(this.weekList, (item: string) => {
            Text(item)
              .fontSize(16)
            Row() {
              ForEach(this.weekList, (item: string) => {
                Text(item)
                  .fontSize(16)
                  .fontColor(Color.White)
                  .width(48)
                  .textAlign(TextAlign.Center)
              })
            }
            .width('100%')

            Grid() {
              ForEach(this.dataList, (item: dateClass) => {
                GridItem() {
                  Column() {
                    Text(item.day.toString())
                      .fontSize(16)
                      .fontColor(Color.White)
                      .width(48)
                      .textAlign(TextAlign.Center)
                      .opacity(item.isToMonth ? 1 : 0.4)
                    Text(item.lunarDay)
                      .fontSize(12)
                      .fontColor(Color.White)
                      .width(48)
                      .textAlign(TextAlign.Center)
                      .opacity(item.isToMonth ? 1 : 0.4)
                  }
                  .width(48)
                  .height(48)
                  .alignItems(HorizontalAlign.Center)
                  .justifyContent(FlexAlign.Center)
                  .backgroundColor(item.dayjsObj.format('YYYY-MM-DD') ==
                  dayjs().format('YYYY-MM-DD') ?
                    '#007DFF' : Color.Black)
                  .borderWidth(1)
                  .borderColor(item.dayjsObj.format('YYYY-MM-DD') ==
                  this.selectDate.format('YYYY-MM-DD') ?
                    '#007DFF' : Color.Black)
                  .onClick(() => {
                    this.selectDate = item.dayjsObj
                  })
                }
              })
            }
            .height(288)
            .columnsTemplate('1fr 1fr 1fr 1fr 1fr 1fr 1fr')
            .rowsTemplate('1fr 1fr 1fr 1fr 1fr 1fr')
            .columnsGap(0)
            .rowsGap(0)
          }
        }
        .width('100%')
        .height('100%')
        .backgroundColor(Color.Black)
        .justifyContent(FlexAlign.Center)
        .padding({ left: 12, right: 12 })
      }
      getDataList(param: number) {
        this.dataList = []
        let firstDate = dayjs().add(param, 'month').startOf('month') // 当月的第一天
        let afterDate = dayjs().add(param, 'month').endOf('month') // 当月的最后一天
        this.selectMonth = firstDate.format('YYYY年MM月')
        let frontDay = 0 // 显示日历的第一天需要向前的天数
        if (firstDate.day() == 0) {
          frontDay = 6
        } else {
          frontDay = firstDate.day() - 1
        }
        let showFirstDay = firstDate.subtract(frontDay, 'day') // 显示日历的第一天
        for (let i = 0;i < 42; i++) {
          let dayjsObj = showFirstDay.add(i, 'day')
          let day = dayjsObj.date()
          let lunarDay = ''
          // if (Lunar.fromDate(dayjsObj.toDate()).getFestivals().length !== 0) { // 显示节日
          // lunarDay = Lunar.fromDate(dayjsObj.toDate()).getFestivals()[0]
          // } else if (Lunar.fromDate(dayjsObj.toDate()).getJieQi() !== '') { // 显示节气
          // lunarDay = Lunar.fromDate(dayjsObj.toDate()).getJieQi()
          // } else { // 显示农历日期
          // lunarDay = Lunar.fromDate(dayjsObj.toDate()).getDayInChinese()
          // }
          let isToMonth = true
          if (dayjsObj.isBefore(firstDate) || dayjsObj.isAfter(afterDate)) {
            isToMonth = false
          }
          this.dataList.push(new dateClass(dayjsObj, day, lunarDay, isToMonth))
        }
        // console.log('aboutToAppear',JSON.stringify(this.dataList))
      }

      aboutToAppear() {
        this.getDataList(this.monthParam)
      }
    }
    // @ts-ignore
    import dayjs from 'dayjs'

    export default class DateClass {
      dayjsObj: dayjs.Dayjs // dayjs时间
      day: number // 天
      lunarDay: string // 农历
      isToMonth: boolean // 是否当前月

      constructor(dayjsObj: dayjs.Dayjs, day: number, lunarDay: string, isToMonth: boolean) {
        this.dayjsObj = dayjsObj
        this.day = day
        this.lunarDay = lunarDay
        this.isToMonth = isToMonth
      }
    }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.
  • 164.
  • 165.
  • 166.
  • 167.
  • 168.
  • 169.
  • 170.
  • 171.
  • 172.
  • 173.
  • 174.
  • 175.
  • 176.
  • 177.
  • 178.
  • 179.
  • 180.
  • 181.
  • 182.
分享
微博
QQ
微信
回复
2024-12-18 18:58:28
相关问题
HarmonyOS 日历相关组件
1186浏览 • 1回复 待解决
HarmonyOS 日历组件cjcalendar文档
999浏览 • 1回复 待解决
HarmonyOS日历组件吗?
1177浏览 • 1回复 待解决
HarmonyOS CJCalendar 日历组件无法编译
2295浏览 • 1回复 待解决
HarmonyOS 组件在其父视图位置
1015浏览 • 1回复 待解决
HarmonyOS 图片浏览和裁剪视图组件
594浏览 • 1回复 待解决
HarmonyOS 希望官方提供日历组件
1102浏览 • 1回复 待解决
HarmonyOS 日历里面添加日历问题
786浏览 • 1回复 待解决
高阶组件视图基本用法
2232浏览 • 1回复 待解决
是否提供日历组件,你了解吗?
2715浏览 • 1回复 待解决
HarmonyOS 日历相关绘制
646浏览 • 1回复 待解决
UIAbility组件启动模式
1344浏览 • 1回复 待解决
HarmonyOS 新闻相关DEMO参考
670浏览 • 1回复 待解决
HarmonyOS 位置picker参考文档
584浏览 • 1回复 待解决
HarmonyOS 图片处理参考api
567浏览 • 1回复 待解决