Visualforce页面仅显示标签无记录数据问题排查求助
问题排查与解决方案
核心问题分析
- 控制器未从
StandardSetController加载Patient记录,也未初始化记录索引i和记录集合currentObject,导致标准视图无数据来源。 - 标准视图中直接绑定
{!Patient__c.Name},但集合控制器模式下,该引用无法指向当前选中的单条记录。 selectedValue无默认值,初始视图状态逻辑异常;未处理空记录集合的边界情况,存在空指针风险。
修正后的Apex控制器代码
public with sharing class MyController { public List<Patient__c> currentObject {get; set;} public Integer i {get; set;} public String selectedValue {get; set;} public Boolean myBoolean {get; set;} public MyController(ApexPages.StandardSetController controller) { // 从集合控制器加载所有Patient记录 currentObject = (List<Patient__c>)controller.getRecords(); // 初始化索引为第一条记录,无记录时设为-1避免异常 i = currentObject.isEmpty() ? -1 : 0; // 设置默认视图为标准视图 selectedValue = 'Standared View'; myAction(); } public void myAction() { myBoolean = 'List View'.equals(selectedValue); } public Patient__c getCurrentPatient() { // 处理空集合或索引越界情况,避免空指针 return (i >=0 && i < currentObject.size()) ? currentObject[i] : null; } public void goToPrevious(){ if (i > 0){ i--; } } public void goToNext() { if (i < currentObject.size() - 1) { i++; } } }
修正后的Visualforce页面代码
<apex:page standardController="Patient__c" extensions="MyController" recordSetVar="Patients"> <apex:pageBlock title="Patient Records...!!!"> <apex:form > <div align='center' style='color:blue;font-size:10px'> Your selection is {!selectedValue} </div> <apex:selectList label="View Type" value="{!selectedValue}" size="1"> <apex:selectOption itemvalue="Standared View" itemLabel="Standared View"/> <apex:selectOption itemvalue="List View" itemLabel="List View"/> <apex:actionSupport event="onchange" action="{!myAction}" rerender="viewContainer"/> </apex:selectList> </apex:form> <apex:outputPanel id="viewContainer"> <!-- 列表视图 --> <apex:pageBlock rendered="{!myBoolean}"> <apex:pageBlockTable value="{!Patients}" var="Pa" > <apex:column value="{!Pa.Name}"/> <apex:column value="{!Pa.Contact_Number__c}"/> <apex:column value="{!Pa.Email__c}"/> <apex:column value="{!Pa.Age__c}"/> </apex:pageBlockTable> </apex:pageBlock> <!-- 标准视图(单记录双列展示) --> <apex:pageBlock title="Standared View" rendered="{!!myBoolean}"> <apex:pageBlockSection columns="2" rendered="{!currentPatient != null}"> <apex:outputField value="{!currentPatient.Name}"/> <apex:outputField value="{!currentPatient.Age__c}"/> <apex:outputField value="{!currentPatient.Email__c}"/> </apex:pageBlockSection> <!-- 无记录提示 --> <apex:outputText rendered="{!currentPatient == null}" style="color:red;"> 暂无Patient记录 </apex:outputText> <apex:form > <apex:commandButton value="Previous" action="{!goToPrevious}" rerender="viewContainer" disabled="{!i > 0}"/> <apex:commandButton value="Next" action="{!goToNext}" rerender="viewContainer" disabled="{!i < currentObject.size() - 1}"/> </apex:form> </apex:pageBlock> </apex:outputPanel> </apex:pageBlock> </apex:page>
关键改动说明
- 控制器构造函数中从
StandardSetController加载记录集合,初始化索引并设置默认视图值。 - 新增
getCurrentPatient()方法返回当前索引对应的记录,处理空集合边界情况。 - Visualforce中标准视图绑定
{!currentPatient}字段,替换原无效的{!Patient__c}引用。 - 为视图切换和记录切换按钮添加
rerender="viewContainer",确保页面实时刷新。 - 为上/下按钮添加禁用逻辑,避免无效操作;新增无记录提示。
内容的提问来源于stack exchange,提问作者Siddhesh Paratane
相关产品推荐
相关产品推荐

