You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法为camVariable添加comment值,需将选中文档的comment项加入对象

实现给selectedDocuments添加comment字段并在表单中交互的方案

看起来你已经在尝试用Camunda表单SDK推进这个需求了,我帮你把代码补全并梳理清楚关键步骤:

1. 确保正确获取selectedDocuments变量

首先要在form-loaded事件里告诉SDK获取目标变量selectedDocuments(你代码里写的document应该是笔误):

var variableManager = camForm.variableManager;
$scope.selectedDocuments = {};

camForm.on('form-loaded', function() {
  // 告诉表单SDK加载selectedDocuments变量
  variableManager.fetchVariable('selectedDocuments');
});

2. 在表单模板中添加comment输入框

在你的HTML表单区域,添加一个绑定到selectedDocuments.comment的输入框,这样用户输入的内容会直接关联到目标对象:

<div class="form-group">
  <label for="comment">备注信息</label>
  <input type="text" 
         class="form-control" 
         id="comment" 
         ng-model="selectedDocuments.comment"
         placeholder="请输入备注">
</div>

3. 变量加载完成后初始化对象(避免空指针)

当变量加载完成后,如果selectedDocuments还没有comment属性,先初始化一个空值,防止用户输入时出现报错:

camForm.on('variables-fetched', function() {
  // 获取变量的实际值,为空时默认初始化空对象
  $scope.selectedDocuments = variableManager.variableValue('selectedDocuments') || {};
  
  // 初始化comment属性
  if (!$scope.selectedDocuments.comment) {
    $scope.selectedDocuments.comment = '';
  }
});

4. 提交表单时保存更新后的对象

最后在表单提交前,把包含comment的selectedDocuments重新设置到变量管理器中,确保值能被持久化:

camForm.on('submit', function() {
  // 将更新后的对象存回流程变量
  variableManager.createVariable({
    name: 'selectedDocuments',
    type: 'Object', // 若你的变量是JSON类型,这里改为'Json'即可
    value: $scope.selectedDocuments
  });
});

完整代码整合

把这些部分组合起来,就是可直接使用的完整实现:

var variableManager = camForm.variableManager;
$scope.selectedDocuments = {};

camForm.on('form-loaded', function() {
  variableManager.fetchVariable('selectedDocuments');
});

camForm.on('variables-fetched', function() {
  $scope.selectedDocuments = variableManager.variableValue('selectedDocuments') || {};
  if (!$scope.selectedDocuments.comment) {
    $scope.selectedDocuments.comment = '';
  }
});

camForm.on('submit', function() {
  variableManager.createVariable({
    name: 'selectedDocuments',
    type: 'Object',
    value: $scope.selectedDocuments
  });
});

这样调整后,用户在表单输入的备注会自动绑定到selectedDocuments.comment,提交时更新后的对象会被保存回流程变量,刚好满足你的需求~

内容的提问来源于stack exchange,提问作者beginer beginer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 08:17:14