简体   繁体   中英

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.

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

You must add a render to your column, something like this:

{ "data": "profit", "render": renderMyProfit}

and you should have the rendering function declared before you call the .DataTable() function.

 var renderMyProfit = function (data, type, row, meta) {
            var renderContent = "<div>*</div>";
            return renderContent.replace("*", row.profit * 0.7);
  };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM