Saturday, May 23, 2015

Liferay Expando Tutorial

http://www.liferay.com/web/raymond.auge/blog/-/blogs/expandos-what-are-they-and-how-do-they-help-me-liferay-portal-%3E5-0-1

One can use Expando Service when:
   Don't have access to backend/server side but is given a role in Liferay who can edit/add Journal Content or Web Content. This way we can create a virtual Database Tables so user inputs could be saved and could later retrieve them when needed.
  
The following template is a very simple liferay Expando example. It could Save/Delete user name on database.

Step 1.  Create a very basic(any) Journal Content Structure on the Form and Presentation section.

Step 2. Create a new Journal Content Template on the Form and Presentation section and map it onto your previously created Structure on Step 1.
Paste these codes on the Template's script(Launch Editor).






  

 #set ($renderUrl = $request.get("render-url"))
#set ($namespace = $request.get("portlet-namespace"))
#set ($cmd = $request.get("parameters").get("cmd"))
#set ($classPK = $getterUtil.getLong($request.get("parameters").get("classPK")))

#if ($classPK <= 0)
    #set ($classPK = $dateTool.getDate().getTime())
#end

#set ($userName = $request.get("parameters").get("userName"))

#set ($ourTableName = "OurTable")
#set ($ourTable = $expandoTableLocalService.getTable($ourTableName, $ourTableName))

#if (!$ourTable && $request.get("parameters").get("cmd") == 'save')
    ##Table doesn't exists. Creating Our Table....
    #set ($ourTable= $expandoTableLocalService.addTable($ourTableName, $ourTableName))

    #set ($ourTableId = $ourTable.getTableId())

    #set ($V = $expandoColumnLocalService.addColumn($ourTableId, "userName", 15)) ## STRING

    ##Table Created....
#else
    ##Table already exists.....  
#end

#if ($request.get("parameters").get("cmd") == 'save')
    ##Saving Data....
    #set ($userName = $request.get("parameters").get("userName"))
  
    #set ($V = $expandoValueLocalService.addValue($ourTableName , $ourTableName , "userName", $classPK, $userName))

    ##User Setting Saved....
#elseif ($request.get("parameters").get("cmd") == 'delete')
    ##Deleting User Setting...
    #set ($V = $expandoRowLocalService.deleteRow($ourTableName, $ourTableName, $classPK))
    ##User Setting Deleted...
#end



<table class="lfr-table">
    <tr>
        <th>ClassPK</th>
        <th>User Name</th>
        <th></th>
    </tr>


   ##
    ## Get all the current records in our ExpandoTable.
    ##

    #set ($rowsCount = $expandoRowLocalService.getRowsCount($ourTableName, $ourTableName))
    #set ($rows = $expandoRowLocalService.getRows($ourTableName, $ourTableName, -1, -1))

    #foreach($row in $rows)
        ##
        ## Get the classPK of this row.
        ##

        #set ($currentClassPK = $row.getClassPK())

        <tr>
            <td>${currentClassPK}</td>

            #set ($currentUserName = $expandoValueLocalService.getData($ourTableName, $ourTableName, "userName", $currentClassPK, ""))
            <td>${currentUserName}</td>
            <td><a href="#" onclick="document.getElementById('classPK').value = '${currentClassPK}'; document.getElementById('cmd').value = 'delete'; document.forms['${namespace}fm10'].submit();">
            Delete
            </a></td>       
           
        </tr>
    #end

    #if ($rowsCount <= 0)
        <tr>
            <td colspan="5">No Users were found.</td>
        </tr>
    #end

    </table>


<form action="$renderUrl" method="post" name="${namespace}fm10">
    <input type="hidden" id="cmd" name="${namespace}cmd" value="none" />
    <input type="hidden" id="classPK" name="${namespace}classPK" value="none" />
    <input type="text" id="userName" name="${namespace}userName" value="">
    <input type="button" value="Add user" onclick="document.getElementById('cmd').value = 'save'; document.forms['${namespace}fm10'].submit(); ">
</form>


Step 3. Save the Template and "Save and Approve" the new Journal Content. You should have a page similar to this:
Thats it! Test the sample expando portlet.

1 comment:

  1. Wow, c'est génial. Nous pouvons y accéder à tout moment, .. j'aime vraiment ce post .. merci. Nous proposons également les services Liferay Java et Liferay Angular. contactez-nous pour plus de détails.

    ReplyDelete