Search code examples
datatablesjeditable

Jquery Inline editor plugin saving numbers on dropdown select


Good day,

I have a datatable with the jQuery plugin that makes it editable and transform to text all the table rows. Need to create a dropdown menu to select and store on database what is selected. Everything is fine, since the plugin have an option to create a row with select options, the problem here is that the field that i have on the database is based on VARCHAR and the plugin only stores data with ENUM on the selected dropdown field. So i cannot change the database field to ENUM because its going to delete or alter the already exixting data and could be a problem.

Example of what i see when i click on EDIT and see the dropdown options: This is what i have on my current dropdown select

When i hit on SAVE button it shows this on the database: enter image description here

This happens because the column type on the database is VARCHAR and the plugin needs it to be ENUM to read the data as text.

This is the code to draw the table and the edit plugin data:

$(document).ready(function(){

 var dataTable = $('#tabla_clientes').DataTable({
    "language": {
        "url": "https://cdn.datatables.net/plug-ins/1.10.20/i18n/Spanish.json"
    },  
  "processing" : true,
  "serverSide" : true,
  "order" : [],
  "ajax" : {
   url:"fectch.php",
   type:"POST"
  }
 });     
$('#tabla_clientes').on('draw.dt', function(){
      $('#tabla_clientes').Tabledit({
       url:'action.php',
       deleteButton: false,
       autoFocus: false,
        buttons: {
            edit: {
                class: 'btn btn-sm btn-primary',
                html: '<span class="glyphicon glyphicon-pencil"></span> &nbsp Editar',
                action: 'edit'
            }
    
    
        },
       dataType:'json',
       columns:{
        identifier : [0, 'id_customer'],
        editable:[[1, 'customerID'], [2, 'RFC'], [3, 'firstname'], [4,'lastname'],[5,'email'],[6, 'tipo_cliente','{"1":"CONTADO","2":"CREDITO"}']]
       },
       restoreButton:false,
       onSuccess:function(data, textStatus, jqXHR)
       {
      
       }
      });
     });

I already did some research on the internet but i cannot find anyone with a solution or with this problem. Like i said, i cannot simply change the field to ENUM because there is a LOT of information that may be changed or deleted because of the type change.

Does anyone know a solution to store the value as text with the VARCHAR type when selecting the dropdown info?

Thanks for the time and patience. Have a excelent day.


Solution

  • I already found a workaround very simple for this.

    Instead of adding directly the value as number on database.

    Added an IF to compare the number and store text on a variable.

    $tipo_cliente = $_POST['tipo_cliente'];
        $sitio = $_POST['sitio']
        if ($tipo_cliente == 1) {
            $tipo_cliente = 'CONTADO';
        } else
        {
            $tipo_cliente = 'CREDITO';
        }