Update one column of all rows in dataTables

Update one column of all rows in dataTables

Mauro26Mauro26 Posts: 17Questions: 7Answers: 0
edited March 2016 in Free community support

Hello everyone,
I have a discount column, i add an input for the % of the discount of the value and i want to refresh the whole column called "discount" when i put some % in the input... Is there anyway to do it, only in the client side?
I just need to calculate the new price, edit the data of that column (i don't know how) and redraw.
This is posible? i need al the help you can give me, i'm stuck here :(

Regards

This question has an accepted answers - jump to answer

Answers

  • jr42.gordonjr42.gordon Posts: 305Questions: 2Answers: 49
    edited March 2016

    Try the following code

    dt.column( 'class of column needing update' ).invalidate().render()
    

    If that doesn't work, try

    dt.column( 'class of column needing update' ).cells().invalidate().render()
    

    This clears the cache attached to this column and calls its associated render function.

  • allanallan Posts: 61,822Questions: 1Answers: 10,129 Site admin

    The invalidate methods are probably the best way to go. However, it can depend upon the configuration of the table - we'd really need a link to the page to say for sure.

    Allan

  • Mauro26Mauro26 Posts: 17Questions: 7Answers: 0

    Hi guys i was investigating about the invalidate property, and searching i find some docs...
    With this code is working:

     $("#desc").change(function () {
         debugger;
    
         t.column( 8 ).data()
            .each( function ( value, index ) {
                console.log( 'Data in index: '+index+' is: '+value );
                var tr = $('#example tbody tr:eq(' + index + ') td:eq(8)').html( '4' );
    
                t.column( '8' ).cells().invalidate().render();
            } );
    

    but this is only working for the first page, i have a pagination of ten records the number the tr(11) doesn't take it so it only update the first page, How can i do for update all the rows?
    Regards :)

  • allanallan Posts: 61,822Questions: 1Answers: 10,129 Site admin
    Answer ✓

    See the second top FAQ :-)

    Allan

  • Mauro26Mauro26 Posts: 17Questions: 7Answers: 0
    edited March 2016

    I resolved with this:

    function CalcDesc(){
    var descuento = $('#desc').val();

         if(descuento != undefined || descuento != 0){
             t.data().each( function ( value, index ) {
                    debugger;
    
                   // var tr = $('#example tbody tr:eq(' + index + ') td:eq(8)').html( '4' );
                     var data = t.row( index ).data();
                     data.descuento = descuento;
                } );
    
             t.column( 'descuento' ).cells().invalidate().render();
         } 
     }
    

    and in the create of the dataTable i put this

                                               "data" : "descuento", 
                                        "width" : "6%",
                                     orderable: false,
                                    "render": function ( data, type, row ) {
                                    var descuento =  $('#desc').val();
                                    var total = row.total;
                                    if (descuento == undefined){
                                         return  '$' + total.toFixed(2);
                                   }
    
                                var result = total - (descuento * total) / 100;
    
                            return  '$' + result.toFixed(2);           
    

    and with that change always i do something in the datatable and exist a discount just add it to the table.

    thanks allan

This discussion has been closed.