The following section explains the required steps to build the NumericTextBox component with its basic usage in step by step procedure.
System requirements for Syncfusion Vue UI components
You can use Vue CLI
to setup your vue applications.
To install Vue CLI use the following command.
npm install -g @vue/cli
Start a new project using below Vue CLI command.
vue init webpack-simple quickstart
cd quickstart
npm install
All the available Essential JS 2 packages are published in
npmjs.com
public registry.
You can choose the component that you want to install.
To install NumericTextBox component, use the following command
npm install @syncfusion/ej2-vue-inputs –save
For Registering Vue Component two ways are available. They are as follows.
Import the NumericTextBox Component Plugin from the EJ2 Vue Package and register the same using Vue.use() with NumericTextBox Component Plugin as its argument.
Refer the code snippet given below.
import { NumericTextBoxPlugin } from "@syncfusion/ej2-vue-inputs";
Vue.use(NumericTextBoxPlugin);
By Registering Component Plugin in Vue, all child directives are also globally registered.
Import the NumericTextBox Component and NumericTextBox Component Plugin from EJ2 Vue Package, register the same using the Vue.component() with name of NumericTextBox Component from NumericTextBox Component Plugin and the EJ2 Vue Component as its arguments.
Refer the code snippet given below.
import { NumericTextBoxComponent, NumericTextBoxPlugin } from '@syncfusion/ej2-vue-inputs';
Vue.component(NumericTextBoxPlugin.name, NumericTextBoxComponent);
By using Vue.component(), only the EJ2 Vue Component is registered. Child directives needs to be registered separately.
Add the EJ2 Vue NumericTextBox using <ejs-numerictextbox>
to the <template>
section of the App.vue
file in src directory,
the content attribute of the NumericTextBox component is provided as name in data option in the <script>
section.
<template>
<div class="control_wrapper">
<div class="control-label">Numeric TextBox
</div>
<ejs-numerictextbox value="10"></ejs-numerictextbox>
</div>
</template>
<script>
import Vue from "vue";
import { NumericTextBoxPlugin } from "@syncfusion/ej2-vue-inputs";
Vue.use(NumericTextBoxPlugin);
export default Vue.extend ({});
</script>
Add Button component’s styles as given below in <style>
section of the App.vue
file.
<style>
@import "../node_modules/@syncfusion/ej2-base/styles/material.css";
@import "../node_modules/@syncfusion/ej2-vue-inputs/styles/material.css";
</style>
If you want to refer the combined component styles, please make use of our
CRG
(Custom Resource Generator) in your application.
Now use the npm run dev
command to run the application in the browser.
npm run dev
The below example shows the NumericTextBox.
<template>
<div id="app">
<div class='wrap'>
<ejs-numerictextbox value="10"></ejs-numerictextbox>
</div>
</div>
</template>
<script>
import Vue from "vue";
import { NumericTextBoxPlugin } from "@syncfusion/ej2-vue-inputs";
Vue.use(NumericTextBoxPlugin);
export default {
data () {
return {
}
}
}
</script>
<style>
@import "../../node_modules/@syncfusion/ej2-base/styles/material.css";
@import "../../node_modules/@syncfusion/ej2-vue-inputs/styles/material.css";
.wrap {
margin: 0 auto;
width: 240px;
}
</style>
You can set the minimum and maximum range of values in the NumericTextBox using the min
and
max
properties, so the numeric value should be in the min and max range.
The validation behavior depends on the strictMode
property.
<template>
<div id="app">
<div class='wrap'>
<ejs-numerictextbox id="numeric" ref="numeric_instance" :value="value" :step="step" :min="min" :max="max"></ejs-numerictextbox>
</div>
</div>
</template>
<script>
import Vue from "vue";
import { NumericTextBoxPlugin } from "@syncfusion/ej2-vue-inputs";
Vue.use(NumericTextBoxPlugin);
export default {
data () {
return {
min: 1,
max: 100,
value: 30,
step: 2
}
}
}
</script>
<style>
@import "../../node_modules/@syncfusion/ej2-base/styles/material.css";
@import "../../node_modules/@syncfusion/ej2-vue-inputs/styles/material.css";
.wrap {
margin: 0 auto;
width: 240px;
}
</style>
User can set the format of the NumericTextBox component using format
property. The value will be displayed in the specified format, when the component is in focused out state. For more information about
formatting the value, refer to this link.
The below example demonstrates format the value by using currency format value c2
.
<template>
<div id="app">
<div class='wrap'>
<ejs-numerictextbox id="numeric" ref="numeric_instance" :format="format" :value="value"></ejs-numerictextbox>
</div>
</div>
</template>
<script>
import Vue from "vue";
import { NumericTextBoxPlugin } from "@syncfusion/ej2-vue-inputs";
Vue.use(NumericTextBoxPlugin);
export default {
data () {
return {
format: 'c2',
// sets value to the NumericTextBox
value: 10
}
}
}
</script>
<style>
@import "../../node_modules/@syncfusion/ej2-base/styles/material.css";
@import "../../node_modules/@syncfusion/ej2-vue-inputs/styles/material.css";
.wrap {
margin: 0 auto;
width: 240px;
}
</style>
You can restrict the number of decimals to be entered in the NumericTextBox by using the decimals
and validateDecimalOnType
properties.
So, you can’t enter the number whose precision is greater than the mentioned decimals.
validateDecimalOnType
is false, number of decimals will not be restricted.
Else, number of decimals will be restricted while typing in the NumericTextBox.<template>
<div id="app">
<div class='wrap'>
<ejs-numerictextbox id="strict" :validateDecimalOnType='true' placeholder="ValidateDecimalOnType enabled" floatLabelType="Auto" format='n3' :value="value" :decimals="decimals"></ejs-numerictextbox>
</div>
<div class='wrap'>
<ejs-numerictextbox id="allow" placeholder="ValidateDecimalOnType disabled" floatLabelType="Auto" format='n3' :value="value" :decimals="decimals"></ejs-numerictextbox>
</div>
</div>
</template>
<script>
import Vue from "vue";
import { NumericTextBoxPlugin } from "@syncfusion/ej2-vue-inputs";
Vue.use(NumericTextBoxPlugin);
export default {
data () {
return {
// sets number of decimal places to be allowed by the NumericTextBox
decimals: 3,
value: 10,
}
}
}
</script>
<style>
@import "../../node_modules/@syncfusion/ej2-base/styles/material.css";
@import "../../node_modules/@syncfusion/ej2-vue-inputs/styles/material.css";
.wrap {
margin: 35px auto;
width: 240px;
}
</style>
In NumericTextBox, the value
property supports model binding functionality.
The below example demonstrates model binding functionality with the NumericTextBox and HTML input element.
<template>
<div id="app">
<div class='wrap'>
<div class='e-input-group' style='margin-bottom:30px'>
<input class='e-input' type='text' v-model='value' placeholder='Enter a number' />
</div>
<ejs-numerictextbox v-model="value" :value="value"></ejs-numerictextbox>
</div>
</div>
</div>
</template>
<script>
import Vue from "vue";
import { NumericTextBoxPlugin } from "@syncfusion/ej2-vue-inputs";
Vue.use(NumericTextBoxPlugin);
export default {
data () {
return {
value : 10
}
}
}
</script>
<style>
@import "../../node_modules/@syncfusion/ej2-base/styles/material.css";
@import "../../node_modules/@syncfusion/ej2-vue-inputs/styles/material.css";
.wrap {
margin: 35px auto;
width: 240px;
}
</style>