openharmony 鸿蒙 js-service-widget-basic-calendar

2025-06-12 浏览 (1)

calendar

The <calendar> component is used to display the calendar page.

NOTE

The APIs of this module are supported since API version 8. Updates will be marked with a superscript to indicate their earliest API version.

Child Components

Not supported

Attributes

In addition to the universal attributes, the following attributes are supported.

NameTypeDefault ValueMandatoryDescription
datestringCurrent dateNoDate selected on the current page. The default value is the current date in the format of YYYY-MM-DD, for example, 2019-11-22.
cardcalendarboolfalseNoWhether the current calendar is a widget calendar.
startdayofweekint6NoStart day of a week on the widget calendar. The default value is Sunday. The value ranges from 0 to 6.
offdaysstring5, 6NoRest days of a week on the widget calendar. The default value is Saturday and Sunday. The value ranges from 0 to 6.
calendardatastring-YesData to be displayed on the monthly widget calendar. You can pass data of 57 or 67 days in the JSON format. For details about the "data" attribute, see Table 1.
showholidaybooltrueNoWhether to display holiday information.

Table 1 "data" attributes of calendardata

NameTypeDescription
indexintIndex of the data, indicating the sequence number of the date.
dayintDay.
monthintMonth.
yearintYear.
isFirstOfLuanrboolWhether the day is the first day of the lunar calendar. The first day is underlined.
hasScheduleboolWhether there is an event scheduled for the day. If there is, a circle is drawn around the date number.
markLunarDayboolWhether the day is a holiday, on which lunar calendar data is marked in blue.
lunarDaystringLunar date.
lunarMonthstringLunar month.
dayMarkstringDay mark.
- work: workday.
- off: rest day.
dayMarkValuestringText to be displayed for a workday or a rest day.

Example of calendardata:

{
"year":2021,
"month":1,
"data": [{
    "index": 0,    
    "lunarMonth": "11",
    "lunarDay": "13",    
    "year": 2020,    
    "month ": 12,    
    "day": 27,    
    "dayMark": "work",    
    "dayMarkValue": "On-duty",
    "isFirstOfLunar": true,
    "hasSchedule": true,
    "markLunarDay": true
  },  {
    "index": 1,
    "lunarMonth": "11",
    "lunarDay": "14",   
    "year": 2020,    
    "month ": 12,    
    "day": 28,    
    "dayMark": "work",    
    "dayMarkValue": "On-duty",
    "isFirstOfLunar": true,
    "hasSchedule": true,
    "markLunarDay": true
  },  {
    "index": 2,
    "lunarMonth": "11",
    "lunarDay": "15",   
    "year": 2020,    
    "month ": 12,    
    "day": 29,    
    "dayMark": "work",    
    "dayMarkValue": "On-duty",
    "isFirstOfLunar": true,
    "hasSchedule": true,
    "markLunarDay": true
  },
  ...
  ]
}

Styles

NameTypeDefault ValueMandatoryDescription
background-color<color>-NoBackground color.

Events

NameParameterDescription
selectedchangechangeEventA user clicks a date or switches between months.
requestdatarequestEventA date is requested.

Table 2 changeEvent

NameTypeDescription
$event.daystringSelected day.
$event.monthstringSelected month.
$event.yearstringSelected year.

Table 3 requestEvent

NameTypeDescription
$event.monthstringRequested month.
$event.yearstringRequested year.
$event.currentYearstringCurrent year.
$event.currentMonthstringCurrent month.

Example

The following examples are not intended as copy-paste-ready. Further customization is required.

<!-- xxx.hml -->
<div class="container">
    <calendar class="container_test"
        cardcalendar="true"
        onselectedchange="clickOneDay"
        onrequestdata="messageEventData"
        date="{{currentDate}}"
        offdays="{{offDays}}"
        showholiday="{{showHoliday}}"
        startdayofweek="{{startDayOfWeek}}"
        calendardata="{{calendarData}}">
   </calendar>
</div>
/* xxx.css */ 
.container {
    flex-direction:column;
    width: 100%;
    height: 100%;
    align-items:center;
    padding-start: 4px;
    padding-end: 4px;
}
.container_test {
    background-color: white;
}
{
    "data": {
        "currentDate": "",
        "offDays": "",
        "startDayOfWeek": 6,
        "showHoliday": true,
        "calendarData": ""
    },
    "actions": {
        "clickOneDay": {
            "action": "router",
            "bundleName": "com.example.calendar",
            "abilityName": "EntryAbility",
            "params": {
                "action": "click_month_view_event",
                "day": "$event.day",
                "month": "$event.month",
                "year": "$event.year"
            }
        },
        "messageEventData": {
            "action": "message",
            "params": {
                "month": "$event.month",
                "year": "$event.year",
                "currentMonth": "$event.currentMonth",
                "currentYear": "$event.currentYear"
            }
        }
    }
}

你可能感兴趣的鸿蒙文章

harmony 鸿蒙JavaScript Service Widget UI Components

harmony 鸿蒙Data Types

harmony 鸿蒙button

harmony 鸿蒙chart

harmony 鸿蒙clock

harmony 鸿蒙divider

harmony 鸿蒙image

harmony 鸿蒙input

harmony 鸿蒙progress

harmony 鸿蒙span

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/3t6SHV