Skip to content

Calendar 日历 ​

按月或年展示日期,并允许在日期格中呈现日程数据。

何时使用 ​

在线体验 ​

Calendar 日历 · 在线体验真实 Go 组件
在独立页面打开 ↗

查看此场景的 Go 源码 ↗

基本用法 ​

go
import (
    "time"
    "github.com/tangoui/tango/ui"
)

var selectedDay time.Time

func page(c *ui.Context) {
    ui.Calendar(c, &selectedDay)
}

selectedDay 初始为零值时从当前月份开始显示,高亮今天且没有选中日期。

常见用法 ​

卡片模式与禁用日期 ​

go
func compact(c *ui.Context, day *time.Time) {
    ui.Calendar(c, day).Card().DisabledDate(func(d time.Time) bool {
        return d.Weekday() == time.Saturday || d.Weekday() == time.Sunday
    })
}

日期格内容 ​

go
func schedule(c *ui.Context, day *time.Time) {
    ui.Calendar(c, day).CellRender(func(d time.Time) {
        if d.Day() == 15 { ui.Badge(c).Status("success", "评审") }
    })
}

年/月切换 ​

go
var calendarMode = "month"

func modes(c *ui.Context, day *time.Time) {
    ui.Calendar(c, day).Mode(&calendarMode)
}

主题与样式 ​

Calendar 令牌控制日期格高度、今天、选中、禁用和头部样式。用 CellHeight 调整全屏日历的日期格高度,用 NoHeader 隐藏头部。

Range(start, end) 显示包含该区间的完整周,区间外的日期不可选,视图固定在月模式。ValidRange(start, end) 保留月份导航,并把可浏览和选择的日期限制在区间内。

ConfigProvider.Defaults("Calendar", ...) 支持 fullscreen(默认 true,false 使用卡片模式)、showWeek(默认 false)和 controllerType(Element Plus 主题的 "button" 或 "select",默认 "button")。

注意事项 ​

选中值和视图模式要保存在页面状态;日期格渲染函数每帧调用,应只读取已准备好的日程数据。键盘方向键可在日期间移动,禁用规则要与业务校验一致。

示例代码 ​

以下是上方在线演示运行的 Go 源码(来自官方 gallery),点击标题展开。函数接收的 c *ui.Context 由窗口的 UI 循环每帧传入,接入方式见 Go GUI 入门。

Calendar 日历

gallery 演示 Calendar 的 Render 函数,源码 page_data_misc2.go#L179。

加载中…

Calendar 指定日期范围

gallery 演示 CalendarRange 的 Render 函数,源码 page_date_more.go#L96。

加载中…

Calendar 可选范围、周数与下拉切换

gallery 演示 CalendarAlign 的 Render 函数,源码 page_b2h.go#L153。

加载中…

API ​

签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。

入口函数 ​

ui.Calendar ​

go
func Calendar(c *Context, val *time.Time) *CalendarW

Calendar adds a calendar bound to *val (zero means today, nothing selected). Modifiers apply until the end of the build; the content is built then (or at Changed/PanelChanged).

ui.Calendar(c, &day).CellRender(func(d time.Time) { ... })
ui.Calendar(c, &day).Card().DisabledDate(isWeekend)

返回 *CalendarW,可链式调用的方法见 CalendarW 方法。

CalendarW 方法 ​

ui.CalendarW:CalendarW is a calendar.

方法说明签名
CardCard uses Ant's compact card mode (fullscreen=false).Card() *CalendarW
CellHeightCellHeight overrides the fullscreen cell height.CellHeight(px float32) *CalendarW
CellRenderCellRender adds content below the date in month view.CellRender(fn func(d time.Time)) *CalendarW
ChangedChanged reports a date pick.Changed() bool
ControllerTypeControllerType sets the Element header's month navigation: "button" (previous / today / next, default) or "select" (year and month selects; Element controller-type).ControllerType(t string) *CalendarW
DisabledDateDisabledDate greys out and blocks dates.DisabledDate(fn func(d time.Time) bool) *CalendarW
FormatterFormatter formats the labels of the header's year and month selects: fn(v, kind) gets the year or the month number (1-12) and kind "year" or "month" (Element formatter); empty results fall back to the default.Formatter(fn func(v int, kind string) string) *CalendarW
LayoutLayout applies layout options to the calendar.Layout(o ...layout.Option) *CalendarW
ModeMode binds the view mode ("month" or "year").Mode(m *string) *CalendarW
MonthCellRenderMonthCellRender adds content below the month in year view.MonthCellRender(fn func(m time.Time)) *CalendarW
NoHeaderNoHeader hides the header.NoHeader() *CalendarW
PanelChangedPanelChanged reports a month/year/mode navigation.PanelChanged() bool
RangeRange shows only the weeks from start to end (Element range): the grid starts with start's week and ends with end's week, the header shows the range instead of the month navigation, days outside [start, end] are greyed out and cannot be picked, and the year view is off.Range(start, end time.Time) *CalendarW
ShowWeekShowWeek adds a column of week numbers (ISO weeks) before the days of the month view (Ant showWeek).ShowWeek(on bool) *CalendarW
ValidRangeValidRange limits the dates that can be picked and shown (Ant validRange): days outside [start, end] are disabled, the header's year and month choices and the month navigation stay inside the range.ValidRange(start, end time.Time) *CalendarW

相关链接 ​

以 MIT 许可发布