change the value of the one column in one row

change the value of the one column in one row

guillermovilguillermovil Posts: 15Questions: 3Answers: 0
edited April 23 in Free community support

Hi, I need to update the value of a cell in a row.
I go through the table in the following way:

            var existe = false;
            detalles.rows().every(function (value, index) {
                var data = this.data();
                if(data.prod_code == $('#prod_code').val()){
                    existe = true;

                }
            }); 

instead of
exists = true
I want to change the value of the prod_quantity column.
How can I do?

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 4,630Questions: 0Answers: 814
    edited April 24

    Hi @guillermovil ,

    You can set the data for that row with row().data() (or cell().data() for individual cells). Note you have to do the draw() afterwards, but you'll see that in the examples.

    Cheers,

    Colin

  • guillermovilguillermovil Posts: 15Questions: 3Answers: 0

    Colin, could you please give me an example?
    My table is defined as follows:

            //Tabla de detalles
            var detalles = $('#detalles_table').DataTable({
                "searching": false,
                "processing": true,
                "serverSide": false,
                "lengthChange": false, 
                "info": false,           
                "paging": false,
                "columns": [
                        { "data":"prod_code", "width": "5%"},
                        { "data":"prod_descrip"},
                        { "data":"prod_cantidad", "width": "10%" },
                        { "data":"prod_precio", "width": "10%"},
                        { "data": "accion", "width": "10%"}
                   ]     
    
            });
    

    I tried the following but the value is not modified

    $('#addRow').on( 'click', function () {
        //verifico que el producto todavía no esté en la grilla
        var existe = false;
        detalles.rows().every(function (value, index) {
            var data = this.data();
            if(data.prod_code == $('#prod_code').val()){
                existe = true;
                data.prod_cantidad++;   
                detalles.draw(false);               
            }
        });         
    } );
    
  • colincolin Posts: 4,630Questions: 0Answers: 814
    Answer ✓

    Hi @guillermovil ,

    You're updating the local copy, but not writing it back.

    Try changing your line 9 from

                detalles.draw(false);              
    

    to

                this.data(data).draw(false);              
    

    If no joy, please link to a test case - a test case that replicates the issue will ensure you'll get a quick and accurate response. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.

    Cheers,

    Colin

  • guillermovilguillermovil Posts: 15Questions: 3Answers: 0

    muchas gracias Colin!!
    thank you very much Colin!!

Sign In or Register to comment.