如何在Tabulator中设置特定行的指定单元格为可编辑状态
Tabulator表格指定单元格设置可编辑方案
需求背景
已完成Tabulator表格初始化配置,成功实现Nom列整列可编辑。现需将以下两个特定单元格设置为可编辑状态:
- Efficiency[-]行的Min列单元格(值为0.91)
- Servo Stiffness(axial)行的Min列单元格(值为0.00,该行为表格第五行)
实现方案
Tabulator允许通过函数形式配置editor属性,动态判断单元格是否可编辑。只需修改Min列的editor配置逻辑,根据当前单元格所在行的名称和索引匹配目标单元格,返回对应的编辑器类型即可。
修改后的完整代码
initializeTabulatortableBng() { let classThis = this; let bngTableData = classThis.tableDataWorm; function decimalFormatter(cell) { var value = cell.getValue(); if (value !== null && value !== undefined && !isNaN(value)) { return parseFloat(value).toFixed(2); } return value; } this.bngTabulatorHeaders = [{ title: "Type", field: "name", headerHozAlign: "left", width: 600, headerSort: false, hozAlign: "left", editable: false }, { title: "Value", headerHozAlign: "center", width: 900, columns: [{ title: "Min", field: "minValue", // 核心修改:通过函数动态判断是否启用编辑 editor: function(cell) { const rowData = cell.getRow().getData(); const rowIndex = cell.getRow().getIndex(); // 匹配目标行:Efficiency[-] 或 第五行的Servo Stiffness(axial) if (rowData.name === "Efficiency[-]" || (rowData.name === "Servo Stiffness(axial)" && rowIndex === 4)) { return "input"; } return false; }, headerSort: false, width: 300, hozAlign: "right", formatter: decimalFormatter, headerHozAlign: "right", editorParams: { formatter: decimalFormatter }, }, { title: "Nom", field: "nomValue", editor: "input", headerSort: false, width: 300, hozAlign: "right", formatter: decimalFormatter, headerHozAlign: "right", editorParams: { formatter: decimalFormatter } }, { title: "Max", field: "maxValue", editor: false, headerSort: false, width: 300, hozAlign: "right", formatter: decimalFormatter, headerHozAlign: "right", editorParams: { formatter: decimalFormatter } }, ], }, ]; this.bngTabulatorTable = new Tabulator("#bngTabulatorTable", { maxHeight: "100%", maxwidth: "100%", data: bngTableData, layout: "fitColumns", headerSort: false, columns: this.bngTabulatorHeaders, }) }
关键逻辑说明
- 利用
cell.getRow().getData()获取当前行数据,通过name字段匹配目标行 - 通过
cell.getRow().getIndex()获取行索引(从0开始计数,第五行对应索引4),精准定位Servo Stiffness(axial)行的单元格 - 符合条件的单元格返回
"input"启用编辑,其余单元格保持不可编辑状态
内容的提问来源于stack exchange,提问作者Sharana Avali
相关产品推荐
相关产品推荐

