我正在使用 Tabulator 3.5 并有一个 mutator:
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_percentage”,因为它适合我当前的数据,但我以后可以轻松概括它。
percentMutator 函数中的 console.log(mutatorParams) 在初始构建表时和编辑后是相同的。但是,console.log(data) 显示编辑后传递给 percentMutator 函数的行数据仅包含有问题的单元格,不包含该行中其他单元格的任何数据 - 因此编辑后计算失败。
我希望您对如何在编辑时强制将所有行数据发送到 mutator 有所了解。非常感谢你读到这里。任何帮助,将不胜感激。