Need to Shrink Functions

Viewed 43

I'm doing a system related to recycled material. I pay to the person who brings the material, and there are 12 kinds of recycled material.

I need to shrink the functions of the buttons to be more friendly.

This is my modal

<div
    class="modal fade bd-example-modal-lg"
    id="modalCRUD"
    tabindex="-1"
    role="dialog"
    aria-labelledby="exampleModalLabel"
    aria-hidden="true"
>
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel"></h5>
                <button
                    type="button"
                    class="close"
                    data-dismiss="modal"
                    aria-label="Close"
                >
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <form id="formUsuarios">
                <div class="modal-body">
                    <div id="s">
                        <div class="row">
                            <div class="col">
                                <div class="form-group">
                                    <label for="" class="col-form-label"
                                        >Nombres</label
                                    >
                                    <input
                                        type="text"
                                        class="form-control"
                                        id="nombre"
                                        readonly
                                    />
                                </div>
                            </div>
                            <div class="col">
                                <div class="form-group">
                                    <label for="" class="col-form-label"
                                        >Apellidos</label
                                    >
                                    <input
                                        type="text"
                                        class="form-control"
                                        id="apellido"
                                        readonly
                                    />
                                </div>
                            </div>
                            <div class="col">
                                <div class="form-group">
                                    <label for="" class="col-form-label"
                                        >DNI</label
                                    >
                                    <input
                                        type="number"
                                        class="form-control"
                                        id="dni"
                                        readonly
                                    />
                                </div>
                            </div>
                            <div class="col">
                                <div class="form-group">
                                    <label for="" class="col-form-label"
                                        >Saldo</label
                                    >
                                    <input
                                        type="number"
                                        class="form-control"
                                        id="saldo"
                                        step="any"
                                        readonly
                                    />
                                </div>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col">
                                <div class="form-group">
                                    <button
                                        type="button"
                                        class="botonimagen1"
                                        onclick="myFunction1()"
                                    ></button>
                                    <button
                                        type="button"
                                        class="botonimagen1"
                                        onclick="myFunction2()"
                                    ></button>
                                    <button
                                        type="button"
                                        class="botonimagen1"
                                        onclick="myFunction3()"
                                    ></button>
                                    <button
                                        type="button"
                                        class="botonimagen1"
                                        onclick="myFunction4()"
                                    ></button>
                                    <button
                                        type="button"
                                        class="botonimagen1"
                                        onclick="myFunction5()"
                                    ></button>
                                    <button
                                        type="button"
                                        class="botonimagen1"
                                        onclick="myFunction6()"
                                    ></button>
                                    <button
                                        type="button"
                                        class="botonimagen1"
                                        onclick="myFunction7()"
                                    ></button>
                                    <button
                                        type="button"
                                        class="botonimagen1"
                                        onclick="myFunction8()"
                                    ></button>
                                    <button
                                        type="button"
                                        class="botonimagen1"
                                        onclick="myFunction9()"
                                    ></button>
                                    <button
                                        type="button"
                                        class="botonimagen1"
                                        onclick="myFunction10()"
                                    ></button>
                                    <button
                                        type="button"
                                        class="botonimagen1"
                                        onclick="myFunction11()"
                                    ></button>
                                    <button
                                        type="button"
                                        class="botonimagen1"
                                        onclick="myFunction12()"
                                    ></button>
                                </div>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col">
                                <div class="form-group">
                                    <input
                                        type="text"
                                        class="form-control"
                                        id="material1"
                                        readonly
                                    />
                                </div>
                            </div>
                            <div class="col">
                                <div class="form-group">
                                    <input
                                        type="text"
                                        class="form-control"
                                        id="precio1"
                                        readonly
                                    />
                                </div>
                            </div>
                            <div class="col">
                                <div class="form-group">
                                    <input
                                        type="number"
                                        class="form-control"
                                        step="any"
                                        oninput="subtotal1a()"
                                        id="peso1"
                                        readonly
                                    />
                                </div>
                            </div>
                            <div class="col">
                                <div class="form-group">
                                    <div class="input-icon input-icon-right">
                                        <input
                                            type="number"
                                            class="form-control"
                                            value="0"
                                            oninput="sumar()"
                                            id="subtotal1"
                                            readonly
                                        />
                                        <i>CC</i>
                                    </div>
                                </div>
                            </div>
                            <div class="col">
                                <div class="form-group">
                                    <input
                                        type="number"
                                        class="form-control"
                                        id="quitar1"
                                        readonly
                                    />
                                </div>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col">
                                <div class="form-group">
                                    <input
                                        type="text"
                                        class="form-control"
                                        id="material2"
                                        readonly
                                    />
                                </div>
                            </div>
                            <div class="col">
                                <div class="form-group">
                                    <input
                                        type="text"
                                        class="form-control"
                                        id="precio2"
                                        readonly
                                    />
                                </div>
                            </div>
                            <div class="col">
                                <div class="form-group">
                                    <input
                                        type="number"
                                        class="form-control"
                                        step="any"
                                        oninput="subtotal2a()"
                                        id="peso2"
                                        readonly
                                    />
                                </div>
                            </div>
                            <div class="col">
                                <div class="form-group">
                                    <div class="input-icon input-icon-right">
                                        <input
                                            type="number"
                                            class="form-control"
                                            value="0"
                                            oninput="sumar()"
                                            id="subtotal2"
                                            readonly
                                        />
                                        <i>CC</i>
                                    </div>
                                </div>
                            </div>
                            <div class="col">
                                <div class="form-group">
                                    <input
                                        type="number"
                                        class="form-control"
                                        id="quitar2"
                                        readonly
                                    />
                                </div>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col">
                                <div class="form-group">
                                    <input
                                        type="text"
                                        class="form-control"
                                        id="material3"
                                        readonly
                                    />
                                </div>
                            </div>
                            <div class="col">
                                <div class="form-group">
                                    <input
                                        type="text"
                                        class="form-control"
                                        id="precio3"
                                        readonly
                                    />
                                </div>
                            </div>

                            <div class="col">
                                <div class="form-group">
                                    <input
                                        type="number"
                                        class="form-control"
                                        step="any"
                                        oninput="subtotal3a()"
                                        id="peso3"
                                        readonly
                                    />
                                </div>
                            </div>

                            <div class="col">
                                <div class="form-group">
                                    <div class="input-icon input-icon-right">
                                        <input
                                            type="number"
                                            class="form-control"
                                            value="0"
                                            oninput="sumar()"
                                            id="subtotal3"
                                            readonly
                                        />
                                        <i>CC</i>
                                    </div>
                                </div>
                            </div>

                            <div class="col">
                                <div class="form-group">
                                    <input
                                        type="number"
                                        class="form-control"
                                        id="quitar3"
                                        readonly
                                    />
                                </div>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col">
                                <div class="form-group">
                                    <div class="input-icon input-icon-right">
                                        <input
                                            type="number"
                                            class="form-control"
                                            placeholder="0"
                                            id="carga"
                                            name="carga"
                                            min="0"
                                            step="any"
                                            style="
                                                font-size: 40px;
                                                text-align: center;
                                            "
                                            size="13"
                                            title="Currency"
                                            pattern="^\d+(?:\.\d{1,2})?$"
                                        />
                                        <i>CC</i>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button
                        type="button"
                        class="btn btn-light"
                        data-dismiss="modal"
                    >
                        Cancelar
                    </button>
                    <button type="submit" id="btnGuardar" class="btn btn-dark">
                        Guardar
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>

this is my script

<script>
function myFunction1() {
    if (document.getElementById("material1").value == "") {
        document.getElementById("material1").value = "1 PET";
        document.getElementById("precio1").value = 1.5;
        document.getElementById('peso1').readOnly = false;
        document.getElementById("peso1").focus();
    } else if (document.getElementById("material2").value == "") {
        document.getElementById("material2").value = "1 PET";
        document.getElementById("precio2").value = 1.5;
        document.getElementById('peso2').readOnly = false;
        document.getElementById("peso2").focus();
    } else if (document.getElementById("material3").value == "") {
        document.getElementById("material3").value = "1 PET";
        document.getElementById("precio3").value = 1.5;
        document.getElementById('peso3').readOnly = false;
        document.getElementById("peso3").focus();
    } else {
        document.getElementById("material4").value = "1 PET";
        document.getElementById("precio4").value = 1.5;
        document.getElementById('peso4').readOnly = false;
        document.getElementById("peso4").focus();
    }

};

function myFunction2() {
    if (document.getElementById("material1").value == "") {
        document.getElementById("material1").value = "2 PEAD";
        document.getElementById("precio1").value = 2.0;
        document.getElementById('peso1').readOnly = false;
        document.getElementById("peso1").focus();
    } else if (document.getElementById("material2").value == "") {
        document.getElementById("material2").value = "2 PEAD";
        document.getElementById("precio2").value = 2.0;
        document.getElementById('peso2').readOnly = false;
        document.getElementById("peso2").focus();
    } else if (document.getElementById("material3").value == "") {
        document.getElementById("material3").value = "2 PEAD";
        document.getElementById("precio3").value = 2.0;
        document.getElementById('peso3').readOnly = false;
        document.getElementById("peso3").focus();
    } else {
        document.getElementById("material4").value = "2 PEAD";
        document.getElementById("precio4").value = 2.0;
        document.getElementById('peso4').readOnly = false;
        document.getElementById("peso4").focus();
    }

};

function myFunction3() {
    if (document.getElementById("material1").value == "") {
        document.getElementById("material1").value = "3 PVC";
        document.getElementById("precio1").value = 1.0;
        document.getElementById('peso1').readOnly = false;
        document.getElementById("peso1").focus();
    } else if (document.getElementById("material2").value == "") {
        document.getElementById("material2").value = "3 PVC";
        document.getElementById("precio2").value = 1.0;
        document.getElementById('peso2').readOnly = false;
        document.getElementById("peso2").focus();
    } else if (document.getElementById("material3").value == "") {
        document.getElementById("material3").value = "3 PVC";
        document.getElementById("precio3").value = 1.0;
        document.getElementById('peso3').readOnly = false;
        document.getElementById("peso3").focus();
    } else {
        document.getElementById("material4").value = "23 PVC";
        document.getElementById("precio4").value = 1.0;
        document.getElementById('peso4').readOnly = false;
        document.getElementById("peso4").focus();
    }

};

Screenshot: Modal

I only showed programing 3 of 12 products. How can I minimize the functions of the buttons? I have to repeat for every row, but how can I add a row every time programatically?

1 Answers

JavaScript functions are able to return other functions. In this case, to generate the button callbacks you want, you can use this. The concept of passing functions to other functions is called "higher order functions." In your case, you can try this:

function myFunctionFactory(material, precio) {
    return function () {
        if (document.getElementById('material1').value == '') {
            document.getElementById('material1').value = material;
            document.getElementById('precio1').value = precio;
            document.getElementById('peso1').readOnly = false;
            document.getElementById('peso1').focus();
        } else if (document.getElementById('material2').value == '') {
            document.getElementById('material2').value = material;
            document.getElementById('precio2').value = precio;
            document.getElementById('peso2').readOnly = false;
            document.getElementById('peso2').focus();
        } else if (document.getElementById('material3').value == '') {
            document.getElementById('material3').value = material;
            document.getElementById('precio3').value = precio;
            document.getElementById('peso3').readOnly = false;
            document.getElementById('peso3').focus();
        } else {
            document.getElementById('material4').value = material;
            document.getElementById('precio4').value = precio;
            document.getElementById('peso4').readOnly = false;
            document.getElementById('peso4').focus();
        }
    };
}

and you could even reduce it further like this, but I didn't want to change too much:

function myFunctionFactory(material, precio) {
    return function () {
        for (i of [1, 2, 3, 4]) {
            if (document.getElementById(`material${i}`).value == '') {
                document.getElementById(`material${i}`).value = material;
                document.getElementById(`precio${i}`).value = precio;
                document.getElementById(`peso${i}`).readOnly = false;
                document.getElementById(`peso${i}`).focus();
                break;
            }
        }
    };
}

With this code, myFunctionFactory is a function that returns another function. If you call myFunctionFactory('1 PET', 1.5), then it will return a callback function that uses '1 PET' and 1.5 in the right places, and still has no arguments.

You can use it in the html code like this:

 <button
    type="button"
    class="botonimagen1"
    onclick="myFunctionFactory('1 PET', 1.5)"
></button>

That will help you reduce the size of the JavaScript code a lot. For the HTML, I highly recommend looking into a standard framework like React, Vue, or Angular (there are a lot). Web design frameworks can take a bit of time and effort to learn, but they are powerful and can help make your code simpler and easier to work with once you understand them.

Related