Search code examples
datatablesrendering

multiply values and apply style in Datatables


I would like to understand how to multiply column values and apply style in Datatables.

Currently my columns section looks like this :

"columns": [
                { data: "id" },
                { data: "brand" },
                { data: "description" },
                { data: "nb_ref" },
                { sortable : false, data: "cost", render: $.fn.dataTable.render.number( ' ', '.', 2, '€' )  },
            ]

what I would like in the 5th column is to multiply the "cost" by the "nb_ref" (the value of the 4th column) and still apply the number format.

Any idea about how to achieve ?

thanks

Adam


Solution

  • looking at this answer I think you can do it like this

    "columns": [
                { data: "id" },
                { data: "brand" },
                { data: "description" },
                { data: "nb_ref" },
                { sortable : false, data: "cost", render: function (data, type, row, meta) {
                    var nb_ref = row['nb_ref'];
                    var cost = row['cost'];  
                    var total = nb_ref * cost;
                    return $.fn.dataTable.render.number( ' ', '.', 2, '€' ).display(total);                        
                   } 
                },
            ]