Implementing automatic number increment in Vue.js forms

I have a field where I can enter numbers

<input type="text" class="form-control" id="validationTooltip01" v-model="verse.number" required>

After saving the number in the database, I would like it to automatically increment.

For example:

If I input 1 and save the form, the next time I open the form the number should be 2 without me having to type it again each time.

Sample Code

data() {
    return {
        verse: {
            number: '',
            heading: '',
            body: '',
            book_id: '',
            chapter_id: '',
        }
    }
},
submit: function(e) {
    axios.post('/my_url', this.verse)
    .then(res => {
        this.isLoading = false;
        $('#exampleModal').modal('toggle');
        this.getVerses.push( res.data.verse );
        this.verse = {
            number: this.verse.number, // here
            heading: '',
            body: '',
            book_id: this.verse.book_id,
            chapter_id: this.verse.chapter_id,
        };
    })
    .catch(error => {
        // handle authentication and validation errors here
        this.errors = error.response.data.errors
        this.isLoading = false
    })
}

I've tried several approaches but none of them worked as expected,

this.verse = {
  number: this.verse.number +=1,
  ...

or

this.verse = {
  number: this.verse.number +1,
  ...

Any suggestions?

Answer №1

According to the type declaration, your current verse number is a String.

this.verse.number + 1 // ''+ 1 = '1'

To ensure correct functionality, it is recommended to set the default value of verse.number in data() {} to 0 instead of an empty string ''. Simply update from number: '', to number: 0, and it will become

this.verse.number + 1 // 0 + 1 = 1

Subsequent increments will then work correctly as follows: 1 + 1 = 2 and so on.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Solving data within an Angular Controller

Recently delving into Angular development, I've found myself caught in a loop trying to solve this particular issue. To give some context, I'm utilizing the MEAN stack through mean.io which includes Angular UI Router functionalities. In my data ...

Divide the information in the table across several pages for easier printing

I have encountered an architectural challenge with my server-side React app. The app renders charts and tables with page breaks in between to allow a puppeteer instance to print the report for users in another application. The issue I'm facing is mak ...

Creating a unique custom view in React Big Calendar with TypeScript

I'm struggling to create a custom view with the React Big Calendar library. Each time I try to incorporate a calendar component like Timegrid into my custom Week component, I run into an error that says react_devtools_backend.js:2560 Warning: React.cr ...

Creating a Node.js API using express and mysql to retrieve various data including record count, page number, and implementing pagination functionality

How can I efficiently retrieve and display total_record_count, page_number, page_size, total_pages, has_more information in the response while implementing pagination given that I am limiting my query to 100 results? What would be the most effective approa ...

Utilizing Redux with React to fetch and handle JSON data from given API endpoint

The assignment I'm working on requires us to fetch JSON data from a specific endpoint called url and display it in an Excel-like table using React, Redux, and Redux-Thunk. I successfully managed to retrieve and display the data in a table by utilizin ...

What could be causing the error message "rawModule is undefined" to appear when attempting to add Vuex modules?

While exploring Vuex modules for the first time, I faced some difficulties. The console error message "rawModule is undefined" had me puzzled as there was not much information available on it. So, let me share with you the problem I encountered and how I ...

Resize a div within another div using overflow scroll and centering techniques

Currently, I am working on implementing a small feature but am facing difficulties with the scroll functionality. My goal is to zoom in on a specific div by scaling it using CSS: transform: scale(X,Y) The issue I am encountering lies in determining the c ...

"Efficiently sharing information in a multi-tenant application: strategies for seamless data transfer between front

In my development of a multi tenancy application using Node.js/Mongoose and React, I made the decision to utilize a single database for all users. The main collection, dubbed companies, serves as storage for basic company data and includes a unique compan ...

Combining an array of intricate data models to create

Currently, my setup involves using Entity Framework 7 in conjunction with ASP.NET MVC 5. In my application, I have various forms that resemble the design showcased in this image. By clicking on the "new" button within these forms, a Bootstrap modal like t ...

Breaking down the jQuery datepicker into individual fields for Day, Month, and Year

Currently, I am using the Contact Form 7 plugin on my Wordpress website, which I believe uses the jQuery datepicker for its date fields (please correct me if I'm mistaken). My query is about splitting the user input box into three separate elements ( ...

Simply touch this element on Android to activate

Currently utilizing the following code: <a href="http://www...." onmouseover="this.click()">Link</a> It is evident that this code does not work with the Android Browser. What javascript code will function properly with the Android Browser as ...

When the properties change, React Router Redux does not get rendered

I am encountering a challenge with using react router redux, where everything seems to be working well except for rendering when props change. Index.js import React from 'react'; import ReactDOM from 'react-dom'; import {Provider} fro ...

Angular 2 - Changes in component properties not reflected in view

I'm currently delving into Angular 2 and as far as I know, interpolated items in the view are supposed to automatically update when their corresponding variable changes in the model. However, in the following code snippet, I'm not observing this ...

Can one retrieve an express session using the sessionID given?

I have a NodeJS Express application with express-session that works well, however, it needs to be compatible with a cookie-less PhoneGap app. My question is: Can I access the data in an express session using the sessionID? I was thinking of adding the se ...

Learn the process of integrating VueJS with RequireJS

I am attempting to set up VueJS with RequireJS. Currently, I am using the following VueJS library: . Below is my configuration file for require: require.config({ baseUrl : "js", paths : { jquery : "libs/jquery-3.2.1.min", fullcalendar : "libs/ful ...

Styling GeoJSON data in the React Leaflet mapping library can greatly enhance the

I successfully incorporated the leaflet map library into my react project. You can check it out here. I also created a geojson map component as shown below: class MapContainer extends React.Component { state = { greenIcon: { lat: 8.3114, ...

What could be the reason for scope.$watch failing to function properly?

My AngularJS directive has a template with two tags - an input and an empty list. I am trying to watch the input value of the first input tag. However, the $watch() method only gets called once during initialization of the directive and any subsequent chan ...

Top button disappears in Chromium browser after fade-in effect

I've encountered a very peculiar issue. Whenever I use the page down or fragmented identifier to jump to a specific div, my "go to top" image disappears. Here is the code snippet: HTML: <a title="Go to top" href="#" class="back-to-top"></ ...

What advantages does separating vendors bring to Laravel Mix?

My initial bundle size was 246kb, and I wanted to reduce it further. After researching, I came across the method of extracting Vue and jQuery using mix.extract(['vue', 'jquery']);. Implementing this in my webpack.mix.js file resulted in ...

What is the process for setting up a subrouter using React Router v6?

This is the current React Router setup I am using: const router = createBrowserRouter([ { path: "/", element: ( <Page activeNav="home" > <Home /> </Page> ) }, { ...