Search results

Getting started with Vue NumericTextBox component

20 Mar 2023 / 6 minutes to read

The following section explains the required steps to build the NumericTextBox component with its basic usage in step by step procedure.

Prerequisites

System requirements for Syncfusion Vue UI components

Installation and configuration

You can use Vue CLI to setup your vue applications. To install Vue CLI use the following command.

Copied to clipboard
npm install -g @vue/cli

Start a new project using below Vue CLI command.

Copied to clipboard
vue init webpack-simple quickstart
cd quickstart
npm install

Adding syncfusion packages

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

Copied to clipboard
npm install @syncfusion/ej2-vue-inputs –save

Registering Vue Component

For Registering Vue Component two ways are available. They are as follows.

  • Vue.use()
  • Vue.component()

Using Vue.use()

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.

Copied to clipboard
import { NumericTextBoxPlugin } from "@syncfusion/ej2-vue-inputs";

Vue.use(NumericTextBoxPlugin);

By Registering Component Plugin in Vue, all child directives are also globally registered.

Using Vue.component()

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.

Copied to clipboard
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.

Adding NumericTextBox component

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.

Copied to clipboard
<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>

Adding CSS Reference

Add Button component’s styles as given below in <style> section of the App.vue file.

Copied to clipboard
<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.

Run the application

Now use the npm run dev command to run the application in the browser.

Copied to clipboard
npm run dev

The below example shows the NumericTextBox.

Source
Preview
app.vue
Copied to clipboard
<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>

Range validation

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.

Source
Preview
app.vue
Copied to clipboard
<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>

Formatting the value

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.

Source
Preview
app.vue
Copied to clipboard
<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>

Precision of numbers

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.

  • If validateDecimalOnType is false, number of decimals will not be restricted. Else, number of decimals will be restricted while typing in the NumericTextBox.
Source
Preview
app.vue
Copied to clipboard
<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>

Model binding

In NumericTextBox, the value property supports model binding functionality. The below example demonstrates model binding functionality with the NumericTextBox and HTML input element.

Source
Preview
app.vue
Copied to clipboard
<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>

See Also