rendering a number signage, and decimals base upon another column in my table

rendering a number signage, and decimals base upon another column in my table

cpshartcpshart Posts: 246Questions: 49Answers: 5

Hi

I am trying to render a number using the function below

this works perfectly below for a column as follows:-

            { data: "dm_transactions.charges",
             render: $.fn.dataTable.render.number( '\,', '.', 2, '£', '' )
},

I wish however to render the number based upon the value of another column this works below, but I should like to use the render function to set decimal places, sign and thousands separator

            { data: null, // total
                        render: function(data, type, row) {
                            if (row.dm_transactions.transaction_type === 'Buy') {
                            return parseFloat(row.dm_transactions.total).toFixed(2); }  
                            else {
                            return 0 - parseFloat(row.dm_transactions.total).toFixed(2); } 
                            },
            }

I have tried to replace the render function in the above code, but I have been unable to get it working, any help would be much appreciated.

I am t

            { data: "dm_transactions.total",
             render: function(data, type, row) {
             switch (row.dm_transactions.transaction_type) {
    case 'Buy':
        return render: $.fn.dataTable.render.number( '\,', '.', 2, '£', '' );
        break;
    case 'Sell':
        return 0 - render: $.fn.dataTable.render.number( '\,', '.', 2, '£', '' );
        break;
    }   
    }                           

I have tried the above but it failed

Console error is 
Uncaught SyntaxError: Unexpected token :
on line 5 above 

Many Thanks

Colin

This question has accepted answers - jump to:

Answers

  • SchautDollarSchautDollar Posts: 21Questions: 6Answers: 1
    edited April 2019 Answer ✓

    Hello,

    When using render you must provide the result value when using a function as that is the value that gets passed to the table for display, so unfortunately, we cannot pass one of the premade renderers ($.fn.dataTable.render.number) as a result.

    Since you are looking for customizability, I would recommend the following:

    Format your data.
    Return data.

    render: function(data, type, row) {
        var returnValue = "";
        switch (row.dm_transactions.transaction_type) {
            case 'Buy':
                returnValue = data; 
                break;
            case 'Sell':
                returnValue = 0 - data; 
                break;
        }
        //You can format the number here. See link below for suggestion.
        returnValue = /*Format Here*/
        return returnValue;
    }  
    
    

    There is a good thread on stack overflow for formatting currency. That might be worth taking a look at.

  • cpshartcpshart Posts: 246Questions: 49Answers: 5

    Hi SchautDollar

    Thanks for your prompt reply, I have inserted your suggested code below and looked at the suggested thread.

    This works if I simply want to change the sign +/- based upon the value of row.dm_transactions.transaction_type.

    But can I use the function render: $.fn.dataTable.render.number( '\,', '.', 2, '£', '' ) commented out below on line 14 instead of line 15. this would make it very easy to change number formats, as I have a large number of reports and columns to change, otherwise I can use the suggestions in the stack overflow thread.

                { data: "dm_transactions.quantity", // signedquantity
                 render: function(data, type, row) {
                //You can format the number here. See link below for suggestion.
                var returnValue = "";
                 switch (row.dm_transactions.transaction_type) {
                    case 'Buy':
                         returnValue = data;
                    break;
                    case 'Sell':
                         returnValue = 0 - data;
                    break;
                }   
        //You can format the number here. See link below for suggestion.
    //    returnValue = render: $.fn.dataTable.render.number( '\,', '.', 2, '£', '' ); /*Format Here*/
        return returnValue;
        }                           
    
    

    Enabling line 15 and disabling line 16 results in the same error on the console.

    Many Thanks Colin

  • kthorngrenkthorngren Posts: 21,144Questions: 26Answers: 4,918
    Answer ✓

    Don't think you can use $.fn.dataTable.render.number() to render numbers from another column. You would need to create your own. Looks like this is the $.fn.dataTable.render.number() code if you want to try to replicate.

    Kevin

  • cpshartcpshart Posts: 246Questions: 49Answers: 5

    Hi Kevin

    I am trying to render the number for the column on line 1 above namely dm-transactions.quantity or the source column ? (as opposed to another column), but using a switch statement I need to change the sign aswell as render the original value based upon the value of another column (transaction_type).

    so
    Buy Quantity 1000 would become 1,000.00
    Sell Quantity 1000 would become -1,000.00

    I will however look at the code in your link to replicate with thanks.

    Many Thanks for your help

    Colin

  • kthorngrenkthorngren Posts: 21,144Questions: 26Answers: 4,918
    edited April 2019 Answer ✓

    Maybe it could just simply be something like this:
    http://live.datatables.net/lujazeja/1/edit

    You may need to adjust depending on other formatting you need. Its a matter of using the Javascript functions needed to get the format you want.

    Kevin

  • cpshartcpshart Posts: 246Questions: 49Answers: 5

    Hi Kevin

    That is exactly what I need, I can add the other formatting required for thousands separator and currency.

    Best Regards

    Colin

This discussion has been closed.