admin管理员组文章数量:1277338
How can we add dynamic aui form elements in liferay by using script or aui:script ?? If that is not possible, is there any alternate solution.
I have an aui form which has two sections. On clicking of a button, I want to add new sections to the form dynamically through javascript. I tried using document.createElement(), but by using that, we will be able to create only HTML dom elements. I want to create aui elements like aui:input, aui:select, etc
This is how my form is structured:
Assume that I have a button in second section. When I click that, I want to add one more aui:fieldset element to the aui:form as a child.
How can we add dynamic aui form elements in liferay by using script or aui:script ?? If that is not possible, is there any alternate solution.
I have an aui form which has two sections. On clicking of a button, I want to add new sections to the form dynamically through javascript. I tried using document.createElement(), but by using that, we will be able to create only HTML dom elements. I want to create aui elements like aui:input, aui:select, etc
This is how my form is structured:
Assume that I have a button in second section. When I click that, I want to add one more aui:fieldset element to the aui:form as a child.
Share Improve this question asked Mar 13, 2014 at 9:43 Kiran KulkarniKiran Kulkarni 1,5043 gold badges18 silver badges41 bronze badges 1- You can have 2 divs hidden (having your fields respectively) in your form and show them based on the button clicks through javascript, will that not work ? – Ankit P Commented Mar 14, 2014 at 5:03
2 Answers
Reset to default 7we will be able to create only HTML dom elements. I want to create aui elements like aui:input, aui:select, etc
Kindly understand aui:input
, aui:select
etc are JSP tags meaning they are server-side and are ultimately rendered into HTML elements by the container and that is what you see in the browser. Its just that those elements are rendered with some <div>
s & <span>
s (which are HTML elements!) and have their own css-class.
So on a click of a button if you want to a create another form element than you have to use either document.createElement
or document.innerHTML
. Javascript has nothing to do with server side so it can't generate or render aui
tags, but you can create HTML elements and add to the form similar in style to the aui
tags.
Here are some basic tutorials to get you started with Alloy UI javascript:
- Working with Elements and Events, you can scroll down to
Manipulating elements
heading. - Alloy UI - working with Nodes
- Alloy UI Form builder, only works with Liferay 6.2.
Liferay way of doing things
Adding Addresses and Phonenumbers in Users and Organizations module (Control Panel → Organization → Edit → Identification section → Addresses), you can check the following JSPs:
- /portal-web/docroot/html/portlet/users_admin/mon/addresses.jsp
- /portal-web/docroot/html/portlet/users_admin/mon/phone_numbers.jsp
EDIT (As per the OP's ment)
- Tutorial on Liferay Auto-fields.
- Source code of the tutorial.
A Short tutorial on auto-fields inspired by LiferaySavvy Link above :-) As per the policy of stackoverflow and for the convenience of users
The explanation is given as code ments.
Javascript code to create dynamic fields:
<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>
JSP or HTML code to work with the javascript:
<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>
Code for fetching the dynamic element's values in our controller:
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); }
Hope this helps.
Look at aui:auto-fields
tag lib.
Let see an example of it within phone managment in user account.
本文标签: javascriptAdding dynamic elements to aui form in liferayStack Overflow
版权声明:本文标题:javascript - Adding dynamic elements to aui form in liferay - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741274902a2369672.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论