Wednesday, August 20, 2014

Liferay auto fields

This is a very good feature in liferay and it is available out-of-box in liferay.

We have created html fields and we need to repeat multiple times in the form, we can use the alloyui module that is liferay-auto-fields.

How to use?

Inside Jsp


Note:- Here, you have to use alloyUI form to support this feature.

 <div id="fields">  
 <div class="lfr-form-row lfr-form-row-inline">  
  <div class="row-fields">  
  /* Html Form Fields like Number, Extension, Type etc. */  
  <aui:input id="number0" name="number0" label="Number"></aui:input>  
  </div>  
 <div>  
 </div>  

Here, whatever fields we add in row-fields div that will be repeated multiple time on click.

AlloyUI Script

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

Here,
contentBox -  main div on which it will be applied
fieldIndexes - name of variable that we need to use in java class for getting all selected field values by index

In Action class 

 public void getFieldsData(ActionRequest actionRequest,ActionResponse response)  
                   throws Exception {  
  String fieldIndexes= actionRequest.getParameter("fieldIndexes");  
  int[] fieldIndexesArray = StringUtil.split(fieldIndexes, 0);//we will get indexes of selected multiple fields  
     for (int fieldIndex : fieldIndexesArray) {  
        String number = ParamUtil.getString(actionRequest, "number" + fieldIndex);  
        //we can get values by specifying name of field and fieldIndex                      
     }  
 }  




No comments:

Post a Comment

Popular Posts