Assign an Array to an input

0

I have to edit the details of a referral guide for which I have the following code that loads the values of the detail table

<tbody>
    @foreach($detalles as $det)
        <tr>            
            <input type="hidden" name="id_detalle[]" value="{{ $det->id_detalle }}">
            <td><input style=" width : 60px; border: none " type="number" name="cantidad[]" id="cantidad" value="{{ $det->cantidad }}"></td>
            <td><input style=" width : 600px; border: none " type="text" readonly name="descripcion[]" id="descripcion[]" value="{{ $det->descripcion }}"></td>
            <td><input style=" width : 90px; border: none " type="number" step="0.01" name="v_unitario[]" id="v_unitario" value="{{ $det->v_unitario }}"></td>
            <td><input style=" width : 90px; border: none " type="number" readonly id="v_parcial" name="v_parcial[]" value="{{ $det->v_parcial }}"></td>
        </tr>
    @endforeach

The next thing is to capture the quantity and v_unit values and in a v_partial array what I do is multiply v_parcial = amount * v_unit as follows

function cantidad()
{
    //Obtengo todos los campos con el nombre cantidad[]
    var cantidad = document.getElementsByName("cantidad[]");
    var v_unitario = document.getElementsByName("v_unitario[]");
    //Creo el arreglo donde almaceno sus valores
    var cant = [];
    var unit = [];
    var parc = [];
    //Recorro todos los nodos que encontre que coinciden con ese nombre
    for(var i=0;i<cantidad.length;i++){
        //Añado el valor que contienen los campos
        cant.push(cantidad[i].value);
        unit.push(v_unitario[i].value);
        parc[i] = cant[i]*unit[i];
    }
}

my question is how to store the values of the "parc" array in the

<input id="v_parcial" name="v_parcial[]">
    
asked by Jonathan Vincent 16.06.2018 в 16:08
source

2 answers

0

dddenis it is used to perform other calculations and it is also to show and if it would show it would be as follows

    
answered by 17.06.2018 в 15:04
0

The value is set in the same way that you read the other values, with the getByName array, something like this:

v_parcial[i].value = cantidad[i].value * v_unitario[i].value;

How and when you calculate that you will see, in this example I put an event on change to the 2 columns that change the result.

There is a lot of room to optimize, take it as a start.

// defino globales
 var cantidad , v_unitario, v_parcial = [];

// helper
function addEventHandler(elem, eventType, handler) {
    if (elem.addEventListener)
        elem.addEventListener (eventType, handler, false);
    else if (elem.attachEvent)
        elem.attachEvent ('on' + eventType, handler); 
}

// on load
addEventHandler(document, 'DOMContentLoaded', function() {
    //Obtengo todos los campos con el nombre cantidad[]
    cantidad = document.getElementsByName("cantidad[]");
    v_unitario = document.getElementsByName("v_unitario[]");
    
    //console.log(cantidad , v_unitario, v_parcial);

    for(var i=0;i<cantidad.length;i++){
      addEventHandler(cantidad[i], 'change', function() {
        calculaCantidad();
      });
      addEventHandler(v_unitario[i], 'change', function() {
        calculaCantidad();
      });
    }
});
   
    
function calculaCantidad()
{
    //Obtengo todos los campos con el nombre cantidad[]
    cantidad = document.getElementsByName("cantidad[]");
    v_unitario = document.getElementsByName("v_unitario[]");
    v_parcial = document.getElementsByName("v_parcial[]");

    //Creo el arreglo donde almaceno sus valores
    var cant = [];
    var unit = [];
    var parc = [];
    //Recorro todos los nodos que encontre que coinciden con ese nombre
    for(var i=0;i<cantidad.length;i++){
        //Añado el valor que contienen los campos
        cant.push(cantidad[i].value);
        unit.push(v_unitario[i].value);
        parc[i] = cant[i]*unit[i];
        
        v_parcial[i].value = cantidad[i].value * v_unitario[i].value;
    }
}
    

calculaCantidad();
input {
background:#fe0;
max-width:320px!important;
}
tbody {
background:#fafafa;
}
<table>
<tbody>
    @foreach($detalles as $det)
        <tr>            
            <input type="hidden" name="id_detalle[]" value="{{ $det->id_detalle }}">
            <td><input style=" width : 60px; border: none " type="number" name="cantidad[]" id="cantidad" value="1"></td>
            <td><input style=" width : 500px; border: none " type="text" readonly name="descripcion[]" id="descripcion[]" value="{{ $det->descripcion }}"></td>
            <td><input style=" width : 90px; border: none " type="number" step="0.01" name="v_unitario[]" id="v_unitario" value="5"></td>
            <td><input style=" width : 90px; border: none " type="number" readonly id="v_parcial" name="v_parcial[]" value=""></td>
        </tr>

        <tr>            
            <input type="hidden" name="id_detalle[]" value="{{ $det->id_detalle }}">
            <td><input style=" width : 60px; border: none " type="number" name="cantidad[]" id="cantidad" value="{{ $det->cantidad }}"></td>
            <td><input style=" width : 500px; border: none " type="text" readonly name="descripcion[]" id="descripcion[]" value="{{ $det->descripcion }}"></td>
            <td><input style=" width : 90px; border: none " type="number" step="0.01" name="v_unitario[]" id="v_unitario" value="{{ $det->v_unitario }}"></td>
            <td><input style=" width : 90px; border: none " type="number" readonly id="v_parcial" name="v_parcial[]" value="{{ $det->v_parcial }}"></td>
        </tr>
        <tr>            
            <input type="hidden" name="id_detalle[]" value="{{ $det->id_detalle }}">
            <td><input style=" width : 60px; border: none " type="number" name="cantidad[]" id="cantidad" value="{{ $det->cantidad }}"></td>
            <td><input style=" width : 500px; border: none " type="text" readonly name="descripcion[]" id="descripcion[]" value="{{ $det->descripcion }}"></td>
            <td><input style=" width : 90px; border: none " type="number" step="0.01" name="v_unitario[]" id="v_unitario" value="{{ $det->v_unitario }}"></td>
            <td><input style=" width : 90px; border: none " type="number" readonly id="v_parcial" name="v_parcial[]" value="{{ $det->v_parcial }}"></td>
        </tr>
        <tr>            
            <input type="hidden" name="id_detalle[]" value="{{ $det->id_detalle }}">
            <td><input style=" width : 60px; border: none " type="number" name="cantidad[]" id="cantidad" value="{{ $det->cantidad }}"></td>
            <td><input style=" width : 500px; border: none " type="text" readonly name="descripcion[]" id="descripcion[]" value="{{ $det->descripcion }}"></td>
            <td><input style=" width : 90px; border: none " type="number" step="0.01" name="v_unitario[]" id="v_unitario" value="{{ $det->v_unitario }}"></td>
            <td><input style=" width : 90px; border: none " type="number" readonly id="v_parcial" name="v_parcial[]" value="{{ $det->v_parcial }}"></td>
        </tr>
        <tr>            
            <input type="hidden" name="id_detalle[]" value="{{ $det->id_detalle }}">
            <td><input style=" width : 60px; border: none " type="number" name="cantidad[]" id="cantidad" value="{{ $det->cantidad }}"></td>
            <td><input style=" width : 500px; border: none " type="text" readonly name="descripcion[]" id="descripcion[]" value="{{ $det->descripcion }}"></td>
            <td><input style=" width : 90px; border: none " type="number" step="0.01" name="v_unitario[]" id="v_unitario" value="{{ $det->v_unitario }}"></td>
            <td><input style=" width : 90px; border: none " type="number" readonly id="v_parcial" name="v_parcial[]" value="{{ $det->v_parcial }}"></td>
        </tr>
@endforeach
</tbody>
</table>
    
answered by 17.06.2018 в 15:59