Calendar日历 - 图1

Calendar 日历

按照日历形式展示数据的容器。

何时使用

当数据是日期或按照日期划分时,例如日程、课表、价格日历等,农历等。目前支持年/月切换。

代码演示

Calendar日历 - 图2

基本

一个通用的日历面板,支持年/月切换。

  1. <template>
  2. <a-calendar @panelChange="onPanelChange" />
  3. </template>
  4. <script>
  5. export default {
  6. methods: {
  7. onPanelChange(value, mode) {
  8. console.log(value, mode);
  9. },
  10. },
  11. };
  12. </script>

Calendar日历 - 图3

卡片模式

用于嵌套在空间有限的容器中。

  1. <template>
  2. <div :style="{ width: '300px', border: '1px solid #d9d9d9', borderRadius: '4px' }">
  3. <a-calendar :fullscreen="false" @panelChange="onPanelChange" />
  4. </div>
  5. </template>
  6. <script>
  7. export default {
  8. methods: {
  9. onPanelChange(value, mode) {
  10. console.log(value, mode);
  11. },
  12. },
  13. };
  14. </script>

Calendar日历 - 图4

通知事项日历

一个复杂的应用示例,用 dateCellRendermonthCellRender 函数来自定义需要渲染的数据。

  1. <template>
  2. <a-calendar>
  3. <ul slot="dateCellRender" slot-scope="value" class="events">
  4. <li v-for="item in getListData(value)" :key="item.content">
  5. <a-badge :status="item.type" :text="item.content" />
  6. </li>
  7. </ul>
  8. <template slot="monthCellRender" slot-scope="value">
  9. <div v-if="getMonthData(value)" class="notes-month">
  10. <section>{{ getMonthData(value) }}</section>
  11. <span>Backlog number</span>
  12. </div>
  13. </template>
  14. </a-calendar>
  15. </template>
  16. <script>
  17. export default {
  18. methods: {
  19. getListData(value) {
  20. let listData;
  21. switch (value.date()) {
  22. case 8:
  23. listData = [
  24. { type: 'warning', content: 'This is warning event.' },
  25. { type: 'success', content: 'This is usual event.' },
  26. ];
  27. break;
  28. case 10:
  29. listData = [
  30. { type: 'warning', content: 'This is warning event.' },
  31. { type: 'success', content: 'This is usual event.' },
  32. { type: 'error', content: 'This is error event.' },
  33. ];
  34. break;
  35. case 15:
  36. listData = [
  37. { type: 'warning', content: 'This is warning event' },
  38. { type: 'success', content: 'This is very long usual event。。....' },
  39. { type: 'error', content: 'This is error event 1.' },
  40. { type: 'error', content: 'This is error event 2.' },
  41. { type: 'error', content: 'This is error event 3.' },
  42. { type: 'error', content: 'This is error event 4.' },
  43. ];
  44. break;
  45. default:
  46. }
  47. return listData || [];
  48. },
  49. getMonthData(value) {
  50. if (value.month() === 8) {
  51. return 1394;
  52. }
  53. },
  54. },
  55. };
  56. </script>
  57. <style scoped>
  58. .events {
  59. list-style: none;
  60. margin: 0;
  61. padding: 0;
  62. }
  63. .events .ant-badge-status {
  64. overflow: hidden;
  65. white-space: nowrap;
  66. width: 100%;
  67. text-overflow: ellipsis;
  68. font-size: 12px;
  69. }
  70. .notes-month {
  71. text-align: center;
  72. font-size: 28px;
  73. }
  74. .notes-month section {
  75. font-size: 28px;
  76. }
  77. </style>

Calendar日历 - 图5

选择功能

一个通用的日历面板,支持年/月切换。

  1. <template>
  2. <div>
  3. <a-alert
  4. :message="`You selected date: ${selectedValue && selectedValue.format('YYYY-MM-DD')}`"
  5. />
  6. <div
  7. :style="{
  8. display: 'inline-block',
  9. width: '500px',
  10. border: '1px solid #d9d9d9',
  11. borderRadius: '4px',
  12. }"
  13. >
  14. <a-calendar :value="value" @select="onSelect" @panelChange="onPanelChange" />
  15. </div>
  16. <div
  17. :style="{
  18. display: 'inline-block',
  19. width: '500px',
  20. marginLeft: '20px',
  21. border: '1px solid #d9d9d9',
  22. borderRadius: '4px',
  23. }"
  24. >
  25. <a-calendar v-model="value1" />
  26. </div>
  27. </div>
  28. </template>
  29. <script>
  30. import moment from 'moment';
  31. export default {
  32. data() {
  33. return {
  34. value: moment('2017-01-25'),
  35. selectedValue: moment('2017-01-25'),
  36. value1: moment('2017-01-25'),
  37. };
  38. },
  39. methods: {
  40. onSelect(value) {
  41. this.value = value;
  42. this.selectedValue = value;
  43. },
  44. onPanelChange(value) {
  45. this.value = value;
  46. },
  47. },
  48. };
  49. </script>

Custom header

MonthYear

2021

Calendar日历 - 图6

Feb

Calendar日历 - 图7

SuMoTuWeThFrSa
31
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
01
02
03
04
05
06
07
08
09
10
11
12
13

Calendar日历 - 图8

自定义头部

自定义日历头部内容。

  1. <template>
  2. <div style="width: 300px; border: 1px solid #d9d9d9; border-radius: 4px">
  3. <a-calendar :fullscreen="false" :header-render="headerRender" @panelChange="onPanelChange" />
  4. </div>
  5. </template>
  6. <script>
  7. export default {
  8. methods: {
  9. onPanelChange(value, mode) {
  10. // eslint-disable-next-line no-console
  11. console.log(value, mode);
  12. },
  13. headerRender({ value, type, onChange, onTypeChange }) {
  14. const start = 0;
  15. const end = 12;
  16. const monthOptions = [];
  17. const current = value.clone();
  18. const localeData = value.localeData();
  19. const months = [];
  20. for (let i = 0; i < 12; i++) {
  21. current.month(i);
  22. months.push(localeData.monthsShort(current));
  23. }
  24. for (let index = start; index < end; index++) {
  25. monthOptions.push(
  26. <a-select-option class="month-item" key={`${index}`}>
  27. {months[index]}
  28. </a-select-option>,
  29. );
  30. }
  31. const month = value.month();
  32. const year = value.year();
  33. const options = [];
  34. for (let i = year - 10; i < year + 10; i += 1) {
  35. options.push(
  36. <a-select-option key={i} value={i} class="year-item">
  37. {i}
  38. </a-select-option>,
  39. );
  40. }
  41. return (
  42. <div style={{ padding: '10px' }}>
  43. <div style={{ marginBottom: '10px' }}>Custom header </div>
  44. <a-row type="flex" justify="space-between">
  45. <a-col>
  46. <a-radio-group size="small" onChange={e => onTypeChange(e.target.value)} value={type}>
  47. <a-radio-button value="month">Month</a-radio-button>
  48. <a-radio-button value="year">Year</a-radio-button>
  49. </a-radio-group>
  50. </a-col>
  51. <a-col>
  52. <a-select
  53. size="small"
  54. dropdownMatchSelectWidth={false}
  55. class="my-year-select"
  56. onChange={newYear => {
  57. const now = value.clone().year(newYear);
  58. onChange(now);
  59. }}
  60. value={String(year)}
  61. >
  62. {options}
  63. </a-select>
  64. </a-col>
  65. <a-col>
  66. <a-select
  67. size="small"
  68. dropdownMatchSelectWidth={false}
  69. value={String(month)}
  70. onChange={selectedMonth => {
  71. const newValue = value.clone();
  72. newValue.month(parseInt(selectedMonth, 10));
  73. onChange(newValue);
  74. }}
  75. >
  76. {monthOptions}
  77. </a-select>
  78. </a-col>
  79. </a-row>
  80. </div>
  81. );
  82. },
  83. },
  84. };
  85. </script>

API

**注意:**Calendar 部分 locale 是从 value 中读取,所以请先正确设置 moment 的 locale。

  1. // 默认语言为 en-US,所以如果需要使用其他语言,推荐在入口文件全局设置 locale // import moment from
  2. 'moment'; // import 'moment/locale/zh-cn'; // moment.locale('zh-cn');
  3. <a-calendar @panelChange="onPanelChange" @select="onSelect">
  4. <template slot="dateCellRender" slot-scope="value"></template>
  5. <template slot="monthCellRender" slot-scope="value"></template>
  6. </a-calendar>
参数说明类型默认值版本
dateCellRender作用域插槽,用来自定义渲染日期单元格,返回内容会被追加到单元格,function(date: moment)
dateFullCellRender作用域插槽,自定义渲染日期单元格,返回内容覆盖单元格function(date: moment)
defaultValue默认展示的日期moment默认日期
disabledDate不可选择的日期(currentDate: moment) => boolean
fullscreen是否全屏显示booleantrue
locale国际化配置object默认配置
mode初始模式,month/yearstringmonth
monthCellRender作用域插槽,自定义渲染月单元格,返回内容会被追加到单元格function(date: moment)
monthFullCellRender作用域插槽,自定义渲染月单元格,返回内容覆盖单元格function(date: moment)
validRange设置可以显示的日期[moment, moment]
value(v-model)展示日期moment当前日期
headerRender自定义头部内容function(object:{value: moment, type: string, onChange: f(), onTypeChange: f()}) | slot-scope-1.5.0
valueFormat可选,绑定值的格式,对 value、defaultValue 起作用。不指定则绑定值为 moment 对象string,具体格式-1.5.4

事件

事件名称说明回调参数
panelChange日期面板变化回调function(date: moment | string, mode: string)
select点击选择日期回调function(date: moment | string)
change日期变化时的回调, 面板变化有可能导致日期变化function(date: moment | string)