日期输入控件校验

InputDate控件可防止用户选择由 minmax 属性确定的范围之外的值.

然而,在许多情况下,范围内的所有日期并不是有效的。 为了处理这些情况,控件具有一个 itemValidator 属性。 此属性表示将日期作为参数的函数,如果日期对于选择有效,则返回true,否则返回false.

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'; import { getHoliday } from './data'; // document.readyState === 'complete' ? init() : window.onload = init; // function init() { // the InputDate let theInputDate = new input.InputDate('#theInputDate', { valueChanged: () => showCurrentDate(theInputDate), itemValidator: (date) => (date.getDay() % 6 != 0) && !getHoliday(date) }); showCurrentDate(theInputDate); // // format items in the InputDate's calendar (apply styles to weekends and holidays): theInputDate.calendar.formatItem.addHandler((sender, e) => { let weekday = e.data.getDay(), holiday = getHoliday(e.data); // wijmo.toggleClass(e.item, 'date-weekend', weekday == 0 || weekday == 6); wijmo.toggleClass(e.item, 'date-holiday', holiday != null); e.item.title = holiday || ''; }); // // show the currently selected date function showCurrentDate(ctrl) { let el = document.querySelector('#theInputDateOutput'); el.textContent = wijmo.Globalize.format(ctrl.value, 'D'); } } <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>GrapeCity InputDate Validation</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- SystemJS --> <script src="node_modules/systemjs/dist/system.src.js"></script> <script src="systemjs.config.js"></script> <script> System.import('./src/app'); </script> </head> <body> <div class="container-fluid"> <input id="theInputDate"> <div> The current date is <b><span id="theInputDateOutput"></span></b>. </div> </div> </body> </html> let holidays = { '1/1': 'New Year\'s Day', '6/14': 'Flag Day', '7/4': 'Independence Day', '11/11': 'Veteran\'s Day', '12/25': 'Christmas Day', '1/3/1': 'Martin Luther King\'s Birthday', '2/3/1': 'Washington\'s Birthday', '5/3/6': 'Armed Forces Day', '9/1/1': 'Labor Day', '10/2/1': 'Columbus Day', '11/4/4': 'Thanksgiving Day' // fourth Thursday in November }; // // gets the holiday for a given date export function getHoliday(date) { let day = date.getDate(), month = date.getMonth() + 1, holiday = holidays[month + '/' + day]; // if (!holiday) { let weekDay = date.getDay(), weekNum = Math.floor((day - 1) / 7) + 1; // holiday = holidays[month + '/' + weekNum + '/' + weekDay]; } // return holiday; } .wj-calendar { max-width: 21em; } .wj-calendar .wj-header { color: white; background: #808080; } .wj-calendar .date-holiday:not(.wj-state-selected) { font-weight: bold; color: #008f22; background: #f0fff0; outline: 2pt solid #008f22; } .wj-calendar .date-weekend:not(.wj-state-selected) { background-color: #d8ffa6; } body { margin-bottom: 36px; } 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'; // import { Component, enableProdMode, NgModule, Inject } from '@angular/core'; import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; import { BrowserModule } from '@angular/platform-browser'; import { WjInputModule } from '@grapecity/wijmo.angular2.input'; import { DataService } from './app.data'; // @Component({ selector: 'app-component', templateUrl: 'src/app.component.html' }) export class AppComponent { constructor(@Inject(DataService) private dataService: DataService) { } // customizeCalendar(calendar: input.Calendar) { calendar.formatItem.addHandler((sender: input.Calendar, e: input.FormatItemEventArgs) => { let weekday = e.data.getDay(), holiday = this.dataService.getHoliday(e.data); // wijmo.toggleClass(e.item, 'date-weekend', weekday == 0 || weekday == 6); wijmo.toggleClass(e.item, 'date-holiday', holiday != null); e.item.title = holiday || ''; }); } // validator = (date: Date) => { return (date.getDay() % 6 != 0) && !this.dataService.getHoliday(date); } } // @NgModule({ imports: [WjInputModule, BrowserModule], declarations: [AppComponent], providers: [DataService], bootstrap: [AppComponent] }) export class AppModule { } // enableProdMode(); // Bootstrap application with hash style navigation and global services. platformBrowserDynamic().bootstrapModule(AppModule); <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>GrapeCity InputDate Validation</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- Polyfills --> <script src="node_modules/core-js/client/shim.min.js"></script> <script src="node_modules/zone.js/dist/zone.min.js"></script> <!-- SystemJS --> <script src="node_modules/systemjs/dist/system.js"></script> <script src="systemjs.config.js"></script> <script> // workaround to load 'rxjs/operators' from the rxjs bundle System.import('rxjs').then(function (m) { System.set(SystemJS.resolveSync('rxjs/operators'), System.newModule(m.operators)); System.import('./src/app.component'); }); </script> </head> <body> <app-component></app-component> </body> </html> <div class="container-fluid"> <wj-input-date #theInputDate [itemValidator]="validator" (initialized)="customizeCalendar(theInputDate.calendar)"></wj-input-date> <div> The current date is <b>{{ theInputDate.value | date:'fullDate'}}</b>. </div> </div> import { Injectable } from '@angular/core'; // @Injectable() export class DataService { private _holidays = { '1/1': 'New Year\'s Day', '6/14': 'Flag Day', '7/4': 'Independence Day', '11/11': 'Veteran\'s Day', '12/25': 'Christmas Day', '1/3/1': 'Martin Luther King\'s Birthday', // third Monday in January '2/3/1': 'Washington\'s Birthday', // third Monday in February '5/3/6': 'Armed Forces Day', // third Saturday in May '9/1/1': 'Labor Day', // first Monday in September '10/2/1': 'Columbus Day', // second Monday in October '11/4/4': 'Thanksgiving Day' // fourth Thursday in November }; // // gets the holiday for a given date getHoliday(date: Date) { let day = date.getDate(), month = date.getMonth() + 1, holiday = this._holidays[month + '/' + day]; // if (!holiday) { let weekDay = date.getDay(), weekNum = Math.floor((day - 1) / 7) + 1; // holiday = this._holidays[month + '/' + weekNum + '/' + weekDay]; } // return holiday; } } .wj-calendar { max-width: 21em; } .wj-calendar .wj-header { color: white; background: #808080; } .wj-calendar .date-holiday:not(.wj-state-selected) { font-weight: bold; color: #008f22; background: #f0fff0; outline: 2pt solid #008f22; } .wj-calendar .date-weekend:not(.wj-state-selected) { background-color: #d8ffa6; } body { margin-bottom: 36px; } <template> <div class="container-fluid"> <wj-input-date :itemValidator="validator" :initialized="customizeCalendar"></wj-input-date> <div> The current date is <b>{{ theInputDate.value | formatDateTime}}</b>. </div> </div> </template> <script> import 'bootstrap.css'; import '@grapecity/wijmo.styles/wijmo.css'; import Vue from 'vue'; import '@grapecity/wijmo.vue2.core'; import '@grapecity/wijmo.vue2.input'; import { getHoliday } from './data'; let App = Vue.extend({ name: 'app', data: function () { return { theInputDate: {} } }, methods: { customizeCalendar: function(date){ this.theInputDate = date; date.calendar.formatItem.addHandler((sender, e) => { let weekday = e.data.getDay(), holiday = getHoliday(e.data); wijmo.toggleClass(e.item, 'date-weekend', weekday == 0 || weekday == 6); wijmo.toggleClass(e.item, 'date-holiday', holiday != null); e.item.title = holiday || ''; }); }, validator: function(date){ return (date.getDay() % 6 != 0) && !getHoliday(date); } }, filters: { formatDateTime: function(value){ return wijmo.Globalize.format(value, 'D'); } } }) let vm = new Vue({ render: h => h(App) }).$mount('#app'); </script> <style> .wj-calendar { max-width: 21em; } .wj-calendar .wj-header { color: white; background: #808080; } .wj-calendar .date-holiday:not(.wj-state-selected) { font-weight: bold; color: #008f22; background: #f0fff0; outline: 2pt solid #008f22; } .wj-calendar .date-weekend:not(.wj-state-selected) { background-color: #d8ffa6; } body { margin-bottom: 36px; } </style> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>GrapeCity InputDate Validation</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- SystemJS --> <script src="node_modules/systemjs/dist/system.src.js"></script> <script src="systemjs.config.js"></script> <script> System.import('./src/app.vue'); </script> </head> <body> <div id="app"></div> </body> </html> let holidays = { '1/1': 'New Year\'s Day', '6/14': 'Flag Day', '7/4': 'Independence Day', '11/11': 'Veteran\'s Day', '12/25': 'Christmas Day', '1/3/1': 'Martin Luther King\'s Birthday', '2/3/1': 'Washington\'s Birthday', '5/3/6': 'Armed Forces Day', '9/1/1': 'Labor Day', '10/2/1': 'Columbus Day', '11/4/4': 'Thanksgiving Day' // fourth Thursday in November }; // // gets the holiday for a given date export function getHoliday(date) { let day = date.getDate(), month = date.getMonth() + 1, holiday = holidays[month + '/' + day]; // if (!holiday) { let weekDay = date.getDay(), weekNum = Math.floor((day - 1) / 7) + 1; // holiday = holidays[month + '/' + weekNum + '/' + weekDay]; } // return holiday; } import 'bootstrap.css'; import '@grapecity/wijmo.styles/wijmo.css'; import './app.css'; // import * as React from 'react'; import * as ReactDOM from 'react-dom'; // import * as wijmo from '@grapecity/wijmo'; import * as wjInput from '@grapecity/wijmo.react.input'; import { getHoliday } from './data'; // class App extends React.Component { constructor(props) { super(props); this.state = { value: null }; } ; render() { return <div className="container-fluid"> <wjInput.InputDate itemValidator={this._validator} initialized={this._onInitialized.bind(this)} valueChanged={this._onValueChanged.bind(this)}> </wjInput.InputDate> <div> The current date is <b>{this._formatDateTime(this.state.value)}</b>. </div> </div>; } _onInitialized(sender) { this.setState({ value: sender.value }); sender.calendar.formatItem.addHandler((sender, e) => { let weekday = e.data.getDay(), holiday = getHoliday(e.data); wijmo.toggleClass(e.item, 'date-weekend', weekday == 0 || weekday == 6); wijmo.toggleClass(e.item, 'date-holiday', holiday != null); e.item.title = holiday || ''; }); } _validator(date) { return (date.getDay() % 6 != 0) && !getHoliday(date); } _formatDateTime(value) { return wijmo.Globalize.format(value, 'D'); } _onValueChanged(sender) { this.setState({ value: sender.value }); } } ReactDOM.render(<App />, document.getElementById('app')); <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>AutoComplete</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- SystemJS --> <script src="node_modules/systemjs/dist/system.src.js"></script> <script src="systemjs.config.js"></script> <script> System.import('./src/app'); </script> </head> <body> <div id="app"></div> </body> </html> .wj-calendar { max-width: 21em; } .wj-calendar .wj-header { color: white; background: #808080; } .wj-calendar .date-holiday:not(.wj-state-selected) { font-weight: bold; color: #008f22; background: #f0fff0; outline: 2pt solid #008f22; } .wj-calendar .date-weekend:not(.wj-state-selected) { background-color: #d8ffa6; } body { margin-bottom: 36px; } const _holidays = { '1/1': 'New Year\'s Day', '6/14': 'Flag Day', '7/4': 'Independence Day', '11/11': 'Veteran\'s Day', '12/25': 'Christmas Day', '1/3/1': 'Martin Luther King\'s Birthday', '2/3/1': 'Washington\'s Birthday', '5/3/6': 'Armed Forces Day', '9/1/1': 'Labor Day', '10/2/1': 'Columbus Day', '11/4/4': 'Thanksgiving Day' // fourth Thursday in November }; // // gets the holiday for a given date export function getHoliday(date) { let day = date.getDate(), month = date.getMonth() + 1, holiday = _holidays[month + '/' + day]; // if (!holiday) { let weekDay = date.getDay(), weekNum = Math.floor((day - 1) / 7) + 1; // holiday = _holidays[month + '/' + weekNum + '/' + weekDay]; } // return holiday; }