Forums

Home » Alloy UI » English

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Smilelws2010 lwz
Multiple portlet instances and Javascript
August 19, 2013 1:32 PM
Answer

Smilelws2010 lwz

Rank: Regular Member

Posts: 157

Join Date: December 16, 2010

Recent Posts

HI

I have an instanceable liferay portlet. On the view.jsp I have a checkbox and have a javascript function being called on onClick which evaluates the checkbox value and do some function. I am using aui:input type checkbox

 1
 2    <div>
 3        <aui:input name="testCategory" id="testCategory" type="checkbox" label="testCategory? " value=""
 4            checked="<%= (activeCount > 0) ? true : false %>" onChange="testCategory(this.id)"/>
 5    </div>
 6
 7<aui:script>
 8    function testCategory(checkBoxId){
 9        var checkBoxValue = document.getElementById(checkBoxId);
10       
11        if (checkBoxValue.checked==true){
12              // do something 1
13             }else{
14                  // do something 1
15             }   
16    }
17</aui:script>


The code works fine when I have only one portlet instance on my page. But when I add a second portlet to the same page, it is not the same. It might be some namespace issue.

SO when I check the value for checkbox in the second portlet the function is being carried out on the first portlet.

Please help.

Appreciate your time, attention and help.

Smile
Byrån Zaugg
RE: Multiple portlet instances and Javascript
August 19, 2013 3:21 PM
Answer

Byrån Zaugg

LIFERAY STAFF

Rank: Regular Member

Posts: 131

Join Date: April 5, 2012

Recent Posts

Try using <portlet:namespace/> to prevent Portlets from conflicting with each other.

 1
 2    <div>
 3        <aui:input name="<portlet:namespace/>testCategory" id="<portlet:namespace/>testCategory" type="checkbox" label="testCategory? " value=""
 4            checked="<%= (activeCount > 0) ? true : false %>" onChange="<portlet:namespace/>testCategory(this.id)"/>
 5    </div>
 6
 7<aui:script>
 8    function <portlet:namespace/>testCategory(checkBoxId){
 9        var checkBoxValue = document.getElementById(checkBoxId);
10       
11        if (checkBoxValue.checked==true){
12              // do something 1
13             }else{
14                  // do something 1
15             }   
16    }
17</aui:script>
Victor Zorin
RE: Multiple portlet instances and Javascript
August 19, 2013 6:56 PM
Answer

Victor Zorin

Rank: Liferay Legend

Posts: 1176

Join Date: April 14, 2008

Recent Posts

.. or if your javascript functions do not need to access 'portlet:namespace'd areas, put you methods into yourname.js file and declare this javascript file in your liferay-portlet.xml file as header-portlet-javascript element.
This way javascript methods should be loaded only once.
Smilelws2010 lwz
RE: Multiple portlet instances and Javascript
August 21, 2013 8:31 AM
Answer

Smilelws2010 lwz

Rank: Regular Member

Posts: 157

Join Date: December 16, 2010

Recent Posts

Thanks everyone.
It helped a lot. Still some trouble hoping to fix it.