Zayn Zayn - 4 months ago 21
jQuery Question

How to multiply Data in Datatables?

How do I multiply Data in Datatables ?
I have Datatables and javascript that look like this:

$('#xxdata').DataTable( {
"destroy": true,
"processing": true,
"ajax": {
url : "xxreport.php",
type : 'GET',
data : {
datedari : SplitRange[0].trim(),
datesampai : SplitRange[1].trim()
}
},
"columns": [
{ "data": "offerName" },
{ "data": "offerCountry" },
{ "data": "visits" },
{ "data": "conversions" },
{ "data": "profit"}
]
} );



I want to multiply data in
{ "data": "profit"}

maybe like
this
{ "data": "profit" * 0.7}



Can I change data in datatables as I want? Or can anyone give other solutions?

Thank You.

Answer

You can use the columns.render option (documented here) to do this.

"columns": [
            { "data": "offerName" },
            { "data": "offerCountry" },
            { "data": "visits" },
            { "data": "conversions" },
            { "data": "profit",
              "render": function (data) {
                            return data * 0.7;
                        }
            }
    ]

In this case, data in the function signature represents the data for the cell. There are other options that can be passed into the function, but in your case these do not need to be included since this is such a simple operation. See the documentation link if you ever want to expand to a more complicated rendering function