简体   繁体   English

如何在Camunda中处理可变数量的过程变量

[英]How to handle variable number of process variables in Camunda

I'm new to Camunda and didn't find any tutorial or reference explaining how to achieve the following: 我是Camunda的新手,没有找到任何教程或参考资料来解释如何实现以下目标:

When starting a process I want the user to add as many items as he likes to an invoice. 当开始一个过程时,我希望用户在发票中添加任意数量的项目。 On the next user task all those items and their quantity should be printed to somebody approving the data. 在下一个用户任务上,所有这些项目及其数量应打印给批准数据的人。

I don't understand yet how to get this 1:n relationship between a process and its variables working. 我还不了解如何在流程及其变量之间建立这种1:n关系。 Do I need to start subprocesses for each item? 我需要为每个项目启动子流程吗? Or do I have to use a custom Java object? 还是我必须使用自定义Java对象? If so, how can I map form elements to such an object from within the Tasklist? 如果是这样,我如何从任务列表中将表单元素映射到这样的对象?

I got it working with the help of the links provided by Thorben. 我在Thorben提供的链接的帮助下工作了。

The trick is to use JSON process variables to store more complex data structures. 诀窍是使用JSON过程变量来存储更复杂的数据结构。 I initialize such lists in my "Start Event". 我在“开始事件”中初始化了此类列表。 This can be done either in a form or in my case in a Listener: 这可以以某种形式或在我的情况下在侦听器中完成:

execution.setVariable("items", Variables.objectValue(Arrays.asList(dummyItem)).serializationDataFormat("application/json").create());

Note that I added a dummyItem, as an empty list would lose its type information during serialization. 请注意,我添加了一个dummyItem,因为空列表将在序列化期间丢失其类型信息。

Next in my custom form I load this list and can add/remove items. 接下来,在我的自定义表单中,我加载此列表,并可以添加/删除项目。 Using the camForm callbacks one can persist the list. 使用camForm回调可以保留列表。

<form role="form" name="form">
    <script cam-script type="text/form-script">
    /*<![CDATA[*/
    $scope.items = [];

    $scope.addItem = function() {
        $scope.items.push({name: '', count: 0, price: 0.0});
    };

    $scope.removeItem = function(index) {
        $scope.items.splice(index, 1);
    };

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

    // variables-fetched is not working with "saved" forms, so we need to use variables-restored, which gets called after variables-fetched
    camForm.on('variables-restored', function() {
        $scope.items = camForm.variableManager.variableValue('items');
    });

    camForm.on('store', function() {
        camForm.variableManager.variableValue('items', $scope.items);
    });
    /*]]>*/
    </script>


    <table class="table">
        <thead>
            <tr><th>Name</th><th>Count</th><th>Price</th><th></th></tr>
        </thead>
        <tbody>
            <tr ng-repeat="i in items">
                <td><input type="text" ng-model="i.name"/></td>
                <td><input type="number" ng-model="i.count"/></td>
                <td><input type="number" ng-model="i.price"/></td>
                <td>
                    <button class="btn btn-default" ng-click="removeItem($index)">
                        <span class="glyphicon glyphicon-minus"></span>
                    </button>
                </td>
            </tr>
        </tbody>
        <tfoot>
            <tr>
                <td colspan="4">
                    <button class="btn btn-default" ng-click="addItem()">
                        <span class="glyphicon glyphicon-plus"></span>
                    </button>
                </td>
            </tr>
        </tfoot>
    </table>

</form>

Two things aren't working yet: 两件事还没有解决:

  • Field validation, eg for number fields 字段验证,例如用于数字字段
  • The dirty flag used on the "save" button doesn't get updated, when adding/removing rows 添加/删除行时,“保存”按钮上使用的脏标志不会更新

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM