数据表,计算列 [英] DataTables, calculated column
问题描述
我正在尝试使用DataTable插件在表中创建一列,该列是使用前两列中的值计算得出的.
I am trying to create a column in my table using the DataTable plugin, that is calculated using values from two previous columns.
这样的事情.(价格)(数量)=总计
|| Price || QTY || Total||
==========================
|| 5 || 2 || 10 ||
|| 10 || 3 || 30 ||
|| 4 || 1 || 4 ||
我觉得它应该很简单,但是我无法使其正常工作. 这里是我尝试过的类似问题跟随.
I feel like it should be simple, but I can't get it to work. Here is a similar question I tried to follow.
var table = $('#tempPOs').DataTable( {
dom: 'frtip', //Bfrtip (removed the B to get rid of the buttons)
ajax: '/inventory/DTResources/php/table.tempPOs.php',
columns: [
{ "data": "pmkPart" },
{ "data": "PartNumber" },
{ "data": "Description" },
{ "data": "fnkManufacturer" },
{ "data": "Notes" },
{ "data": "EachPrice", render: $.fn.dataTable.render.number( ',', '.', 0, '$' ) },
{ "data": "Quantity" },
{ "data": "Username" },
{
data: null,
className: "center",
defaultContent: '<a href="" class="editor_remove">Remove</a>'
}
],
select: true,
lengthChange: false,
buttons: [
{ extend: 'create', editor: editor },
{ extend: 'edit', editor: editor },
{ extend: 'remove', editor: editor }
],
} );
这是我的HTML
<th>Part ID</th>
<th>Part Number</th>
<th>Description</th>
<th>Manufacturer</th>
<th>Notes</th>
<th>Part Price</th>
<th>Quantity</th>
<th>Username</th>
<th>Total Price</th>
<th>Remove</th>
有人能指出我正确的方向吗?
Anyone able to point me in the right direction?
推荐答案
您可以使用render
选项,类似于在"EachPrice"
字段中使用它的方式.如果您想获取更多信息,请在此处中记录内置功能.下面将概述您的情况.
You can use the render
option similar to how you used it for the "EachPrice"
field. There is a built-in function documented here if you want to get more information, but I'll outline what it would look like for you below.
columns: [
/*other columns omitted for example*/
{
"data": null, //data is null since we want to access ALL data
//for the sake of our calculation below
"render": function(data,type,row) { return (data["price"] * data["quantity"])}
}
],
通过这种方式,您可以使用data
中的其他列来呈现此列的输出.
In this way you can use other columns from data
to render output for this column.
该选项基本上可以用来显示您想要的任何东西,只要它可以通过函数来计算即可.如果要获取有关render
选项用法的更多信息,请查看上面的链接.
This option can be used to display basically anything you want, as long as it can be calculated with a function. If you want to get more information on the usage of the render
option, take a look at the link above.
这篇关于数据表,计算列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!