by
121 9
0
6
371k
3
Top 1% !
Popular
Famous
Pearl of Wisdom
Specified
Refined
Popularity: 2416th place
Created
Modified Feb 22, 2013
This snippet is Public
No tags for this snippet yet.
LanguageJava
LanguageJava Server Pages
LanguageJavaScript
LicenseMIT_X11
SourceGithub
TechnologyAJAX

Gist to demonstrate ajax request on checkbox

Gist to demonstrate ajax request on checkbox: 
ajax-checkbox.tag
ajaxCheckbox.java
main.js
view.jsp
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="https://www.snip2code.com/Embed/836/Gist-to-demonstrate-ajax-request-on-chec?startLine=0"></iframe>
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
// Tagfile to be placed in WEB-INF/tags/ui <%@tag import="com.liferay.portal.kernel.util.HtmlUtil"%> <%@tag import="com.liferay.portal.kernel.language.LanguageUtil"%> <%@tag import="com.liferay.portal.kernel.util.StringPool"%> <%@tag import="com.liferay.portal.util.PortalUtil"%> <%@ attribute name="resourceUrl" description="URL to process the ajax call. ServeResource parameter: newValue (0 or 1 for unchecked, checked). responseData is not used. An alert is open on failure." type="String" required="true" %> <%@ attribute name="name" description="Checkbox name. A random name is set if this is not provided." type="String"%> <%@ attribute name="label" description="Checkbox label" type="String" required="true" %> <%@ attribute name="initialValue" description="Initial value of checkbox" type="Boolean" required="true" %> <%@ attribute name="successCallback" description="ajax callback on success. Parameters are: checkbox node, response data." type="String" %> <%@ attribute name="failureCallback" description="Callback on failure. Parameter is checkbox name." type="String" %> <%@ taglib uri="http://liferay.com/tld/theme" prefix="theme" %> <%@ taglib uri="http://java.sun.com/portlet" prefix="portlet" %> <%@ taglib uri="http://alloy.liferay.com/tld/aui" prefix="aui" %> <portlet:defineObjects /> <theme:defineObjects /> <% if (name == null){ name = PortalUtil.generateRandomKey(request, "ajaxToggleKey"); } String onclick = "bb_ajaxCheckbox('" + resourceUrl + "', this, " + successCallback + "," + failureCallback + ")"; %> <aui:input type="checkbox" onClick="<%=onclick%>" value="<%=initialValue%>" name="<%=name%>" label="<%=label%>" /> @Override public void serveResource(ResourceRequest resourceRequest, ResourceResponse resourceResponse) throws IOException, PortletException { _log.info("processing resource"); Map<String, String[]> parameters = resourceRequest.getParameterMap(); for (String key : parameters.keySet()){ _log.info(key + ":[" + StringUtil.merge(parameters.get(key)) + "]"); } String resourceId = resourceRequest.getResourceID(); if (Validator.isNotNull(resourceId)) { String cmd = resourceRequest.getResourceID(); if (cmd.equals("testToggle")) { try { boolean newValue = ParamUtil.getBoolean(resourceRequest,"newValue"); _log.info(String.valueOf(newValue)); if (!newValue){ throw new IOException("simulate failure"); } else { resourceResponse.setContentType("text/plain"); resourceResponse.getWriter().write("this is the response data"); } } catch(Exception e){ throw new IOException(e); } } } super.serveResource(resourceRequest, resourceResponse); } Liferay.provide( window, "bb_ajaxCheckbox", function(resourceUrl, checkbox, success, failure){ var A = AUI(); var checked = false; var checkbox = A.one(checkbox); var label = null; if (checkbox) { checked = checkbox.attr('checked'); var labelSelector = 'label[for="' + checkbox.attr("name") + '"]'; if (labelSelector) label = A.one(labelSelector); } var ajaxCallbacks = { success: function(){ if (label) label.removeClass("ajax-loading"); if (success) success(checkbox, this.get('responseData')); }, failure: function(){ if (failure) failure(checkbox); } }; if (label) label.addClass("ajax-loading"); A.io.request(resourceUrl, { method: 'GET', data: {newValue: checked,}, on : ajaxCallbacks, }); }, [ 'aui-io-request' ] ); <%@ taglib tagdir="/WEB-INF/tags/ui" prefix="bamboost-ui"%> <%@ include file="/html/init.jsp"%> <portlet:resourceURL id="testToggle" var="testToggleURl"/> <bamboost-ui:ajax-checkbox name="myname" resourceUrl="<%=testToggleURl %>" initialValue="true" label="no callback"/> <bamboost-ui:ajax-checkbox resourceUrl="<%=testToggleURl %>" initialValue="true" label="testSuccess" successCallback="testSuccess"/> <bamboost-ui:ajax-checkbox resourceUrl="<%=testToggleURl %>" initialValue="true" label="testFailure" failureCallback="testFailure"/> <script> function testSuccess(checkbox, responseData) { alert(checkbox.attr("name") + "\n" + responseData); }; function testFailure() {alert("failure");}; </script>
If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus