Take input value within datatable

1

People I have a table that loads medications from BD , it has a input in to indicate the desired amount of each, the question is that I can not take the value entered in input .

I leave the code just in case someone could give me a hand.

Thanks in advance.

function obtener_medicamentos() {
$.ajax({
    type: "POST",
    dataType: "json",
    url: "WebService.asmx/obtenerMedicamentos",
    data: {},
    success: function (data) {
        if (data == 1) {
            sinSession()
        } else {
            var dataTableVariable = $("#tbm").DataTable({
                "lengthMenu": [[5], [5]],
                destroy: true,
                data: data,
                columns: [
                    //{ 'data': 'idItem' },
                    { 'data': 'dsItem' },
                    { 'data': 'unidad' },
                    { 'data': 'stock' },
                    { 'defaultContent': '<input type="text" id="cant" CssClass="cant" value="0">' },
                    { 'defaultContent': "<button type='button' class='btn btn-xs btn-info ag' title='Agregar Medicamento'><i class='fa fa-check' aria-hidden='true'> Agregar</i></button>" }
                ],
                "language": { "sUrl": "fonts/datatable_espanol.txt" }
            })
            bServerSide: true;
            bProcessing: true;
            $("#tbm tbody").off('click')
            agregar_med("#tbm tbody", dataTableVariable)
        }
    }, error: function () {
        alertify.alert("Ha ocurrido un error")
    }
})
}

var agregar_med = function (tbody, table) {
$(tbody).on("click", "button.ag", function () {
    //asi me toma siempre 0 que es el valor introducido por defecto al cargar la tabla
    var fila = table.row($(this).parents("tr")).data()
    //alert("Id: " + fila.idItem + " Med: " + fila.dsItem + " Unidad: " + fila.unidad + " Stock: " + fila.stock + " Cantidad: " + fila.cantidad )

    //cualquiera de las 2 formas toma el valor del input de la primera fila sin importar que botón apriete
    var dato = table.$('input').val();
    var dato = table.$('#cant').val();
    alert(dato)
})
}
    
asked by DavidC 15.09.2017 в 14:29
source

1 answer

1

In the end, solve it with this line inside the function add_med ()

var cant = $(this).parents("tr").find('#cant').val();
    
answered by 15.09.2017 в 18:21