Alfresco技术咨询:如何在工作流表单中显示可添加行的数据表?
嘿,这个需求我之前在项目里刚好折腾过,给你梳理两个可行的方案,一步步来就能搞定:
方案一:用Alfresco自带的Dynamic Table控件(首选)
Alfresco Share本身就提供了dynamic-table控件,专门用来处理这种需要动态添加行的表格需求,不用自己从零写代码,步骤如下:
定义行数据模型
先在数据字典里创建一个自定义的节点类型(比如叫wf:tableRow),用来存放表格每行的数据。比如你需要三列,就给这个类型加三个字段:<type name="wf:tableRow"> <parent>cm:content</parent> <properties> <property name="wf:column1"> <title>第一列(文本)</title> <type>d:text</type> </property> <property name="wf:column2"> <title>第二列(数字)</title> <type>d:int</type> </property> <property name="wf:column3"> <title>第三列(日期)</title> <type>d:date</type> </property> </properties> </type>把这个模型部署到Alfresco,记得重启服务让模型生效。
在工作流模型里关联表格字段
打开你的工作流模型文件(比如customWorkflowModel.xml),添加一个多值关联属性,用来关联刚才创建的wf:tableRow节点:<association name="wf:dataTable"> <title>数据表</title> <source> <mandatory>false</mandatory> <many>true</many> </source> <target> <class>wf:tableRow</class> <mandatory>false</mandatory> <many>false</many> </target> </association>配置工作流表单
在share-config-custom.xml里,针对你的工作流任务节点,配置表单字段并指定dynamic-table控件:<config evaluator="task-type" condition="wf:myCustomTask"> <forms> <form> <field-visibility> <show id="wf:dataTable" /> </field-visibility> <appearance> <field id="wf:dataTable" label="业务数据表"> <control template="/org/alfresco/components/form/controls/dynamic-table.ftl"> <!-- 指定每行对应的节点类型 --> <control-param name="childType">wf:tableRow</control-param> <!-- 显示添加行按钮 --> <control-param name="showAddButton">true</control-param> <!-- 显示删除行按钮 --> <control-param name="showRemoveButton">true</control-param> <!-- 配置每列的字段、标签和控件类型 --> <control-param name="fieldConfig"> <![CDATA[ [ {"id":"wf:column1", "label":"客户名称", "control":"textfield"}, {"id":"wf:column2", "label":"订单数量", "control":"number"}, {"id":"wf:column3", "label":"下单日期", "control":"date"} ] ]]> </control-param> </control> </field> </appearance> </form> </forms> </config>这里的
fieldConfig可以根据你的实际需求调整列的数量、字段类型和控件,比如用dropdown做下拉选择列,或者textarea做多行文本列。
方案二:自定义表单控件(应对复杂需求)
如果自带的dynamic-table样式或者交互满足不了你的要求(比如需要自定义校验、联动逻辑),可以自己写一个表单控件:
创建自定义FTL模板
在Share的WEB-INF/classes/alfresco/site-webscripts/org/alfresco/components/form/controls/目录下,新建custom-dynamic-table.ftl模板,用jQuery实现动态添加/删除行的逻辑,比如:<div class="custom-table-container"> <table id="dynamic-table" class="alfresco-forms-controls-DynamicTable"> <thead> <tr> <th>客户名称</th> <th>订单数量</th> <th>操作</th> </tr> </thead> <tbody> <!-- 初始行 --> <tr class="table-row"> <td><input type="text" name="column1[]" /></td> <td><input type="number" name="column2[]" /></td> <td><button class="remove-row">删除</button></td> </tr> </tbody> </table> <button id="add-row">添加新行</button> </div> <script type="text/javascript"> // 添加行逻辑 $("#add-row").click(function() { var newRow = $(".table-row:first").clone(); newRow.find("input").val(""); $("#dynamic-table tbody").append(newRow); }); // 删除行逻辑 $(document).on("click", ".remove-row", function() { $(this).closest(".table-row").remove(); }); </script>配置使用自定义控件
在share-config-custom.xml里,把表单字段的template路径改成你自定义的FTL路径即可:<control template="/org/alfresco/components/form/controls/custom-dynamic-table.ftl" />
注意事项
- 部署模型后一定要重启Alfresco服务,不然模型不会生效。
- 确保工作流任务的用户有创建
wf:tableRow节点的权限,默认的工作流权限一般足够,要是遇到权限报错,可以在模型里调整权限设置。 - 如果是Activiti工作流,要确保流程变量和表单字段的映射正确,避免数据无法传递。
内容的提问来源于stack exchange,提问作者Chafouette

