【问题标题】:How do I get this mutator to work upon cellEdited?如何让这个 mutator 在 cellEdited 上工作?
【发布时间】:2018-10-30 08:53:55
【问题描述】:

我正在使用 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 有所了解。非常感谢你读到这里。任何帮助将不胜感激。

【问题讨论】:

    标签: tabulator


    【解决方案1】:

    如果您只想在编辑时调用 mutator,则需要使用 mutatorEditmutatorEditParams 选项。

    仅当单元格值已更改时才会在单元格上调用突变器,更新该行数据中其他字段的值不会触发再次调用突变器,因为这可能导致不可预测的行为(想象一个mutator 将传入值乘以 1000 作为单位更改,如果在任何行数据更新时调用它,它会很快损坏数据)

    如果您要做的只是向用户显示值并且您不需要实际存储数据,我可以建议您使用格式化程序来代替,这样您就可以调用@987654321 @ 更新后的函数,它将重新运行格式化程序并重新计算显示值。

    【讨论】:

    • 嗨 Oli - 我希望在数据的初始设置上调用 mutator,然后在任何编辑上再次调用。它在我最初设置数据时起作用,但是当我更改相邻单元格时,然后使用 row.update({'Statistics_percentage': false});没有行数据被传递给 mutator(尽管 mutatorParams 被传递给 mutator 函数!)。
    • 格式化程序选项有效,但我希望能够按列中的值排序,因此需要一个 mutator。
    • 在这种情况下,我建议您在 cellEdited 回调中进行的同一 row.update 调用中更新该列的值并将计算抽象为一个可以在 mutator 或 cellEdited 函数中调用的函数。
    • 之后可以调用row.reformat()函数刷新该行所有单元格的内容
    • 太棒了。很有魅力!
    猜你喜欢
    • 2020-08-16
    • 2015-01-01
    • 2012-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-24
    • 2016-11-13
    • 1970-01-01
    相关资源
    最近更新 更多