数据表、计算列

2024-05-08

我正在尝试使用 DataTable 插件在表中创建一列,该列是使用前两列的值计算的。

像这样的东西..(价格)(数量)=总计

|| Price || QTY || Total||
==========================
||  5    ||  2  ||  10  ||
||  10   ||  3  ||  30  ||
||  4    ||  1  ||  4   ||

我觉得它应该很简单,但我无法让它发挥作用。这是我试图遵循的类似问题。 https://datatables.net/forums/discussion/25675/how-to-display-computed-column-dynamically-using-jquery-datatables

这是我正在初始化表的 JS 文件

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>

有人能指出我正确的方向吗?


您可以使用render选项类似于您使用它的方式"EachPrice"场地。有一个内置函数记录在案here https://datatables.net/reference/option/columns.render如果您想获得更多信息,但我将在下面概述它会是什么样子。

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呈现此列的输出。

这个选项基本上可以用来显示任何你想要的东西,只要它可以用函数计算。如果您想获得更多有关使用的信息render选项,请查看上面的链接。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

数据表、计算列 的相关文章

随机推荐