Skip to content
Advertisement

How to change numeric validation to string validation

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>
                                Date Order
                            </th>
                            <th>
                                Order
                            </th>
                            <th>
                                Status
                            </th>
                        </tr>
                        </thead>
                        <tbody>`;
                                                        
     d.Factura.forEach(f => { tabla += 
                               `<tr>                                                         
                                <td>${f.DateInvoice}</td>
                                <td>${f.Invoice}</td>       
                                <td>${f.Status}</td>                                                                                           
                                <td><button type="button" class="btn btn-primary" data-toggle="modal" data-target="#ModalCargaFactura" onclick="LoadInvoice('${f.PurchaseOrder}' )"`;
                 
                                if($tipoproveedor != '0'){
                                    if (disabled.indexOf(f.Estatus) > -1) {
                                        tabla += ` disabled `;
                                    }
                                }    
                                tabla += `>Upload Documents</button></td>                               
                                <td><button type="button" class="btn btn-primary" onclick="ShowDetailsInvoice('${f.Invoice}')">Details</button></td>
                                </tr>`;
     });
     tabla += '</tbody></table>';
     return tabla;    
}

Where I disable the button in the values 0,2,3,5 now these values ​​will change to strings giving the following assignment to the numeric values ​​like this:

0 = 'None'
2 = 'Accept'
3 = 'Send'
5 = 'Delivered'

What I require now is to validate no longer with the numbers but with the character string, I hope someone can give me some guidance with this validation.

Update 1:

Based on the answer I have made the following code changing my array of values ​​for strings as follows:

disabled = ['None','Accept','Send','Delivered']
    
    /* 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>
                                    Date Order
                                </th>
                                <th>
                                    Order
                                </th>
                                <th>
                                    Status
                                </th>
                            </tr>
                            </thead>
                            <tbody>`;
                                                            
      d.Factura.forEach(f => {tabla += 
                                   `<tr>                                                         
                                    <td>${f.DateInvoice}</td>
                                    <td>${f.Invoice}</td>       
                                    <td>${f.Status}</td>                                                                                           
                                    <td><button type="button" class="btn btn-primary" data-toggle="modal" data-target="#ModalCargaFactura" onclick="LoadInvoice('${f.PurchaseOrder}' )"`;
                     
                                    if($tipoproveedor != '0'){
                                        if (disabled.indexOf(f.Estatus) > -1) {
                                        tabla += ` disabled `;
                                        }
                                    }    
                                    tabla += `>Upload Documents</button></td>                               
                                    <td><button type="button" class="btn btn-primary" onclick="ShowDetailsInvoice('${f.Invoice}')">Details</button></td>
                                    </tr>`;
      });
      tabla += '</tbody></table>';
      return tabla;    
    }

What 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 into the table.

It will explain a little more in detail, currently I have in the table the column Status 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 much I have decided to make this change from the query with which I show the data in the table with its simple caselike this:

CASE STATUS
                WHEN 0 THEN 'None'
                WHEN 1 THEN 'Receipment'
                WHEN 2 THEN 'Accept'
                WHEN 3 THEN 'Send'
                WHEN 4 THEN 'Process'
                WHEN 5 THEN 'Delivered'
                ELSE 'Other'
            END as 'STATUS'

Advertisement

Answer

Save the values as an object in the form:

const disabledValues = {
    0: 'None',
    2: 'Accept',
    3: 'Send',
    5: 'Delivered',
};

Later, during a comparison, cast your Estatus into the number (adding + at the beginning) and use it in the form:

if (disabledValues[+f.Estatus]) {
    tabla += ` disabled `;
}
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement