我用的是制表3.5,还有一个变种:
Tabulator.extendExtension("mutator", "mutators", {
percentageMutator:function(value, data, type, mutatorParams){
console.log(mutatorParams); // sanity check - we'll come back to this!
console.log(data); // sanity check - we'll come back to this!
// code omitted here to perform calculations
return percentage
}
});
我的构造函数对象去掉了所有细节,如下所示。请注意,这些列嵌套了两次。还要注意,我正在使用Django模板语言动态构建表。
$("#markbook-table").tabulator({
columns: [
{title: "First Name", field: "firstname", validator: ["maxLength:50", "string"]},
{title: "Last Name", field: "lastname", validator: ["maxLength:50", "string"]},
{ title:"Some grouping",
columns:[
{% for assessment in assessments %}
{
title: "{{ assessment.name }} (out of {{ assessment.max_mark }})",
columns:[
{title: "Result", field: "{{ assessment.name }}", editor: "input",},
{title: "Mark out of", field: "{{ assessment.name }}_outof"},
{title: "weighting", field: "{{ assessment.name }}_weighting"},
{title: "%", field: "{{ assessment.name }}_percentage", mutator: "percentageMutator", mutatorParams:{assessmentName: "{{ assessment.name }}"}},
},
]
},
{% endfor %}
],
},
{% endfor %}
],
data: tableData,
cellEdited: function(cell) {
row.update({'Statistics_percentage': false}); // this is the line that fails
$.ajax({
// Do ajax magic here to save to database
});
},
})
问题的关键是我的row.update()不会在结果字段发生更改后重新计算百分比。之所以选择字段名“Statistics\u percentage”,是因为它符合我当前的数据,但我可以在以后很容易地对其进行归纳。
percentageMutator函数中的console.log(mutatorParams)在表的初始构造和编辑之后是相同的。但是,console.log(data)显示编辑后传递给percentageMutator函数的行数据只包含有问题的单元格,不包含行中其他单元格的任何数据-因此编辑后计算失败。