Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
214 views
in Technique[技术] by (71.8m points)

angularjs - How to add a Consecutive Number in Angular JS

Every time the customer creates a "Proveedor (provider)", they need to fill the Numero de proveedor field in order to continue as you can see in this picture (the top-left input field):

Picture of the customer view

The problem is that the DB has around 1000+ providers, and the "Numero de proveedor" was randomly selected (I've providers with 1234567890 number) so, I want to see how can I set the consecutive number not being used already in that field when the user access to that view.

What I want is to access to the create view of Proveedores and see that field filled automatically with a number not being used.

This is my code:

<div class="form-group col-md-4">
    <label for="razon_social">Numero de proveedor</label>
    <input ng-model="proveedor.codigo_empresa" class="form-control" type="number" placeholder="Codigo empresa">
</div>

If the solution can be in AngularJS it would be great!

Thanks for the help.

question from:https://stackoverflow.com/questions/65849638/how-to-add-a-consecutive-number-in-angular-js

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I would create a server side request for a new Proveedore, and that function can generate and return a new unused number.

To generate a new number you can do that a couple of different ways:
1: generate a random number and see if it exists in the database. If it does generate another and test, repeat until you find an unused number.
2: Generate a random number and if it exists increment that number by one and test if that exists. Repeat incrementing and testing until you find one that doesn't exist.

There are other ways also. If you don't need a random number then you could restart numbering at the lowest allowed, such as 1, and test if it's available. Increment until you find an unused one. Then save this for the next request.

After your function finds an used number return that to the client - browser.

Server side would be done in Laravel (php). AngularJS browser side would be a simple http.get request, depending on the structure of your code you would do something like:

$http.get('/api/newproveedore)
        .success(function(data) {
            $scope.proveedore = data;
        })
        .error(function(data) {
          console.log('Error: ' + data);
        });

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...