How to render negative value as positive / absolute?

How to render negative value as positive / absolute?

juniorDev_seijuniorDev_sei Posts: 3Questions: 1Answers: 0

Here is the code for table

$('#table').DataTable({
            pageLength: 25,
            searching: false,
            ajax: {
                url: 'url/to/json',
                dataSrc: 'data',
            },
            columns: [
                {data:'customer'},
                {data:'part_id_customer'},
                {data:'part_id_internal'},
                {data:'part_name'},
                {data:'lot_no'},
                {data:'quantity'},
                {data:'date_time'},
                {data:'pic'},
            ],
            order:[[6, 'asc']],
            columnDefs: [{'orderable':false, 'targets':'_all'},
                        {'render':$.fn.dataTable.render.number('.',',',0,''), 'targets':5, 'className':'dt-right'},
                        {'render':$.fn.dataTable.render.moment('','DD MMMM YYYY, hh:mm:ss','id'), 'targets':6, 'className':'dt-center'}],
        })

On this table, the data is generated by json and all quantity column values are negative (I need to record it as negative on database for other computing reason). But if I display the value as negative on the table, I'm afraid it will confuse the user. Is there a way to render the as positive? Thanks for all the help.

This question has an accepted answers - jump to answer

Answers

  • juniorDev_seijuniorDev_sei Posts: 3Questions: 1Answers: 0

    I think I found a way to deal with the problem.

    {data:'quantity', fnCreatedCell:function(nTd, sData){
                        var abs_sData = Math.abs(sData);
                        $(nTd).html(abs_sData);
                    }},
    

    Let me know if there is a more efficient way to deal with this kind of problem.
    Thank you.

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓
    data: function (row) {
      return Math.abs( row.quantity );
    ),
    render: $.fn.dataTable.render.number('.',',',0,'')
    

    Is possibly the neatest and fastest way of doing it.

    Allan

  • juniorDev_seijuniorDev_sei Posts: 3Questions: 1Answers: 0

    Thank you for your help, Allan.

    The problem with my method before is it didn't show thousand separator, and if I try to convert it to locale string, it won't use dot as thousand separator (probably because I choose en-US as browser language).
    Now it shows exactly as I wanted.

Sign In or Register to comment.