从自定义对象添加新记录并在 visualforce 页面上显示它们

Adding new records and displaying them on visualforce page from custom object

我现在在 visualforce 页面中收到错误消息。说:

Error:Unknown property 'tudent__cStandardController.d'

当我将删除 commandLink 按钮添加到 vf 页面时,会发生这种情况。在我添加它之前它没有发出错误但是它没有在页面上显示记录。

提前感谢您的帮助

Visualforce 代码:

    <apex:page standardController="tudent__c" extensions="vidsav">
    <apex:form >
       <apex:outputPanel id="check">
        <apex:pageBlock title="Dodaj Študenta">
          <apex:pageBlockSection columns="1">
            <apex:inputField value="{! tudent__c.Name }"/>
            <apex:inputField value="{! tudent__c.priimek__c }"/>
            <apex:inputField value="{! tudent__c.Datum_rojstva__c }"/>
            <apex:inputField value="{! tudent__c.letnik__c }"/>
            <apex:inputField value="{! tudent__c.Naslov__c }"/>
            <apex:inputField value="{! tudent__c.naziv_fakultete__c }"/>
            <apex:inputField value="{! tudent__c.tudijski_program__c }"/>
            <apex:inputField value="{! tudent__c.tip_tudija__c }">
            <apex:actionSupport event="onchange" rerender="check" />
            </apex:inputField>
            <apex:inputField value="{! tudent__c.Samopla_nik__c }" rendered="{!IF( tudent__c.tip_tudija__c == 'izredni', true, false )}" />
          </apex:pageBlockSection>
            <apex:pageBlockButtons >
                <apex:commandButton action="{!save}" value="Save" />
                <apex:commandButton value="Cancel" action="{!cancel}"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
       </apex:outputPanel>    
    <apex:pageBlock title="Študenti">
      <apex:pageBlockTable value="{!studentsR}" var="s" >
       <apex:commandLink action="{!deleteStudent}" onclick="if(!confirm('Are you sure?')) return false;">`enter code here`Del
    <apex:param value="{!d.Id}" name="idToDel" assignTo="{!SelectedStudentId}"/>
</apex:commandLink>
         <apex:column value="{!s.Name}"/>
         <apex:column value="{!s.priimek__c}"/>
         <apex:column value="{!s.Datum_rojstva__c}"/>
         <apex:column value="{!s.letnik__c}"/>
         <apex:column value="{!s.Naslov__c}"/>
         <apex:column value="{!s.naziv_fakultete__c}"/>
         <apex:column value="{!s.tudijski_program__c}"/>
         <apex:column value="{!s.tip_tudija__c}"/>
         <apex:column value="{!s.Samopla_nik__c}"/>
      </apex:pageBlockTable>
    </apex:pageBlock>
    </apex:form>
</apex:page>

控制器:

public class vidsav {

public vidsav(ApexPages.StandardController controller) {

}

public List<tudent__c> studentsR {get;set;}
public String SelectedStudentId {get;set;}

public vidsav() {
    loadData();

}         

public void loadData() {

    studentsR = [Select id,Name,priimek__c,Datum_rojstva__c,letnik__c,Naslov__c,naziv_fakultete__c,tudijski_program__c,tip_tudija__c,Samopla_nik__c, CreatedDate from tudent__c Order By CreatedDate desc];

}

public void deleteStudent(){

   studentsR = [Select id,Name,priimek__c,Datum_rojstva__c,letnik__c,Naslov__c,naziv_fakultete__c,tudijski_program__c,tip_tudija__c,Samopla_nik__c, CreatedDate from tudent__c where id = :SelectedStudentId];

  if(studentsR.size() > 0 || studentsR[0].Id != ''){

  delete studentsR;

  }

 loadData();

   }

}

这是您应该使用的更新代码 ->

1) 将您的 PageBlockTable 更新为此 -

<apex:pageBlockTable value="{!studentsR}" var="s" >
     <apex:column value="{!s.Name}"/>
     <apex:column value="{!s.priimek__c}"/>
     <apex:column value="{!s.Datum_rojstva__c}"/>
     <apex:column value="{!s.letnik__c}"/>
     <apex:column value="{!s.Naslov__c}"/>
     <apex:column value="{!s.naziv_fakultete__c}"/>
     <apex:column value="{!s.tudijski_program__c}"/>
     <apex:column value="{!s.tip_tudija__c}"/>
     <apex:column value="{!s.Samopla_nik__c}"/>
     <apex:column>
         <apex:commandLink action="{!deleteStudent}" onclick="if(!confirm('Are you sure?')) return false;" value="Del">
             <apex:param value="{!s.Id}" name="idToDel" assignTo="{!SelectedStudentId}"/>
         </apex:commandLink>
     </apex:column>

您需要将 commandLink 放在 apex:column 中,以便它在 pageBlockTable 中正确显示,当然 d 也必须被替换使用 s 因为您的 table 变量是 s (var="s").

2) 将您的控制器更新为此 -

public class vidsav {

public vidsav(ApexPages.StandardController controller) {
    loadData();
}

public List<Account> studentsR {get;set;}
public String SelectedStudentId {get;set;}      

public void loadData() {

    studentsR = [Select id,Name,priimek__c,Datum_rojstva__c,letnik__c,Naslov__c,naziv_fakultete__c,tudijski_program__c,tip_tudija__c,Samopla_nik__c, CreatedDate from tudent__c Order By CreatedDate desc];

}

public void deleteStudent(){

   studentsR = [Select id,Name,priimek__c,Datum_rojstva__c,letnik__c,Naslov__c,naziv_fakultete__c,tudijski_program__c,tip_tudija__c,Samopla_nik__c, CreatedDate from tudent__c where id = :SelectedStudentId];

    if(studentsR.size() > 0 || studentsR[0].Id != ''){

      delete studentsR;

    }
    loadData();

  }

}

您使用了两个构造函数并且从错误的构造函数调用了 loadData(),这就是它没有加载数据的原因。有了这个,您将能够删除记录并重新加载您的 table.

将 {!d.Id} 替换为 {!s.Id} 用于遍历pageblocktable中记录的变量是s不是d。