代码之家  ›  专栏  ›  技术社区  ›  Philip Southwell

我怎样才能让这个变种人工作呢?

  •  0
  • Philip Southwell  · 技术社区  · 7 年前

    我用的是制表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函数的行数据只包含有问题的单元格,不包含行中其他单元格的任何数据-因此编辑后计算失败。

    1 回复  |  直到 7 年前
        1
  •  3
  •   Oli Folkerd    7 年前

    你需要使用 突变编辑参数 选项,如果您只希望在编辑时调用mutator。

    只有当某个单元格的值被更改时,才会对该单元格调用mutator,更新该行数据中其他字段的值不会触发再次调用mutator,因为这可能会导致不可预知的行为 (假设一个mutator将传入值乘以1000作为一个单位更改,如果在任何行数据更新时调用它,它将很快损坏数据)

    如果您希望做的只是向用户显示值,而不需要实际存储数据,那么我建议您使用格式化程序来执行此操作,这样您就可以调用 row.reformat()

    推荐文章