Search This Blog

Tuesday, 8 October 2013

Using checkbox in an Aui:Form

InCase you are facing an issue fetching the appropriate value of a checkbox field in an Aui-Form..this post is definetely going to of some help..

-----------------------------------------------------------------------------------------------------
Creating a Checkbox field using an Aui:form in a JSP..

<aui:form action="<%=submitUrl.toString() %>" type="post" >
 <aui:input name="view" id="view" type="checkbox" label="View" onchange="onChangeView(this);"/>
  <aui:button name="submit" type="submit" />
</aui:form>

-----------------------------------------------------------------------------------------------------
Retrieving the Checkbox field values in a JavaScript function..

function onChangeView(){

// Not Working Correctly
var viewChecked = document.getElementById("<portlet:namespace/>view").checked;  [X]

// Whenever i tried to check if the checkbox is checked or not it was always showing false even if it was
// checked.
// Later ,I came to know through some blogs/forums-post[courtsey - Atin Agarwal ..still remem the name] 
// that in AUI if the input type type is checkbox then it
// appends "Checkbox" to the name of the field.
// like hereby the name of the field was "view" but it was making : "viewCheckbox". It automatically adds this //redundant string "Checkbox" at the end of  the name you give.
// So ,in Case to execute it correctly we have to write something like :

//Works just as Expected
var viewChecked = document.getElementById("<portlet:namespace/>viewCheckbox").checked; [Y]
alert("viewChecked == " +viewChecked);

}

-----------------------------------------------------------------------------------------------------
Retrieving the Checkbox field values in Portlet class..

public void doAssignUserBasedPermission(ActionRequest actionRequest ,ActionResponse actionResponse){
//Works just as Expected
boolean  viewChecked = ParamUtil.getBoolean(actionRequest, "view");
System.out.println(" viewchecked = "+viewChecked);
}

No comments:

Post a Comment