Friday, January 14, 2011

Retrieve selected checkbox value in liferay 6 with aui:script and Liferay.provide

FYI: Everything inside of an aui:script block that contains a "use" attribute is called asynchronously, so it could fire right away (if the resources are on the page) or if the network resolution is taking a long time, it could execute later on.

Bellow is an example:


alert('not "using" any modules, Im fired right away');
alert('"using" aui-io, Im fired when its ready');


So we make sure never to define a function inside of an aui-script block that has a "use" attribute, unless we're creating another module to be used.

But then how do you define a function that uses a module?
Luckily, we can use Liferay.provide.

For simplicity here I have used two field which will indicated the checked and unchecked value of checkboks and can easily get this value on action class, like bellow:


<aui:input name="addCommIds" type="hidden" value="" />
<aui:input name="removeCommIds" type="hidden" value="" />


Bellow given the aui:script code:



Liferay.provide(
window,
'saveEntry',
function() {
document.fm.addCommIds.value = Liferay.Util.listCheckedExcept(document.fm, "allRowIds");
document.fm.removeCommIds.value = Liferay.Util.listUncheckedExcept(document.fm, "allRowIds");
submitForm(document.fm);
},
['liferay-util-list-fields']
);


NB: listCheckedExcept is a javascript function
we can simply call this function as bellow:


<aui:button onClick="<%= renderResponse.getNamespace() + "saveEntry();" %>"value="save" />


Source

1 comment:

  1. Hi,

    The usage is not clear. Please explain with an example.

    with Regards
    Suman Kumar

    ReplyDelete