layout | title | description | control | platform | documentation | domainurl |
---|---|---|---|---|---|---|
post |
Getting started with Vue Numerictextbox component | Syncfusion |
Checkout and learn about Getting started with Vue Numerictextbox component of Syncfusion Essential JS 2 and more details. |
Getting started |
ej2-vue |
ug |
This article provides a step-by-step guide for setting up a Vue 2 project using Vue-CLI and integrating the Syncfusion Vue Numerictextbox component using the Composition API / Options API.
To get start quickly with Vue Numerictextbox component, you can check on this video:
{% youtube "https://www.youtube.com/watch?v=q97G43pPSnA" %}
System requirements for Syncfusion Vue UI components
To generate a Vue 2 project using Vue-CLI, use the vue create command. Follow these steps to install Vue CLI and create a new project:
npm install -g @vue/cli
vue create quickstart
cd quickstart
npm run serve
or
yarn global add @vue/cli
vue create quickstart
cd quickstart
yarn run serve
When creating a new project, choose the option Default ([Vue 2] babel, eslint)
from the menu.
Once the quickstart
project is set up with default settings, proceed to add Syncfusion components to the project.
Syncfusion packages are available at npmjs.com. To use Vue components, install the required npm package.
This article uses the Vue Numerictextbox component as an example. Install the @syncfusion/ej2-vue-inputs
package by running the following command:
npm install @syncfusion/ej2-vue-inputs --save
or
yarn add @syncfusion/ej2-vue-inputs
You can import themes for the Syncfusion Vue component in various ways, such as using CSS or SASS styles from npm packages, CDN, CRG and Theme Studio. Refer to themes topic to know more about built-in themes and different ways to refer to themes in a Vue project.
In this article, the Material
theme is applied using CSS styles, which are available in installed packages. The necessary Material
CSS styles for the Numerictextbox component and its dependents were imported into the <style>
section of src/App.vue file.
{% tabs %} {% highlight html tabtitle="~/src/App.vue" %}
<style> @import "../node_modules/@syncfusion/ej2-base/styles/material.css"; @import "../node_modules/@syncfusion/ej2-vue-inputs/styles/material.css"; </style>{% endhighlight %} {% endtabs %}
If you want to refer the combined component styles, please make use of our
CRG
(Custom Resource Generator) in your application.
Follow the below steps to add the Vue Numerictextbox component using Composition API
or Options API
:
1. First, import and register the Numerictextbox component in the script
section of the src/App.vue file. If you are using the Composition API
, you should add the setup
attribute to the script
tag to indicate that Vue will be using the Composition API
.
{% tabs %} {% highlight html tabtitle="Composition API (~/src/App.vue)" %} {% raw %}
<script setup> import { NumericTextBoxComponent as EjsNumerictextbox} from "@syncfusion/ej2-vue-inputs"; </script>{% endraw %} {% endhighlight %} {% highlight html tabtitle="Options API (~/src/App.vue)" %}
<script> import { NumericTextBoxComponent } from "@syncfusion/ej2-vue-inputs"; export default { name: "App", components: { "ejs-numerictextbox':":NumericTextBoxComponent }, } </script>{% endhighlight %} {% endtabs %}
2. In the template
section, define the Numerictextbox component with the value property.
{% tabs %} {% highlight html tabtitle="~/src/App.vue" %}
{% endhighlight %} {% endtabs %}
Here is the summarized code for the above steps in the src/App.vue file:
{% tabs %}
{% highlight html tabtitle="Composition API (/src/App.vue)" %}
{% include code-snippet/numeric-textbox/getting-started/getting-started-cs2/app-composition.vue %}
{% endhighlight %}
{% highlight html tabtitle="Options API (/src/App.vue)" %}
{% include code-snippet/numeric-textbox/getting-started/getting-started-cs2/app.vue %}
{% endhighlight %}
{% endtabs %}
To run the project, use the following command:
npm run serve
or
yarn run serve
{% previewsample "page.domainurl/code-snippet/numeric-textbox/getting-started/getting-started-cs2" %}
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.
{% tabs %}
{% highlight html tabtitle="Composition API (/src/App.vue)" %}
{% include code-snippet/numeric-textbox/getting-started/range-validation-cs1/app-composition.vue %}
{% endhighlight %}
{% highlight html tabtitle="Options API (/src/App.vue)" %}
{% include code-snippet/numeric-textbox/getting-started/range-validation-cs1/app.vue %}
{% endhighlight %}
{% endtabs %}
{% previewsample "page.domainurl/code-snippet/numeric-textbox/getting-started/range-validation-cs1" %}
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
.
{% tabs %}
{% highlight html tabtitle="Composition API (/src/App.vue)" %}
{% include code-snippet/numeric-textbox/getting-started/formating-value-cs1/app-composition.vue %}
{% endhighlight %}
{% highlight html tabtitle="Options API (/src/App.vue)" %}
{% include code-snippet/numeric-textbox/getting-started/formating-value-cs1/app.vue %}
{% endhighlight %}
{% endtabs %}
{% previewsample "page.domainurl/code-snippet/numeric-textbox/getting-started/formating-value-cs1" %}
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.
{% tabs %}
{% highlight html tabtitle="Composition API (/src/App.vue)" %}
{% include code-snippet/numeric-textbox/getting-started/precision-numbers-cs1/app-composition.vue %}
{% endhighlight %}
{% highlight html tabtitle="Options API (/src/App.vue)" %}
{% include code-snippet/numeric-textbox/getting-started/precision-numbers-cs1/app.vue %}
{% endhighlight %}
{% endtabs %}
{% previewsample "page.domainurl/code-snippet/numeric-textbox/getting-started/precision-numbers-cs1" %}
In NumericTextBox, the value
property supports model binding functionality.
The below example demonstrates model binding functionality with the NumericTextBox and HTML input element.
{% tabs %}
{% highlight html tabtitle="Composition API (/src/App.vue)" %}
{% include code-snippet/numeric-textbox/getting-started/model-cs1/app-composition.vue %}
{% endhighlight %}
{% highlight html tabtitle="Options API (/src/App.vue)" %}
{% include code-snippet/numeric-textbox/getting-started/model-cs1/app.vue %}
{% endhighlight %}
{% endtabs %}
{% previewsample "page.domainurl/code-snippet/numeric-textbox/getting-started/model-cs1" %}
- How to perform custom validation using FormValidator
- How to customize the UI appearance of the control
- How to customize the spin button’s up and down arrow
- How to customize the step value and hide spin buttons
- How to prevent nullable input in NumericTextBox
- How to maintain trailing zeros in NumericTextBox