[]
可以通过实现 ISupportReadOnly 支持只读。实现只读属性的单元格可以和单元格权限或设置单元格属性命令集成。
C# 示例代码如下:
public class MyPluginCellType : CellType, ISupportReadOnly
{
[DisplayName("只读")]
public bool ReadOnly { get; set; }
}
JavaScript 通过重写 setReadOnly函数实现只读效果。
示例代码如下:
class MyPluginCellType extends Forguncy.Plugin.CellTypeBase {
createContent() {
this.input = $("<input style='width:100%;height:100%'>");
this.input.change(() => {
this.commitValue();
})
return this.input;
}
setValueToElement(_, value) {
this.input.val(value?.toString());
}
getValueFromElement() {
return this.input.val();
}
setReadOnly(value) {
super.setReadOnly(value); // 这里必须调用基类方法
if (this.isReadOnly()) { // 这里使用 isReadOnly 方法,而不是 value, 因为有可能因为单元格权限的原因,isReadOnly() 永远为 true
this.input.attr("readonly", "readonly");
}
else {
this.input.removeAttr("readonly");
}
}
}
Forguncy.Plugin.CellTypeHelper.registerCellType("MyPlugin.MyPluginCellType, MyPlugin", MyPluginCellType);
重写 setReadOnly方法需要注意:
必须调用 super.setReadOnly(value) 方法。
使用 this.isReadOnly() 获取最新的只读状态,而不是通过 value 参数。