function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
GerhardNewman2GerhardNewman2 

How do I callapse PageBlockSection by default on Page or by Controller?

Sorry, my original text is missing for some reason...... (user error I suspect!)
 
I have searched for an answer to this and found no solution.  My understanding is that on a normal SF page, it remembers at the user level if the user wants a section open or closed by default.  This works for me on normal pages.  On VisualForce pages this functionality does not work.
 
But what I would really like is to either set a parameter on my VF page so that permanently sections are closed by default.  Alternatively have it possible to manipulate the state of the section via the controller.


Message Edited by GerhardNewman2 on 11-06-2008 01:57 PM
Best Answer chosen by Admin (Salesforce Developers) 
JeremyKraybillJeremyKraybill
Yes, you can do this, with some tricky javascript. You have to get a javascript function that is a document "onload" handler to call "twistSection" on the image that is the little twist control for the page block section. Good info here on how to add onload handlers dynamically, I like Willison's solution as implemented below.

Example:

Code:
<apex:pageBlockSection id="xxxxx" showHeader="true" title="zyzzyzz" columns="2" collapsible="true">
[...]
</apex:pageBlockSection>

<script language="javascript">
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

function dotest() {
 twistSection(document.getElementById('j_id0:form9:j_id189:xxxxx').childNodes[0].childNodes[0]);
}

addLoadEvent(dotest);
</script>

 Just note that the argument to "getElementById" is the actual SF rendered element ID, and may change if your page composition changes.

HTH!

Jeremy

All Answers

VarunCVarunC
Did you found any solution for this. Also, I'm facing an issue where when I refresh using partial Refresh, i.e. AJAX, the PageblockSection seems broken, as on Clicking it does not hide the content as it should have done ...
JeremyKraybillJeremyKraybill
Yes, you can do this, with some tricky javascript. You have to get a javascript function that is a document "onload" handler to call "twistSection" on the image that is the little twist control for the page block section. Good info here on how to add onload handlers dynamically, I like Willison's solution as implemented below.

Example:

Code:
<apex:pageBlockSection id="xxxxx" showHeader="true" title="zyzzyzz" columns="2" collapsible="true">
[...]
</apex:pageBlockSection>

<script language="javascript">
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

function dotest() {
 twistSection(document.getElementById('j_id0:form9:j_id189:xxxxx').childNodes[0].childNodes[0]);
}

addLoadEvent(dotest);
</script>

 Just note that the argument to "getElementById" is the actual SF rendered element ID, and may change if your page composition changes.

HTH!

Jeremy

This was selected as the best answer
VarunCVarunC
Thanks a lot .. that worked like charm .. but now I'm stuck with another issue of its sort ... :( ..

My VF page code looks something like this:
Code:
<apex:page>
<apex:pageBlock id="page1">
.. [command button that invoke or reRender the other page block (page2)] ..
</apex:pageBlock>

<apex:pageBlock id="page2">
.. <apex:pageBlockSection>
... <apex:pageBlockTable>
... </apex:pageBlocktable>
.. </apex:pageBlockSection>
</apex:pageBlock>

there is some databound action that is called on button click. But aftre AJAX action, the data is refreshed alright in datatable, but the SECTION is now Not Enclosing the datatable .. :( .. I mean, Collapsing or opening the Section Does not Show and Hide the datatable content ..

can you help me in here too ... ?

JeremyKraybillJeremyKraybill
Not sure I know your exact issue, but it seems a likely solution would be to tweak the 'reRender' attribute -- move it to higher elements or lower elements (e.g. pageBlockSection / table) and see if that helps. If it doesn't, you may need to either add some weird javascript to re-arrange the page elements, or do away with the ajax requests and force a full page refresh.
VarunCVarunC
Thanks, i tried that disn't worked .. :( .. Page Postback works .. but not with AJAX ... also this specific function also calls only on page Load .. can we call or use it on AJAX call complete .. ?
JeremyKraybillJeremyKraybill
sure, setting that function as an "oncomplete" attribute of a commandButton should work fine.
Paulo CastroPaulo Castro

It would be nice to have this feature natively.

 

If you want too, please help to promote this ideia:

 

http://ideas.salesforce.com/article/show/10098227/Add_Collapsedtrue_option_to_ltapexpageblocksection