jQuery

Component identification parameters

Name | Value
--- | ---
component-type | org.richfaces.JQuery
component-class | org.richfaces.component.html.HtmlJQuery
component-family | org.richfaces.JQuery
renderer-type | org.richfaces.JQueryRenderer
tag-class | org.richfaces.taglib.JQueryTag

## Creating the Component with a Page Tag

To create the simplest variant on a page, use the following syntax:

Example:

    ...
          <rich:jQuery selector="#customList tr:odd" timing="onload" query="addClass(odd)" />
    ...

## Creating the Component Dynamically Using Java

Example:

    import org.richfaces.component.html.HtmlJQuery;
    ...
    HtmlJQuery myJQuery = new HtmlJQuery();
    ...

## Details of Usage

<rich:jQuery> can be used in two main modes:

- as a one-time query applied immediately or on a document ready event
- as a JavaScript function that can be invoked from the JavaScript code

The mode is chosen with "timing" attribute that has the following options:

- immediate - applying a query immediately
- onload - applying a query when a document is loaded
- onJScall - applying a query by invoked JavaScript function defined with the "name" attribute

Definition of the "name" attribute is mandatory when the value of "timing" attribute is "onJScall". If the "name" attribute is defined when "timing" value equals to "immediate" or "onload", the query is applied according to this value, but you still have an opportunity to invoke it by a function name.

The "selector" attribute defines an object or a list of objects. The query is defined with the "query" attribute.

Here is an example of how to highlight odd rows in a table:

Example:

    ...
        <style>
            .odd {
                    background-color: #FFC;
            }
        </style>
        ...

        <rich:table id="customList" ...>
            ...
        </rich:table>
        ...
        <rich:jQuery selector="#customList tr:odd" timing="onload" query="addClass(odd)" />
    ...

The "selector" attribute uses defined by w3c consortium syntax for CSS rule selector with some jQuery extensions.

Those are typical examples of using selector in the <rich:jQuery> component.

Examples of using selector

Selector | Comment
--- | ---
"p[a]" | In a document all "p" tags with "a" tag inside are selected
"ul/li" | All "li" elements of unordered "ul" lists are selected
"p.foo[a]" | All "p" tags with "foo" class and inserted "a" tag are selected
"input[@name=bar]" | All "input" tags with "name" attribute which value is "bar" are selected
"input[@type=radio][@checked]" | All "input" tags with attribute "type"="radio" and attribute value = "chekced" are selected
"p,span,td" | All tag elements "p" or"span" or "td" are selected
"p#secret" | "p" paragraph element with "id" identification = "secret" is selected
"p span" | "span" tag is a (direct or non-direct) child of "p" tag. If it's necessary, use "p > span" or "p/span" is selected
"p[@foo^=bar]" | "p" tag containing "foo" attribute with textual value beginning with "bar" word is selected
"p[@foo$=bar] " | "p" tag containing "foo" attribute with textual value ending with "bar" word is selected
"p[@foo*=bar] " | "p" tag with "foo" attribute containing substring "bar" in any place is selected
"p//span " | "span" tag that is a (direct or non-direct) child of "p" tag is selected
"p/../span " | "span" tag that is a grandchild of "p" tag is selected

In addition, RichFaces allows using either a component id or client id if you apply the query to a JSF component. When you define a selector, RichFaces examines its content and tries to replace the defined in the selector id with component id if it's found.

For example, you have the following code:

    ...
        <h:form id="form">
            ...
            <h:panelGrid id="menu">
                <h:graphicImage ... />
                <h:graphicImage ... />
                ...
            </h:panelGrid>
        </h:form>
    ...

The actual id of the <h:panelGrid> table in the browser DOM is "form:menu". However, you still can reference to images inside this table using the following selector:

    ...
        <rich:jQuery selector="#menu img" query="..." />
    ...

You can define the exact id in the selector if you want. The following code reference to the same set of a DOM object:

    ...
        <rich:jQuery selector="#form\\:menu img" query="..." />
    ...

Pay attention to double slashes that escape a colon in the id.

In case when the "name" attribute is defined, <rich:jQuery> generates a JavaScript function that might be used from any place of JavaScript code on a page.

There is an example of how to enlarge the picture smoothly on mouse over event and return back to the normal size on mouse out:

    ...
        <h:graphicImage onmouseover="enlargePic(this)" width="50" value="/images/price.gif"
                    onmouseover="enlargePic(this, {pwidth:'60px'})" onmouseover="releasePic(this)"  />
        <h:graphicImage onmouseover="enlargePic(this)" width="50" value="/images/discount.gif"
                    onmouseover="enlargePic(this, {pwidth:'100px'})" onmouseover="releasePic(this)"  />
        ...
        <rich:jQuery name="enlargePic" timing="onJScall" query="animate({width:param.pwidth})" />
        <rich:jQuery name="releasePic" timing="onJScall" query="animate({width:'50px'})"/>
    ...

The JavaScript could use two parameters. The first parameter is a replacement for the selector attribute. Thus, you can share the same query, applying it to the different DOM objects. You can use a literal value or a direct reference for an existing DOM object. The second parameter can be used to path the specific value inside the query. The JSON syntax is used for the second parameter. The "param." namespace is used for referencing data inside the parameter value.

<rich:jQuery> adds styles and behavior to the DOM object dynamically. This means if you replace something on a page during an Ajax response, the applied artifacts will be overwritten. You are resistible to apply them again after the Ajax response is complete.

Usually, it could be done with reRendering the <rich:jQuery> components in the same Ajax interaction with the components these queries are applied to. Note, that queries with "timing" attribute set to "onload" are not invoked even the query is reRendered, because a DOM document is not fully reloaded during the Ajax interaction. If you need to re-applies query with "onload" value of "timing" attribute , define the "name attribute and invoke the query by name in the "oncomplete" attribute of the Ajax component.

RichFaces includes jQuery JavaScript framework. You can use the futures of jQuery directly without defining the <rich:jQuery> component on a page if it is convenient for you. To start using the jQuery feature on the page, include the library to the page with the following code:

    ...
        <a4j:loadScript src="resource://jquery.js"/>
    ...

Refer to the jQuery documentation for the right sintax. Remember to use jQuery() function instead of $(), as soon as jQuery works without conflicts with prototype.js.

## Look-and-Feel Customization

<rich:jQuery> has no skin parameters and custom style classes, as the component isn't visual.

## Relevant Resources Links

More information about jQuery framework and its features you can read here.

How to use jQuery with other libraries see here.