views:

49

answers:

1

Using RichFaces 3.3.3, JSF 1.2 and Facelets.

For some presentation purposes I need to wrap the contents of some divs with other divs to be able to style them a certain way.

I am using jQUery to do this as its cleaner when writing HTML and it can be controlled in one location, otherwise I have to hardcode the extra divs every place I want to use them.

eg. original JSF generated HTML:

<div id="j_id4:main-container">
    <div id="j_id4:j_id5:nav">...</div>
</div>

after using jQUery the HTML now looks like this:

<div id="j_id4:main-container">

    <div class="top"><div></div></div>

    <div class="middle">
            <div id="j_id4:j_id5:nav">...</div>
    </div>

    <div class="bottom"><div></div></div>

</div>

Can this cause problems? I am going against this rule from the jboss richfaces documentation which says:

Any Ajax framework should not append or delete, but only replace elements on the page. For successful updates, an element with the same ID as in the response must exist on the page. If you'd like to append any code to a page, put in a placeholder for it (any empty element). For the same reason, it's recommended to place messages in the "AjaxOutput" component (as no messages is also a message).

If I can't do this, what other approach would you suggest?

UPDATE 1:

I tried this on a page which has a rich:tabPanel control set to ajax mode, i made the first (default) one of the tab contents get the nested divs on dom ready via jQuery. it works fine the first time, but when i click the other tabs and come back to the first one the extra divs don't appear. i think this is because the js is fired only once at dom ready and then on richfaces rerendering of tabPanel control it rerenders as if it was unaware of the extra divs. is there a way to avoid this?

+1  A: 

As long as you aren't disturbing the existing element ID's, I don't forsee any technical problems. The rule by RichFaces is probably just to put themselves safe for the case an enduser encounters problems with that, mainly when deleting elements or appending elements with the same ID. At least, they didn't say "may not", but just "should not".

As to the actual problem: JSF is of course not aware of the newly added HTML elements and doesn't take them into account when re-rendering the HTML to the client side. You would need to write a callback function which "fixes" the HTML DOM structure after JSF's re-rendering. Alternatively, if inserting those elements already in the view side (the XHTML page) isn't an option, you may consider to write a custom renderer which renders the elements the way you want.

BalusC
I would just like to add: as JSF "updates" components by completely re-rendering them, it shouldn't matter what's actually inside the component. Also, I would suppose JSF recognizes the components simply by their IDs (as every component instance has unique ID), so special selectors that would depend on DOM structure, are not used.
Tuukka Mustonen
i tried it and experience some unwanted behaviour. see question update.
Moin Zaman
@BalusC: Is that a good way? Writing custom renderers? I did think about it, to write a a4j:customPanel or something like that that would draw my div structure and I can put my contents and other controls inside this. Is there a way to get JSF to be made aware of the new HTML?
Moin Zaman
The preferred way would be to define the elements straight in the view, but since that's apparently not an option (output is controlled by a single JSF component?), your best bet is a custom renderer. That's the easiest way to make JSF aware of the new HTML. You could lookup what renderer it uses and then extend it and override the appropriate method(s). If it's open source, you could copypaste the original method and alter where necessary. Another way would be to create a composite component based on the more basic components. But I think you already considered this?
BalusC
More related questions yet to be answered here: http://stackoverflow.com/questions/3882022/how-to-use-for-jquery-inside-domready-when-prototype-is-using-outside and http://stackoverflow.com/questions/3902687/richfaces-application-should-i-use-richdatatable-or-jqgrid-pros-cons
Moin Zaman
This goes too specific in with RichFaces and jqGrid. I don't have hands-on experience with both, so I can't answer from top of head. Sorry not to be able to answer your new question.
BalusC