自定义的controller public with sharing class MerchandiseController { public List<Merchandise__c> merchandises{get;set;} public List<SelectOption> merchandisePrices = new List<SelectOption>(); public Merchandise__c merchandise{get;set;} public Boolean isStatus{get;set;} public String name{get;set;} public Decimal quantity{get;set;} public Decimal price{get;set;} public MerchandiseController(){ merchandises = new List<Merchandise__c>(); refreshData(); } public List<SelectOption> getPrice(){ merchandisePrices.clear(); merchandisePrices.add(new SelectOption('200','200')); return merchandisePrices; } public void refreshData(){ isStatus = true; String goodsQueryString = 'SELECT Name,Quantity__c,Price__c from Merchandise__c'; merchandises = Database.query(goodsQueryString); } public void save(){ upsert merchandises; } public void deleteMerchandises(){ Id id = ApexPages.currentPage().getParameters().get('merchandisesId'); Database.delete(id); refreshData(); } public void add(){ if(merchandises == null){ merchandises = new List<Merchandise__c>(); } Merchandise__c merchandise2 = new Merchandise__c(); System.debug('-----------merchandiseList---------------------'+merchandises); if(merchandises.size()==0){ merchandises.add(merchandise2); }else{ merchandises.add(0,merchandise2); } } public void query(){ String goodsQueryString = 'SELECT Name,CreatedBy,LastModifiedBy,Owner,Quantity__c,Price__c from Merchandise__c'; merchandises = Database.query(goodsQueryString); } }
visual force 的 page页面
<apex:page controller="MerchandiseController" showHeader="false">
<apex:messages />
<apex:form >
<apex:pageBlock title="Merchandises">
<apex:pageBlockSection title="query merchandises">
<apex:inputText value="{!name}" tabindex="4" label="name" id="name"></apex:inputText>
<apex:selectList multiselect="false" size="1" value="{!price}" label="Price">
<apex:selectOptions value="{!price}"></apex:selectOptions>
</apex:selectList>
<apex:inputText value="{!quantity}" tabindex="3" label="Quantity" />
</apex:pageBlockSection>
<apex:pageBlockTable value="{!Merchandises}" var="merchan" id="merchandisResult">
<apex:column headervalue="name">
<apex:inputField value="{!merchan.Name}"/>
</apex:column>
<apex:column headervalue="quantity">
<apex:inputField value="{!merchan.Quantity__c}" />
</apex:column>
<apex:column headervalue="price">
<apex:inputField value="{!merchan.Price__c}" />
</apex:column>
<apex:column headervalue="delete?">
<apex:commandLink value="delete" action="{!deleteMerchandises}">
<apex:param name="merchandisesId" value="{!merchan.Id}"></apex:param>
</apex:commandLink>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
最后的结果如下图所示