DevExtreme React - Handle the Value Change Event
If instead of picking a value, an end user types it into the DateBox, this value applies when the change event is raised. If you need the value to be applied on another event, set the valueChangeEvent option.
jQuery
$(function() { $("#dateBoxContainer").dxDateBox({ value: new Date(), type: "date", valueChangeEvent: "keyup" }); });
Angular
<dx-date-box [(value)]="date" type="date" valueChangeEvent="keyup"> </dx-date-box>
import { DxDateBoxModule } from "devextreme-angular"; // ... export class AppComponent { date: Date = new Date() } @NgModule({ imports: [ // ... DxDateBoxModule ], // ... })
Vue
<template> <DxDateBox :value="date" type="date" value-change-event="keyup" /> </template> <script> import 'devextreme/dist/css/dx.common.css'; import 'devextreme/dist/css/dx.light.css'; import DxDateBox from 'devextreme-vue/date-box'; export default { components: { DxDateBox }, data() { return { date: new Date() }; } } </script>
React
import React from 'react'; import 'devextreme/dist/css/dx.common.css'; import 'devextreme/dist/css/dx.light.css'; import DateBox from 'devextreme-react/date-box'; class App extends React.Component { constructor(props) { super(props); this.date = new Date(); } render() { return ( <DateBox defaultValue={this.date} type="date" valueChangeEvent="keyup" /> ); } } export default App;
To process a new DateBox value, you need to handle the value change event. If the handling function is not going to be changed during the lifetime of the widget, assign it to the onValueChanged option when you configure the widget.
jQuery
$(function() { $("#dateBoxContainer").dxDateBox({ // ... onValueChanged: function (e) { const previousValue = e.previousValue; const newValue = e.value; // Event handling commands go here } }); });
Angular
<dx-date-box ... [(value)]="date" (onValueChanged)="handleValueChange($event)"> </dx-date-box>
import { DxDateBoxModule } from "devextreme-angular"; // ... export class AppComponent { date: Date = new Date(), handleValueChange (e) { const previousValue = e.previousValue; const newValue = e.value; // Event handling commands go here } } @NgModule({ imports: [ // ... DxDateBoxModule ], // ... })
Vue
<template> <DxDateBox :value.sync="date" @value-changed="handleValueChange" /> </template> <script> import 'devextreme/dist/css/dx.common.css'; import 'devextreme/dist/css/dx.light.css'; import DxDateBox from 'devextreme-vue/date-box'; export default { components: { DxDateBox }, data() { return { date: new Date() }; }, methods: { handleValueChange(e) { const previousValue = e.previousValue; const newValue = e.value; // Event handling commands go here } } } </script>
React
import React from 'react'; import 'devextreme/dist/css/dx.common.css'; import 'devextreme/dist/css/dx.light.css'; import DateBox from 'devextreme-react/date-box'; class App extends React.Component { constructor(props) { super(props); this.state = { date: new Date() }; this.handleValueChange = this.handleValueChange.bind(this); } handleValueChange(e) { const previousValue = e.previousValue; const newValue = e.value; // Event handling commands go here this.setState({ date: newValue }); } render() { return ( <DateBox value={this.state.date} onValueChanged={this.handleValueChange} /> ); } } export default App;
If you are going to change event handlers at runtime, or if you need to attach several handlers to the value change event, subscribe to this event using the on(eventName, eventHandler) method. This approach is more typical of jQuery.
var valueChangedHandler1 = function (e) { var previousValue = e.previousValue; var newValue = e.value; // First handler of the "valueChanged" event }; var valueChangedHandler2 = function (e) { var previousValue = e.previousValue; var newValue = e.value; // Second handler of the "valueChanged" event }; $("#dateBoxContainer").dxDateBox("instance") .on("valueChanged", valueChangedHandler1) .on("valueChanged", valueChangedHandler2);
See Also
- Handle Events: Angular | Vue | React | jQuery | AngularJS | Knockout | ASP.NET MVC 5 | ASP.NET Core
- DateBox - Control the Behavior
- DateBox - Specify Value Range
- DateBox Demos
- DateBox API Reference
If you have technical questions, please create a support ticket in the DevExpress Support Center.