在liferay中将动态元素添加到aui表单中

时间:2014-03-13 09:43:30

标签: javascript forms liferay alloy-ui liferay-aui

如何使用script或aui:script在liferay中添加动态aui表单元素?如果那是不可能的,是否有任何替代解决方案。

我有一个有两个部分的aui表格。点击一个按钮,我想通过javascript动态地向表单添加新的部分。我尝试使用document.createElement(),但通过使用它,我们将只能创建HTML dom元素。我想创建aui元素,如aui:input,aui:select等

这就是我的表单的结构:

enter image description here


假设我在第二部分有一个按钮。当我点击它时,我想在aui:form中添加一个aui:fieldset元素作为子元素。

2 个答案:

答案 0 :(得分:7)

  

我们将只能创建HTML dom元素。我想创建aui元素,如aui:input,aui:select等

请理解aui:inputaui:select等是JSP标记,这意味着它们是服务器端,最终由容器呈现为HTML元素,这就是您在浏览器中看到的内容。只是这些元素用一些<div> s&amp; <span> s(HTML元素!)并拥有自己的css类。

因此,如果您想创建另一个表单元素,只需单击一个按钮,就必须使用document.createElementdocument.innerHTML。 Javascript与服务器端无关,因此无法生成或呈现aui标记,但您可以创建HTML元素并添加到与aui标记类似的表单中。

以下是一些基础教程,帮助您开始使用Alloy UI javascript:

  1. Working with Elements and Events,您可以向下滚动到Manipulating elements标题。
  2. Alloy UI - working with Nodes
  3. Alloy UI Form builder,仅适用于Liferay 6.2。
  4. Liferay的做事方式

    在用户和组织模块中添加地址和语音(控制面板组织编辑标识部分地址),您可以检查以下JSP:

    1. /portal-web/docroot/html/portlet/users_admin/common/addresses.jsp
    2. /portal-web/docroot/html/portlet/users_admin/common/phone_numbers.jsp

    3. 编辑 (根据OP的评论)

      1. Tutorial on Liferay Auto-fields
      2. 本教程的
      3. Source code

      4. 上述LiferaySavvy Link启发的自动字段简短教程:-) 根据stackoverflow的策略和方便用户

        解释以代码注释的形式给出。

        1. 用于创建动态字段的Javascript代码:

          <aui:script use="liferay-auto-fields">
          new Liferay.AutoFields(
              {
                  contentBox: '#phone-fields', // this is the container within which the fields would be added dynamically
                  fieldIndexes: '<portlet:namespace />phonesIndexes' // this is the field which will store the values of the 
              }
          ).render();
          </aui:script>
          
        2. 使用javascript的JSP或HTML代码:

          <aui:form action="<%=editArticleActionURL%>" method="post" name="LiferayAutoFieldForm">
              <div id="phone-fields">
                  <div class="lfr-form-row lfr-form-row-inline">
                      <div class="row-fields">
                          <!--
                              Notice the zero at the end of the name & id of the input element "phoneNumber0".
                              When you add dynamic fields this would be incremented.
                          -->
                          <aui:input fieldParam='phoneNumber0' id='phoneNumber0' name="phoneNumber0" label="Phone Number" />
                          <aui:select id="phoneTypeId0" name="phoneTypeId0" label="Type">
                              <aui:option value="11006" label="Business"></aui:option>
                              <aui:option value="11007" label="Business Fax"></aui:option>
                              <aui:option value="11008" label="Mobile Phone"></aui:option>
                              <aui:option value="11009" label="Other"></aui:option>
                              <aui:option value="11011" label="Personal"></aui:option>
                          </aui:select>
                      </div>
                  </div>
              </div>
              .... <!-- other input fields and submit buttons etc -->
          </aui:form>
          
        3. 在控制器中获取动态元素值的代码:

          String phonesIndexesString = actionRequest.getParameter("phonesIndexes"); // do you remember: fieldIndexes: '<portlet:namespace />phonesIndexes'? :-)
          int[] phonesIndexes = StringUtil.split(phonesIndexesString, 0);
          
          for (int phonesIndex : phonesIndexes) {
              String number = ParamUtil.getString(actionRequest, "phoneNumber" + phonesIndex);
              int typeId = ParamUtil.getInteger(actionRequest, "phoneTypeId" + phonesIndex);
          }
          
        4. 希望这有帮助。

答案 1 :(得分:2)

查看aui:auto-fields标签库。 让我们在用户帐户的电话管理中查看它的示例。