docs/docs/cn/runjs/context/get-value.md
在 JSField、JSItem 等可编辑字段场景中,获取当前字段的最新值。与 ctx.setValue(v) 配合可实现与表单的双向绑定。
| 场景 | 说明 |
|---|---|
| JSField | 可编辑自定义字段中读取用户输入或表单当前值 |
| JSItem | 表格/子表格的可编辑项中读取当前单元格值 |
| JSColumn | 表格列渲染时读取对应行的字段值 |
注意:
ctx.getValue()仅在带表单绑定的 RunJS 上下文中可用;事件流、联动规则等无字段绑定场景中不存在此方法。
getValue<T = any>(): T | undefined;
undefined。ctx.getValue() 按以下顺序取值:
表单尚未渲染完成或字段未注册时,可能返回
undefined。
const current = ctx.getValue();
if (current == null || current === '') {
ctx.render(<span>请先输入内容</span>);
} else {
ctx.render(<span>当前值:{current}</span>);
}
const { Input } = ctx.libs.antd;
// 读取当前值作为默认值
const defaultValue = ctx.getValue() ?? '';
ctx.render(
<Input
defaultValue={defaultValue}
onChange={(e) => ctx.setValue(e.target.value)}
/>
);
getValue 配合实现双向绑定js-field:value-change - 外部值变更时触发的容器事件,用于更新显示