I am developing an E-Commerce using Javascript. In the section of the products of the page, I want to implement the functionality to change the order of the products setting the number in the corresponding input. You can see an image to get an idea of the interface.
The algorithm I looking for has to reorder all the products if the user changes the input of the Orden column.
For example:
- The user sets the input Orden of the product 0003-0020 to 11 and clicks on the Guardar button.
- Then, the algorithm has to change the position of this product and put it second on the list. Updating its orden input value to 20.
- The algorithm has to update all the following products, increasing their positions +10.
If the user sets a value that already exists, the product will place below the product that already has the position entered. The position values are always multiple of 10.
The data is structured in an ArrayList that contains the product reference and its position: Its name is lista_variantes.
0: {numero: "0003-0007", posicion: 10}
1: {numero: "0003-0006", posicion: 20}
2: {numero: "0003-0004", posicion: 30}
3: {numero: "0003-0008", posicion: 40}
4: {numero: "0003-0014", posicion: 50}
5: {numero: "0003-0016", posicion: 60}
6: {numero: "0003-0017", posicion: 70}
7: {numero: "0003-0018", posicion: 80}
And then using this map, I update the Database and read the product information in the correct order. I have implemented correctly this, I only want the algorithm to achieve the example behaviour.