I want to add a checkTodo that marks all the rows of a datatable. each row has its own checkbox that is rendered according to an attribute called 'selectable'. At the moment I managed to totalize the amount of voucher selectable and the sum of the amounts but I need to locate the individual check to be able to mark it and add the class 'selected' to the corresponding rows. I leave my code to see if someone can help me
function obtener_comprobantes_actualizados(idcuenta, ids, ids2, ids3) {
var cant = 0;
var monto = 0;
$("#pnlDeuda").show();
$.ajax({
type: "POST",
dataType: "json",
url: "obtenerDeuda",
data: { 'idCuenta': idcuenta, 'idSubsistema': ids, 'idSubsistema2': ids2, 'idSubsistema3': ids3 },
success: function (data) {
var dataTableVariable = $("#tbComp").DataTable({
"footerCallback": function (row, data, start, end, display) {
var api = this.api(), data;
var intVal = function (i) {
return typeof i === 'string' ?
i.replace(/[\$,]/g, '') * 1 :
typeof i === 'number' ?
i : 0;
};
total = api
.column(6)
.data()
.reduce(function (a, b) {
return intVal(a) + intVal(b);
}, 0);
$("#deudaTotal").text("Deuda Total $ " + total);
},
"lengthMenu": [[10, 15], [10, 15]],
"searching": false,
destroy: true,
data: data,
columns: [
{ 'data': 'idComprobante' },
{
'data': "seleccionable",
"searchable": false,
"orderable": false,
"render": function (data, type, row) {
if (data === 1) {
return '<input type="checkbox" id="chkFila" />';
} else {
return '';
}
}
},
{ 'data': 'ano' },
{ 'data': 'cuota' },
{ 'data': 'fecVtoForm' },
{ 'data': 'dsEstadoComp' },
{ 'data': 'totalActual' },
],
columnDefs: [
{
"targets": [0],
"visible": false,
}],
"language": { "sUrl": "fonts/datatable_espanol.txt" }
});
bServerSide: true;
bProcessing: true;
$('#tbComp tbody').off('click');
$('#tbComp tbody').on('click', 'tr', function () {
$("#frmpago").hide();
let sel = dataTableVariable.row(this).data().seleccionable;
let ch = $(this).find("#chkFila");
if (sel == 0) {
alertify.error("Comprobante no seleccionable");
} else {
let imp_comp = dataTableVariable.row(this).data().totalActual;
if ($(this).hasClass('selected')) {
$(this).removeClass('selected');
monto -= parseFloat(imp_comp);
$("#monto").html("Monto a pagar $ " + monto.toFixed(2));
cant -= 1;
$("#cant").html("Comprobantes Seleccionados: " + cant);
ch.prop('checked', false);
}
else {
$(this).addClass('selected');
monto += parseFloat(imp_comp);
$("#monto").html("Monto a pagar $ " + monto.toFixed(2));
cant += 1;
$("#cant").html("Comprobantes Seleccionados: " + cant);
ch.prop('checked', true);
}
}
});
$('#chkTodo').change(function () {
monto = 0;
cant = 0;
if ($(this).is(':checked')) {
var data = dataTableVariable.rows().data();
data.each(function (value, index) {
if (value.seleccionable === 1) {
monto += value.totalActual;
cant++;
// check checkbox y addClass('selected')
}
});
}
if (cant > 0) {
$("#monto").html("Monto a pagar $ " + monto.toFixed(2));
$("#cant").html("Comprobantes Seleccionados: " + cant);
} else {
$("#monto").html("");
$("#cant").html("");
}
})
}, error: function () {
alertify.alert("Ha ocurrido un problema")
}
})
}