简体   繁体   English

如何在数据表中乘以数据?

[英]How to multiply Data in Datatables?

How do I multiply Data in Datatables ? 如何将数据乘以数据表? I have Datatables and javascript that look like this: 我有看起来像这样的数据表和javascript:

$('#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"} 我想乘以{ "data": "profit"}
maybe like this { "data": "profit" * 0.7} 可能是这样的{ "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.render选项( 在此处记录 )进行此操作。

"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. 在这种情况下, data在函数签名表示为单元的数据。 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. 并且应该在调用.DataTable()函数之前声明渲染函数。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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