如何为Tabulator的value列实现基于type列的动态编辑器?
解决Tabulator 4.9.x动态编辑器切换问题
要实现根据每行type列的值动态切换value列的编辑器,核心是利用Tabulator中editor支持函数返回配置的特性——在这个函数里可以直接获取当前行的完整数据,从而判断应该使用哪种编辑器。
具体实现代码
数据(无需修改)
[ {"ID":1, "value":"Text Value", "type":"text"}, {"ID":2, "value":"1", "type":"bool"}, {"ID":3, "value":"-1.23", "type":"numeric"}, {"ID":4, "value":"0", "type":"undefined"} ]
列配置(修改value列的editor字段)
[ {"title": "ID", "field": "ID" , "sorter": "number"}, { "title": "Value", "field": "value", "editor": function(cell) { const rowData = cell.getRow().getData(); switch(rowData.type) { case "bool": return "tickCross"; case "numeric": return "number"; default: return "input"; } } }, {"title": "Type", "field": "type" , "visible":false} ]
补充说明
- 版本兼容:这种写法在Tabulator 4.9.x和5.x版本中都适用,后续升级到5.x时无需大幅修改代码。
- 值类型适配:注意数据中
bool和numeric类型的value是字符串格式,tickCross和number编辑器会自动处理类型转换;如果需要更严格的控制,可以在编辑器的params里添加配置,或者通过onEditCell事件完成值的转换。
内容的提问来源于stack exchange,提问作者McLoo
相关产品推荐
相关产品推荐

