2012-12-10 38 views
6

我在我的liferay portlet中使用AlloyUI。如何在liferay中获取客户端portlet-id?

我想在JavaScript中使用我的<input>的ID。问题是元素的ID在客户端更改。

例如:
如果设置一个<input>的ID设置为‘用户名’,它被改变成_hospital_WAR_hospitalportlet_userName_hospital_WAR_hospitalportlet_被附加到ID,其中Hospital是我的组件名称。

我如何获得客户端ID,以便我可以在jQuery中使用它?

回答

7

<input>Id前面的字符串_hospital_WAR_hospitalportlet_不过是portlet命名空间。

此,如果您使用<aui:input>标签只预置到您的<input>name & id属性,如果你只是用普通的香草HTML <input>标签name & id属性没有改变。

但因为它是使用<aui:input>,你可以做以下,以获得portlet的命名空间在你的JavaScript代码的好做法:

  1. 如果您使用的使用中<script> .. </script>或JSP,即内部的JavaScript <aui:script> .. </aui:script>然后你可以使用<portlet:namespace /><%= renderResponse.getNamespace() %>在你的javascript里面得到字符串_hospital_WAR_hospitalportlet_,就像。

    jQuery("#<portlet:namespace />username").val(); 
    
    // Or 
    
    jQuery("#<%= renderResponse.getNamespace() %>username").val(); 
    
  2. 但是,如果你使用的是*.js文件,那么我建议你要把这个名字空间作为参数传递给JavaScript函数在js文件:

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

    调用从JSP此功能:

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

希望这会有所帮助。我不知道是否有办法直接通过Liferay定义的某个JavaScript函数获取namespaceportletId。如果你有这样的东西,你可以在这里发布,这将是非常有益的。

4

试试这个:

Liferay.Portlet.ready(

    /* 
    This function gets loaded after each and every portlet on the page. 

    portletId: the current portlet's id 
    node: the Alloy Node object of the current portlet 
    */ 

    function(portletId, node) { 
    } 
);