本帖最后由 Wijmo5User 于 2019-9-28 13:44 编辑
以下这个示例或许能解决楼主的需求。
源代码如下:
- import 'bootstrap.css';
- import '@grapecity/wijmo.styles/wijmo.css';
- import './styles.css';
- import * as wijmo from '@grapecity/wijmo';
- import * as input from '@grapecity/wijmo.input';
- //
- document.readyState === 'complete' ? init() : window.onload = init;
- //
- function init() {
- const elDayMap = new WeakMap();
- const selectedDaySet = new Set();
- const toYmd = (d) => wijmo.Globalize.formatDate(d, 'yyyy-MM-dd');
- // the calendar
- const theCalendar = new input.Calendar('#theCalendar', {
- value: null,
- isReadOnly: true,
- itemFormatter: (date, el) => elDayMap.set(el, date)
- });
- theCalendar.addEventListener(theCalendar.hostElement, 'click', (e) => {
- const clickedDay = elDayMap.get(e.target);
- if (clickedDay == null) return;
- const dateString = toYmd(clickedDay);
- if (selectedDaySet.has(dateString)) {
- selectedDaySet.delete(dateString);
- } else {
- selectedDaySet.add(dateString);
- }
- showSelectedDateList();
- theCalendar.invalidate();
- });
- theCalendar.formatItem.addHandler((sender, args) => {
- const selected = selectedDaySet.has(toYmd(args.data));
- wijmo.toggleClass(args.item, 'wj-state-selected', selected);
- wijmo.setAttribute(args.item, 'aria-selected', selected);
- });
- //
- // show the currently selected date
- function showSelectedDateList() {
- const el = document.querySelector('#theCalendarOutput');
- el.textContent = '';
- selectedDaySet.forEach((d) => {
- el.textContent = el.textContent + '[' + d + '] ';
- });
- el.textContent = el.textContent.trim();
- }
- showSelectedDateList();
- }
复制代码
如果想查看以上代码的运行效果,可以打开以下官方示例页面:
WijmoJS 示例 - 输入(Input) - 日历(Calendar) - 概览(Overview) - ES6
在运行效果面板的下方,有一个源码编辑器面板。
在源码编辑器面板的“app.js”页签中,将以下内容替换成上面的那段代码:
- import 'bootstrap.css';
- import '@grapecity/wijmo.styles/wijmo.css';
- import './styles.css';
- import * as wijmo from '@grapecity/wijmo';
- import * as input from '@grapecity/wijmo.input';
- //
- document.readyState === 'complete' ? init() : window.onload = init;
- //
- function init() {
- // the calendar
- let theCalendar = new input.Calendar('#theCalendar', {
- valueChanged: () => showCurrentDate()
- });
- //
- // show the currently selected date
- function showCurrentDate() {
- let el = document.querySelector('#theCalendarOutput');
- el.textContent = wijmo.Globalize.format(theCalendar.value, 'D');
- }
- showCurrentDate();
- }
复制代码
点击源码编辑器面板右上角的“运行”按钮。
在运行效果面板中点击日历组件中的元素,查看日历组件下方的输出。
|