Skip to content

Instantly share code, notes, and snippets.

@jamesfalkner
Created November 15, 2013 20:23
Show Gist options
  • Save jamesfalkner/7490976 to your computer and use it in GitHub Desktop.
Save jamesfalkner/7490976 to your computer and use it in GitHub Desktop.
## example expando with json
#if ($request.lifecycle == "RENDER_PHASE")
<p><em>Expando Demo 2: Creating and Reading Expando Data from AJAXified WCM Portlet Resource</em></p>
<div id="dataTable"></div>
<body onload="getExpando()">
<script type="text/javascript">
function getExpando() {
AUI().use(
"aui-base", "aui-io-plugin", "aui-io-request", "aui-datatable",
function (A) {
A.io.request(
'${request.resource-url}',
{
dataType: "json",
on: {
success: function (event, id, obj) {
var responseData = this.get("responseData");
var dt = document.getElementById("dataTable");
var pre = document.createElement("pre");
pre.innerHTML = JSON.stringify(responseData);
dt.appendChild(pre);
}
}
}
);
}
);
}
</script>
</body>
#elseif ($request.lifecycle == "RESOURCE_PHASE")
#set ($expandoConstants = $portal.getClass().forName("com.liferay.portlet.expando.model.ExpandoColumnConstants"))
#set ($expandoConstantsInstance = $expandoConstants.newInstance())
#set ($stringType = $expandoConstants.getField("STRING").get($expandoConstantsInstance))
## create table
#set ($table = $expandoTableLocalService.getTable($getterUtil.getLong($companyId), "org.lrnas2.james", "MyExpandoTable"))
#if ($validator.isNull($table))
#set ($table = $expandoTableLocalService.addTable($getterUtil.getLong($companyId), "org.lrnas2.james", "MyExpandoTable"))
#end
#set ($col = $expandoColumnLocalService.addColumn($table.getTableId(), "FavoriteColor", 15))
#foreach ($color in ["red", "blue", "pink", "green"])
#set ($V = $expandoValueLocalService.addValue($getterUtil.getLong($companyId), "org.lrnas2.james", "MyExpandoTable", "FavoriteColor", $color.hashCode(), $color))
#end
#set ($result = $jsonFactoryUtil.createJSONArray())
#foreach ($rowVal in $expandoValueLocalService.getColumnValues($getterUtil.getLong($companyId), "org.lrnas2.james", "MyExpandoTable", "FavoriteColor", -1, -1))
#set ($colorJson = $jsonFactoryUtil.createJSONObject())
#set ($V = $colorJson.put("color", $rowVal.getString()))
#set ($V = $result.put($colorJson))
#end
$result
#end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment