开发者

Visualforce load apex components via ajax on page load

开发者 https://www.devze.com 2023-01-19 04:21 出处:网络
Can someone tell me how to use ajax to load an apex pageBlockTable via ajax on page load?I\'ve seen examples showing how to use an apex actionFunction, but the samples are usually simple (e.g. - retur

Can someone tell me how to use ajax to load an apex pageBlockTable via ajax on page load? I've seen examples showing how to use an apex actionFunction, but the samples are usually simple (e.g. - returning a string from the controller and putting it on the page. My controller returns a list of sObjects and i'm just not quite sure how it's done.

page:

<apex:pageBlockTable value="{!TopContent}"开发者_如何学运维 var="item">
    <apex:column headerValue="Title">
        <apex:outputLink value="/sfc/#version?selectedDocumentId={!item.Id}">
          {!item.Title}
        </apex:outputLink>
    </apex:column>
</apex:pageBlockTable>

controller:

List<ContentDocument> topContent;
public List<ContentDocument> getTopContent()
{
    if (topContent == null)
    {
        topContent = [select Id,Title from ContentDocument limit 10];
    }
    return topContent;
}


I figured this out. The trick is to use an actionFunction and then call it directly from javascript.

So the VF page looks like this:

<apex:page controller="VfTestController">
    <apex:form>
        <apex:actionFunction action="{!loadDocuments}" name="loadDocuments" rerender="pageBlock" status="myStatus" />
    </apex:form>
    <apex:pageBlock id="pageBlock"> 
        <apex:pageBlockTable value="{!TopContent}" rendered="{!!ISBLANK(TopContent)}" var="item">
            <apex:column headerValue="Title">
                <apex:outputLink value="/sfc/#version?selectedDocumentId={!item.Id}">
                    {!item.Title}
                </apex:outputLink>
            </apex:column>
        </apex:pageBlockTable>
        <apex:actionStatus startText="Loading content..." id="myStatus" />
    </apex:pageBlock>
    <script type="text/javascript">
        window.setTimeout(loadDocuments, 100);
    </script>
</apex:page>

and the controller like this:

public class VfTestController 
{
    List<ContentDocument> topContent;
    public List<ContentDocument> getTopContent()
    {
        return topContent;
    }

    public PageReference loadDocuments()
    {
        if (topContent == null)
        {
            topContent = [select Id,Title from ContentDocument limit 10];
        }
        return null;
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号