javascript - Tabulator 4 我可以扩展格式化程序而不是创建自定义格式化程序吗?

标签 javascript tabulator

制表符有几个内置单元格格式化程序,如文档中所述 - http://tabulator.info/docs/4.0/format#format

我们知道我们可以构建一个像这样的自定义格式化程序:

var myCustomFormatter=function(cell, formatterParams, onRendered){
return "Mr" + cell.getValue();
}

然后在 columns 参数中像这样使用它:

{title:"Name", field:"name", formatter:myCustomFormatter}

虽然制表符格式化程序被“用作”字符串:

{title:"Name", field:"name", formatter:"textarea"}

我们的目标是通过扩展现有格式化程序,将“myCustomFormatter”添加到参数列的可用选项列表中。

为什么?我们正在构建动态的东西,columns 参数将填充一个从 JSON 字符串接收其值的变量。像这样的事情:

var jc='[{"title":"Name", "field":"name", "formatter":"myCustomFormatter"}]';
var c=JSON.parse(c);

var table = new Tabulator("#tbdiv", {columns: c});

但是此代码不起作用,因为“myCustomFormatter”(作为字符串)在格式化程序参数中不可用。

结论,制表符格式化程序可以使用新的单元格格式化程序进行扩展(而不更改原始代码)吗?

如果没有,我们有什么想法可以填写列参数,以便它使用我们的自定义格式化程序?

谢谢

最佳答案

您可以使用 Tabulator 原型(prototype)上的 extendModule 函数来扩展模块:

//add uppercase formatter to format module
Tabulator.prototype.extendModule("format", "formatters", {
    uppercase:function(cell, formatterParams){
        return cell.getValue().toUpperCase(); //make the contents of the cell uppercase
    }
});

//use formatter in column definition
{title:"name", field:"name", formatter:"uppercase"}

您需要确保在包含源文件之后、创建第一个表之前扩展 Tabulator。

这方面的完整文档可以在 Tabulator Website Documentation 上找到。

关于javascript - Tabulator 4 我可以扩展格式化程序而不是创建自定义格式化程序吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52762577/

相关文章:

javascript - 使用按钮和鼠标滚动 DIV

javascript - 如何找出数组中包含的项目的位置?

javascript - FitText.js 的非 jQuery 替代品?

javascript - 根据其他数组的内容创建一个对象数组 "complete"

tabulator - 如何在制表符中显示选择编辑器文本但不显示值

javascript - 使用制表器对多个表使用相同的数据

javascript - 如何将动态 URL 添加到制表单元格?

javascript - 是否可以在不自动折叠/伸展树(Splay Tree)的情况下替换具有数据树的表中的数据?

javascript - 使用 JS,如何换出特定的文本实例而不是所有实例?