我如何让这个突变器在 cellEdited 上工作?
How do I get this mutator to work upon cellEdited?
我正在使用 Tabulator 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 模板语言动态构建 table.
$("#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',因为它适合我当前的数据,但我以后可以轻松地概括它。
percentageMutator 函数中的 console.log(mutatorParams) 在 table 的初始构造和编辑后是相同的。但是,console.log(data) 显示编辑后传递给 percentageMutator 函数的行数据仅包含有问题的单元格,不包含行中其他单元格的任何数据 - 因此编辑后计算失败。
我希望您知道如何在编辑时强制将所有行数据发送到修改器。非常感谢您阅读到这里。任何帮助将不胜感激。
您需要使用 mutatorEdit 和 mutatorEditParams 选项,如果你只想在编辑时调用修改器。
仅当单元格的值已更改时才会调用增变器,更新该行数据中其他字段的值不会触发再次调用增变器,因为这可能导致不可预测的行为(想象一个将传入值乘以 1000 作为单位更改的增变器,如果在任何时候更新任何行数据时调用它,它会很快损坏数据).
如果您要做的只是向用户显示值,而您不需要实际存储数据,我可以建议您使用格式化程序来代替,这样您就可以调用row.reformat()
更新后的函数,它将重新运行 格式化程序并重新计算显示值。
我正在使用 Tabulator 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 模板语言动态构建 table.
$("#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',因为它适合我当前的数据,但我以后可以轻松地概括它。
percentageMutator 函数中的 console.log(mutatorParams) 在 table 的初始构造和编辑后是相同的。但是,console.log(data) 显示编辑后传递给 percentageMutator 函数的行数据仅包含有问题的单元格,不包含行中其他单元格的任何数据 - 因此编辑后计算失败。
我希望您知道如何在编辑时强制将所有行数据发送到修改器。非常感谢您阅读到这里。任何帮助将不胜感激。
您需要使用 mutatorEdit 和 mutatorEditParams 选项,如果你只想在编辑时调用修改器。
仅当单元格的值已更改时才会调用增变器,更新该行数据中其他字段的值不会触发再次调用增变器,因为这可能导致不可预测的行为(想象一个将传入值乘以 1000 作为单位更改的增变器,如果在任何时候更新任何行数据时调用它,它会很快损坏数据).
如果您要做的只是向用户显示值,而您不需要实际存储数据,我可以建议您使用格式化程序来代替,这样您就可以调用row.reformat()
更新后的函数,它将重新运行 格式化程序并重新计算显示值。