2010-10-06 122 views
3

有人能告诉我如何使用ajax在页面加载时通过ajax加载apex pageBlockTable?我看过一些例子展示了如何使用apex actionFunction,但这些示例通常很简单(例如 - 从控制器返回一个字符串并将其放在页面上。我的控制器返回一个sObject列表,我不太确定它是如何做Visualforce在页面加载时通过ajax加载apex组件

页:

<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> 

控制器:。

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

回答

1

我想通了这一点诀窍是使用actionFunction,然后直接从JavaScript调用它

所以VF页看起来是这样的:

<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> 

和控制器这样的:

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; 
    } 
} 
相关问题