How Can I Convert The Row Values(add row values) To A Particular Format?

How Can I Convert The Row Values(add row values) To A Particular Format?

vaishnavkokavaishnavkoka Posts: 132Questions: 23Answers: 1

I Have a size of file column in my datatable , each row value can be in any of the form (such as KB,MB,GB,TB) , now i would them to convert all the row values to a (kb/mb/gb/tb), I have used footercallback method but it doesnt give the correct result as expected(as i didnt convert the values to tb).
Ref : http://live.datatables.net/yozamuto/1/edit
how do i achieve the required results ?

Thanks
Koka

Replies

  • kthorngrenkthorngren Posts: 21,301Questions: 26Answers: 4,946

    You would need to take each string, 3.2GB for example, and extract the numeric portion of the string then multiply that by the appropriate multiplier of the KB, MB, etc portion of the string. The result can then be summed. It would take a little bit of Javascript programming to do.

    Or you could try a JS library such as numeral.js to do the conversion for you. I haven't tried it but looks like it would be easy to use.

    Kevin

  • vaishnavkokavaishnavkoka Posts: 132Questions: 23Answers: 1

    I have a php code for conversion
    here it is:
    function formatSize( $bytes )
    {
    $types = array( 'B', 'KB', 'MB', 'GB', 'TB' );
    for( $i = 0; $bytes >= 1024 && $i < ( count( $types ) -1 ); $bytes /= 1024, $i++ );
    return( round( $bytes, 2 ) . " " . $types[$i] );
    }
    what could be the other way ?

  • kthorngrenkthorngren Posts: 21,301Questions: 26Answers: 4,946

    I added the numeral.js library to your example:
    http://live.datatables.net/gepudopi/1/edit

    It seems to work although I didn't validate the data and totals. Its a pretty simple solution unless you want to write your own Javascript.

    Kevin

  • vaishnavkokavaishnavkoka Posts: 132Questions: 23Answers: 1

    We are close, i think there is some calculation error in your code, just look at the below picture.

    Image link :http://prntscr.com/k3d83o
    actual answer should be : 0.9970474243164063
    moreover the value of tb should be 2-4 decimal places.

  • kthorngrenkthorngren Posts: 21,301Questions: 26Answers: 4,946

    Its not my code. Please refer to the documentation for numeral.js for formatting options.

    I'm not sure what the image is referring to but when I manually add the numbers in your example I get 1.5 TB. It is possible my manual calculations are wrong.

    In my example you can look at the browser's console to see the original string and converted number. They look correct to me. Like I said before I've not used the numeral.js library so it my be incorrect. If so then I suggest contacting them to fix or add enhancements.

    Kevin

  • vaishnavkokavaishnavkoka Posts: 132Questions: 23Answers: 1

    Yeah but [numeral.js](http:// numeral.js "numeral.js") i am not clear with that information. The image link i shared with you (result) gives a wrong output in the total though i used your code i got wrong output. so where did i need to make changes ?

  • kthorngrenkthorngren Posts: 21,301Questions: 26Answers: 4,946

    where did i need to make changes ?

    Without seeing your code its hard to say. If you feel the numeral.js script is not working then you will need to use their support resources to debug. However you can post a link to your non-working example and we can take a look.

    Kevin

  • vaishnavkokavaishnavkoka Posts: 132Questions: 23Answers: 1

    here is my code:
    `$(document).ready(function() {
    $('#example').DataTable( {
    "footerCallback": function ( row, data, start, end, display ) {
    var api = this.api();

            // Remove the formatting to get integer data for summation
            var intVal = function ( i ) {
              console.log(i, numeral(i).value());
                return typeof i === 'string' ?
                        numeral(i).value() : i;
            };
    
            // Total over all pages
            total = api
                .column( 2 )
                .data()
                .reduce( function (a, b) {
                    return intVal(a) + intVal(b);
                }, 0 );
    
            // Total over this page
            pageTotal = api
                .column( 2, { page: 'current'} )
                .data()
                .reduce( function (a, b) {
                    return intVal(a) + intVal(b);
                }, 0 );
    
            total = numeral(total).format('0.0a');
            pageTotal = numeral(pageTotal).format('0.0a');
            // Update footer
            $( api.column( 2 ).footer() ).html(
                '--'+pageTotal +' ( --'+ total +' total)'
            );
        }
    } );
    

    } );`
    here is my database data(imgage) :

    output image:

    Actual output should be : 0.273TB
    ex: 14*20=280GB
    1TB =1024GB
    280GB=0.273TB(approx 0.28TB) but here it shows different output.
    where i am going wrong ?

  • kthorngrenkthorngren Posts: 21,301Questions: 26Answers: 4,946
    edited July 2018

    According to the numeral.js docs you will need to change '0.0a' to `'0.0b' to display bytes, like GB or TB.

    If you want to show 280GB as 0.28TB then you may need to write your own function for that. I'm not sure that numeral.js will do this for you.

    The basic code you have is working for summing. How you handle the data to get the numbers you want is outside the scope of Datatables. Stackoverflow is probably your best option to learn how to write the Javascript code to do the conversions. That's what I would do to provide you a working example.

    Kevin

  • vaishnavkokavaishnavkoka Posts: 132Questions: 23Answers: 1

    Hi @kthorgren,
    As of now, things seems to be working fine. Even the output is correct.
    Thanks
    Koka

This discussion has been closed.