扩展Tabulator日期时间格式化器时无法访问dependencyRegistry问题
解决Tabulator自定义格式化器调用datetime时的上下文问题
问题出在直接调用原生datetime格式化器时,函数内部的this没有绑定到正确的Tabulator格式模块实例,导致this.table为undefined,无法访问luxon依赖。
修改你的自定义格式化器代码,使用call()方法为datetime格式化器绑定当前上下文:
Tabulator.extendModule("format", "formatters", { cellByRequiredFormatter: function cellByRequiredFormatter(cell, formatterParams, onRendered) { const requiredField = formatterParams.requiredField; onRendered(() => { const data = cell.getData(); const required = data[requiredField]; if (!required) cell.getElement().classList.add("disabled"); }); // 使用call绑定当前上下文,让datetime格式化器能访问this.table return Tabulator.moduleBindings.format.formatters.datetime.call(this, cell, formatterParams, onRendered); } });
原理:Tabulator的原生格式化器函数依赖this指向格式模块实例来获取表格对象(this.table),通过call(this, ...)把当前自定义格式化器的上下文传递给datetime格式化器,就能让它正常访问this.table.dependencyRegistry去查找luxon依赖了。
内容的提问来源于stack exchange,提问作者Busti
相关产品推荐
相关产品推荐

