Multiply in columns

Multiply in columns

fedefazzfedefazz Posts: 19Questions: 6Answers: 1
edited July 2015 in Editor

Hi im having one issue. I hace a BD table that contains quantity, price, cost and utility. Quantity, price and cost are filled by the user. Utility is filled with: (quantity * price) - cost. I have this working with my older php system, now im migrating all to DataTables, and i cant make this work. I d like that when the user makes a new record that the utility fills automaticaly and that utility was readonly. I post my code.

var editor; // use a global for the submit and return data rendering in the examples
 $(document).ready(function() {
    editor = new $.fn.dataTable.Editor( {
        "ajax": "examples/php/staff.php",
        "table": "#example",
        "fields": [ {
                "label": "Empresa:",
                "name": "empresa"
            }, {
                "label": "Nombre:",
                "name": "nombre",
            }, {
                "label": "Cantidad:",
                "name": "cantidad",
            }, {
                "label": "Precio:",
                "name": "precio"
            },{
                "label": "Costo:",
                "name": "costo"
            },{
                "label": "Utilidad:",
                "name": "utilidad",
                type:  "readonly",
                
                
            },{
                "label": "Vendedor:",
                "name": "vendedor"
            }, {
                "label": "Pago:",
                "name": "estado_pago"
            }, {
                 type:  "textarea",
                 label: "Comentarios:",
                 name:  "notas"
            }
        ],
    i18n: {
            create: {
                button: "Nuevo",
                title:  "Nuevo Registro",
                submit: "Crear"
            },
            edit: {
                button: "Editar",
                title:  "Editar Registro",
                submit: "Actualizar"
            },
            remove: {
                button: "Eliminar",
                title:  "Eliminar",
                submit: "Eliminar",
                confirm: {
                    _: "Esta seguro que desea eliminar %d registro?",
                    1: "Esta seguro que desea eliminar 1 registros?"
                }
            },
            error: {
                system: "Un error a ocurrido contactese con el administrador"
            }
        }
    } );
    
    // Activate the bubble editor on click of a table cell
    $('#example').on( 'click', 'tbody td:not(:first-child)', function (e) {
        editor.bubble( this );
    } );
 
    $('#example').DataTable( {
        "columnDefs": [
    { "searchable": false, "targets": [4,0]}
  ],
        "footerCallback": function( thead, data, start, end, display ) {
    $(thead).find('th').eq(0).css({"background-color":"#C8ECA9"})
  },
         "footerCallback": function( tfoot, data, start, end, display ) {
    $(tfoot).find('th').eq(0).html("");
    $(tfoot).find('th').eq(1).html("");
    $(tfoot).find('th').eq(2).html("");
    $(tfoot).find('th').eq(3).html("");
    $(tfoot).find('th').eq(4).html("");
    $(tfoot).find('th').eq(5).html("");
    $(tfoot).find('th').eq(6).html("");
  },
        
         responsive: true,
        
        "pageLength": 25,
        
        
        
        
        "processing": true,
        "ajax": "scripts/ids-objects.php",
        
        dom: "Tfrtip",
        ajax: {
            url: "examples/php/staff.php",
            type: "POST"
        },
        serverSide: true,
        columns: [
            { data: null, defaultContent: '', orderable: false },
           { data: "empresa" },
            { data: "nombre" },
            { data: "cantidad" },
            { data: "precio" },
            { data: "costo" },
            { data: null, type:"readonly"},
            { data: "vendedor" },
            { data: "estado_pago"},
            { data: "notas" },
        ],order: [ 1, 'asc' ],
        tableTools: {
            sRowSelect: "os",
            sRowSelector: 'td:first-child',
            sSwfPath: "TableTools/swf/copy_csv_xls_pdf.swf",
            aButtons: [
                { sExtends: "editor_create", editor: editor },
                { sExtends: "editor_edit",   editor: editor },
                { sExtends: "editor_remove", editor: editor },
                {
                    sExtends: "collection",
                    sButtonText: "Exportar",
                    sButtonClass: "save-collection",
                    aButtons: [ 'copy', 'csv', 'xls', 'pdf' ]
                },
                {
                    sExtends: "print",
                    sButtonText: "Imprimir",
                    sInfo: "Presiones la tecla 'Escape' para salir"
                    
                    },
            ]
        },
        language: {
            processing:     "Procesando...",
            search:         "Buscar:",
            lengthMenu:     "Afficher _MENU_ éléments",
            info:           "Mostrando  _START_  de  _END_ - <b>Total:</b> _TOTAL_ registros",
            infoEmpty:      "Affichage de l'&eacute;lement 0 &agrave; 0 sur 0 &eacute;l&eacute;ments",
            infoFiltered:   "(Filtrando _MAX_ &eacute;l&eacute;ments au total)",
            infoPostFix:    "",
            loadingRecords: "Cargando...",
            zeroRecords:    "No se han encontrado registros",
            emptyTable:     "La tabla esta vacia",
            paginate: {
                first:      "Primera",
                previous:   "Anterior",
                next:       "Siguiente",
                last:       "Ultima"
            },
            aria: {
                sortAscending:  ": Ordenar Ascendente",
                sortDescending: ": Ordenar Descendente"
            }
        }
    } );
} );

Thaks to all

Answers

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin

    So the utilidad field's value is dependent on other fields? You could use a keyup event on the other fields and perform the calculation you need:

    function multiply () {
      editor.field( 'utilidad' ).val( 
        editor.field( 'precio' ).val() * editor.field( 'costo' ).val()
      );
    }
    
    editor.field( 'precio' ).input().on( 'keyup', multiply );
    editor.field( 'costo' ).input().on( 'keyup', multiply );
    

    Allan

  • fedefazzfedefazz Posts: 19Questions: 6Answers: 1

    Thanks Allan, can you tell me where i should put this code? Thanks

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin

    Line 64 in your above code I would suggest. Anywhere after the editor has been initialised basically.

    Allan

  • fedefazzfedefazz Posts: 19Questions: 6Answers: 1

    Thanks bro, works great!

This discussion has been closed.