Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get client side portlet-id in liferay?

I'm using AlloyUI in my liferay portlet.

I want to use my <input>'s id in javascript. The problem is that the id of the elements are changed in client side.

For example:
If I set an <input>'s Id to "username" it is changed to _hospital_WAR_hospitalportlet_userName i.e. _hospital_WAR_hospitalportlet_ is appended to the Id, where Hospital is my portlet name.

How can I get client-side Id so that I can use it in jquery?

like image 340
Beginner Avatar asked Dec 10 '12 11:12

Beginner


People also ask

How do you get a portlet name in Liferay?

RE: How to get current portlet's name? portletDisplay is a variable that will be already available in your JSP if you've included Liferay's init. jsp or have added yourself an invocation to the theme:defineObjects tag.

What is portlet namespace?

Namespacing ensures that a given portlet's name is uniquely associated with elements in request parameters it sends to the portal. This prevents name conflicts with other elements on the portal page and with elements from other portlets on the page. Namespacing your portlet elements is easy.


1 Answers

The string _hospital_WAR_hospitalportlet_ prepended to the Id of the <input> is nothing but the portlet-namespace.

This is only prepended to your <input>'s name & id attribute if you use <aui:input> tag and the name & id attributes are not changed if you just use plain-vanilla html <input> tag.

But as it is a good practice to use <aui:input> you can do the following to get the portlet-namespace in your javascript code:

  1. If you are using javascripts inside a JSP i.e. using within <script> .. </script> or <aui:script> .. </aui:script> then you can use <portlet:namespace /> or <%= renderResponse.getNamespace() %> to get the string _hospital_WAR_hospitalportlet_ inside your javascript, something like.

    jQuery("#<portlet:namespace />username").val();
    
    // Or
    
    jQuery("#<%= renderResponse.getNamespace() %>username").val();
    
  2. But if you are using a *.js file then I suggest you pass the namespace as an argument to the javascript function in the js file:

    function changeValues(portletNamespace) { // this function is in a js file
        jQuery("#" + portletNamespace + "username").val("Lets change");
    }
    

    calling this function from the JSP:

    <input type="button" onClick="changeValues('<portlet:namespace />')" />
    

Hope this helps. I don't know if there is a way to get the namespace or portletId directly through some javascript function defined by Liferay. If you get something like that you can post it here and that would be very helpful.

like image 107
Prakash K Avatar answered Sep 29 '22 17:09

Prakash K