Saturday 20 September 2014

Liferay auto fields, adding multiple fields in Liferay, multiple elements adding dynamically

Hi in this post I just post my code of auto field.
Auto field related post is there in net but I faced some problem so I am posting my code that will helpful for me in future and may be helpful for others.

my jsp page code

<%
PortletURL updateUserPassword = renderResponse.createActionURL();
updateUserPassword.setParameter(ActionRequest.ACTION_NAME, "updateUserPassword");
%>

<aui:form name="fm" method="POST" action="<%=updateUserPassword.toString()%>" >


<div id="phone-fields">
    <div class="lfr-form-row lfr-form-row-inline">
      <div class="row-fields" style="display:flex;">
        <aui:input fieldParam='groupMemberName1' id='groupMemberName1' name="groupMemberName1" label="group-member-name" />
        <aui:input fieldParam='groupMemberEmail1' id='groupMemberEmail1' name="groupMemberEmail1" label="group-member-email" />
        <aui:select id="groupMemberGender1" name="groupMemberGender1" label="group-member-gender">
          <aui:option value="male" label="group-member-female"></aui:option>
          <aui:option value="female" label="group-member-male"></aui:option>
        </aui:select>
    <aui:input fieldParam='groupMemberJob1' id='groupMemberJob1' name="groupMemberJob1" label="group-member-job" />
      </div>
    </div>
  </div>
</aui:form>
here id="phone-fields" is the id of div that we will pass to our aui script that will take care to add duplicate fields.
Inside my div 4 fields is there so if we press plus button then these 4 fields will create again and again with different ids.

<aui:script>

AUI().use('liferay-auto-fields',function(A) {
 new Liferay.AutoFields(
       {
           contentBox: '#phone-fields',
           fieldIndexes: '<portlet:namespace />phonesIndexes'
       }
   ).render();
   });
</aui:script>



So jsp is complete now I want to read its value in action class.


public void updateUserPassword(ActionRequest actionRequest, ActionResponse actionResponse) throws PortalException, SystemException
{
String numberOfRowsString = actionRequest.getParameter("phonesIndexes");
String[] indexOfRows = numberOfRowsString.split(",");

for( int i=0; i<indexOfRows.length ; i++) //getting value of each row
{
 String groupMemberName = (actionRequest.getParameter("groupMemberName"+indexOfRows[i])).trim(); //as field name constant but there indexes are changes
String groupMemberEmail = (actionRequest.getParameter("groupMemberEmail"+indexOfRows[i])).trim();
String groupMemberGender = actionRequest.getParameter("groupMemberGender"+indexOfRows[i]);
String groupMemberJob = (actionRequest.getParameter("groupMemberJob"+indexOfRows[i])).trim();//deleting leading and trailing space from string
long userGroupId = Long.parseLong((actionRequest.getParameter("groupID1")));

}

variable indexOfRows  will get indexes of rows that user added from jsp.
One row is always there. If user added 5 rows then the value is
{1,2,3,4,5}.
This may be the case user added 4 rows then deleted one row, suppose second row then the value in
indexOfRows is {1,3,4}. We just use fix name(id) of element and then adding indexes to it will get values.


Thanks and regards
md asif aftab


No comments:

Post a Comment