API 制表符 - 汇总列值 - 单元格值编辑时的动态总计

API Tabulator - Summing Columns Values - dynamic total on cell value edit

我正在使用 C#、asp.net、JQuery 和通过 REST API

提供的 Tabulator 开发网络应用程序

我正在努力实现 2 件事,下面我的制表符定义应该在以下位置提供行总和:

 AvailTotal field 

编辑任何列中的每个列值后:

 availableBedsM, availableBedsF, availableBedsC

请有人帮忙,我需要 AvailTotal 列中的默认总和,以便在值更改时动态反映对上述字段的任何编辑。

我的制表符:

 // tabulator start
 var tabledata = new Tabulator("#example-table", {
     persistence:true, //enable table persistence
     reactiveData: true,
     height: (window.innerHeight - 10),
     ajaxURL: "api/bed_data_",
     groupBy:"siteCOSitrep",
     columns:[

     {title:"Ward Code", 
         field:"wardCode", 
         frozen:true,
         width:200
     },
         {//create column group
             title:"Available Beds",
                      columns:[
                      {title:"Male", 
                     field:"availableBedsM", 
                     align:"center", 
                     headerVertical:true,  
                     width:50, 
                     editor:"number",
                     validator:["integer", "min:0", "required", "max:99"],
                     topCalc:"sum", topCalcParams:{precision:0,},
                     cellEdited: function(cell) {
                     cell.AvailTotal = cell.getValue() + tabledata.availableBedsF + tabledata.availableBedsC;}
            },

                 {title:"Female", 
                     field:"availableBedsF", 
                     align:"center", 
                     headerVertical:true, 
                     width:50, 
                     editor:"number",
                     validator:["integer", "min:0", "required", "max:99"],
                     topCalc:"sum", topCalcParams:{precision:0,},
                     cellEdited: function(cell) {
                     cell.AvailTotal = cell.getValue() + tabledata.availableBedsM + tabledata.availableBedsC;}
            },
                 {title:"Cubicle", 
                     field:"availableBedsC", 
                     align:"center", 
                     headerVertical:true, 
                     width:50, 
                     editor:"number",
                     validator:["integer", "min:0", "required", "max:99"],
                     topCalc:"sum", topCalcParams:{precision:0,},
                     cellEdited: function(cell) {
                     cell.AvailTotal = cell.getValue() + tabledata.availableBedsM + tabledata.availableBedsC;}
                 },
                 {title:"Avail Total",  
                     field:"AvailTotal",
                     align:"center", 
                     headerVertical:true, 
                     width:50,
                     topCalc:"sum", topCalcParams:{precision:0,},
                    },
                 ],
         },

如果您想根据其他列的值计算得到一个列,那么您需要考虑使用 Mutator

如果我们假设您有第三列,您想要保存 val1 和 [=29 之和的值=]val2 列然后定义将是这样的:

var sumMutator= function(value, data, type, params, component){
    //value - original value of the cell
    //data - the data for the row
    //type - the type of mutation occurring  (data|edit)
    //params - the mutatorParams object from the column definition
    //component - when the "type" argument is "edit", this contains the cell component for the edited cell, otherwise it is the column component for the column

    return data.val1 + data.val2;
}

{title:"Sum Column", field:"sumcol", mutator:sumMutator}

如果您正在操作数据,您应该始终使用 变元,因为它会更改数据本身,这意味着该列将正确排序。如果您使用 formatter 这纯粹是一种视觉效果,意味着您将无法对列进行排序或过滤。

如果您想在其他列更改时更新计算列,那么我建议使用 cellEdited 回调来触发该专栏的更新:

function updateCalc(cel){
    cell.getRow().update({sumcol:false});
}
{title:"Val 1", field:"val1", cellEdited:updateCalc}

你给sumcol什么值并不重要,改变它会触发重新计算值的mutator