Calendar 日历
按月或年展示日期,并允许在日期格中呈现日程数据。
何时使用
- 排班、日程、预约和按日期查看数据的页面。
- 只需要在表单中选日期时使用 DatePicker 日期选择框。
在线体验
基本用法
import (
"time"
"github.com/tangoui/tango/ui"
)
var selectedDay time.Time
func page(c *ui.Context) {
ui.Calendar(c, &selectedDay)
}selectedDay 初始为零值时从当前月份开始显示,高亮今天且没有选中日期。
常见用法
卡片模式与禁用日期
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
})
}日期格内容
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", "评审") }
})
}年/月切换
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 入门。
API
签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。
入口函数
ui.Calendar
func Calendar(c *Context, val *time.Time) *CalendarWCalendar 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.
| 方法 | 说明 | 签名 |
|---|---|---|
Card | Card uses Ant's compact card mode (fullscreen=false). | Card() *CalendarW |
CellHeight | CellHeight overrides the fullscreen cell height. | CellHeight(px float32) *CalendarW |
CellRender | CellRender adds content below the date in month view. | CellRender(fn func(d time.Time)) *CalendarW |
Changed | Changed reports a date pick. | Changed() bool |
ControllerType | ControllerType 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 |
DisabledDate | DisabledDate greys out and blocks dates. | DisabledDate(fn func(d time.Time) bool) *CalendarW |
Formatter | Formatter 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 |
Layout | Layout applies layout options to the calendar. | Layout(o ...layout.Option) *CalendarW |
Mode | Mode binds the view mode ("month" or "year"). | Mode(m *string) *CalendarW |
MonthCellRender | MonthCellRender adds content below the month in year view. | MonthCellRender(fn func(m time.Time)) *CalendarW |
NoHeader | NoHeader hides the header. | NoHeader() *CalendarW |
PanelChanged | PanelChanged reports a month/year/mode navigation. | PanelChanged() bool |
Range | Range 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 |
ShowWeek | ShowWeek adds a column of week numbers (ISO weeks) before the days of the month view (Ant showWeek). | ShowWeek(on bool) *CalendarW |
ValidRange | ValidRange 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 |