Currently I have a validation where I disable the buttons depending on the numerical value as follows:
disabled = [0,2,3,5]
/* Formatting function for row details - modify as you need */
function format(d) {
// `d` is the original data object for the row
$tipoproveedor = $("#txttipoproveedor").val();
console.log(d);
let tabla = `<table cellpadding="5" cellspacing="0" style="border-collapse: separate; border-spacing: 40px 5px;">
<thead>
<tr>
<th>
Fecha de recepción
</th>
<th>
Recepción
</th>
<th>
Estatus
</th>
</tr>
</thead>
<tbody>`;
d.Factura.forEach(f => {
tabla += `<tr>
<td>${f.FechaFactura}</td>
<td>${f.Factura}</td>
<td>${f.Estatus}</td>
<td><button type="button" class="btn btn-primary" data-toggle="modal" data-target="#ModalCargaFactura" onclick="CargarFactura('${f.OrdenCompra}' )"`;
if($tipoproveedor != '0'){
if (disabled.indexOf(f.Estatus) > -1) {
tabla += ` disabled `;
}
}
tabla += `>Cargar Documentos</button></td>
<td><button type="button" class="btn btn-primary" onclick="MostrarDetalleFactura('${f.Factura}')">Ver Detalle</button></td>
</tr>`;
});
tabla += '</tbody></table>';
return tabla;
}
Where I disable the button on the values 0,2,3,5
, now these values will change to strings giving the following assignment to the numeric values like so:
0 = 'Ninguno'
2 = 'Aceptado'
3 = 'Enviado'
5 = 'Entregado'
What I require now is to validate not with the numbers but with the character string, I hope someone can give me a little guidance with this validation.
Update 1:
Based on the response I have made the following code changing my array of values for strings as follows:
disabled = ['Ninguno','Aceptado','Enviado','Entregado']
/* Formatting function for row details - modify as you need */
function format(d) {
// `d` is the original data object for the row
$tipoproveedor = $("#txttipoproveedor").val();
console.log(d);
let tabla = `<table cellpadding="5" cellspacing="0" style="border-collapse: separate; border-spacing: 40px 5px;">
<thead>
<tr>
<th>
Fecha de recepción
</th>
<th>
Recepción
</th>
<th>
Estatus
</th>
</tr>
</thead>
<tbody>`;
d.Factura.forEach(f => {
tabla += `<tr>
<td>${f.FechaFactura}</td>
<td>${f.Factura}</td>
<td>${f.Estatus}</td>
<td><button type="button" class="btn btn-primary" data-toggle="modal" data-target="#ModalCargaFactura" onclick="CargarFactura('${f.OrdenCompra}' )"`;
if($tipoproveedor != '0'){
if (disabled.indexOf(f.Estatus) > -1) {
tabla += ` disabled `;
}
}
tabla += `>Cargar Documentos</button></td>
<td><button type="button" class="btn btn-primary" onclick="MostrarDetalleFactura('${f.Factura}')">Ver Detalle</button></td>
</tr>`;
});
tabla += '</tbody></table>';
return tabla;
}
Trying it this way I got the following error:
Invalid JSON response. For more information about this error, please see http://datatables.net/tn/1
The little I understand is that the validation no longer detects the numerical values that existed in the array disabled
and for this reason it marks the error and the data is not loaded in the table.
It will explain a little more in detail, currently I have in the table the column Estatus
where the values are shown 0,2,3,5
and the buttons are disabled or enabled depending on their value. In this case I have been forced to change these same values for strings and in order not to complicate my life too much I have decided to make this change from the query with which I show the data in the table with its simple case
like this:
CASE ESTATUS
WHEN 0 THEN 'Ninguno'
WHEN 1 THEN 'Recibido'
WHEN 2 THEN 'Aceptado'
WHEN 3 THEN 'Enviado'
WHEN 4 THEN 'Procesado'
WHEN 5 THEN 'Entregado'
ELSE 'Otro'
END as 'Estatus'
Look what I have done from your question
I have put this code in a page with a block 2 to avoid the variable not found error.
I have deduced the list variable from the structure of the code that you put in the question.
Run and you will see that the buttons are disabled except for the one that I have set as Status 'Other' (to check) and note that the validation is as you have it in your question.
Oh, and I've added to the end of
document.write()
so you can see the result.I hope it answers the question.
EDITED
Disabled
returns to be an array of numbersI have changed part of the code to not touch
disabled
Since it cannot be touched
disabled
andf.Estatus
now it is a string, I create an array with the possible states in their order (that of the CASE) in the form of a string and see what number corresponds to the current state. Now validate by checking withdisabled
.I understand that
f.Estatus
instead of a numeric code now comes a string that can be one of the following: 'None', 'Received', 'Accepted', 'Sent', 'Processed', 'Delivered' or 'Other'. If so, you can validate f.Status as follows: