5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Visualforce 検索一覧画面実装(View State 確認用)

Posted at

#1.画面イメージ
image.png

#2.Apex Controller 実装

ContactListController.cls
public with sharing class ContactListController {
    // 【ViewState確認用】
    private String v1 = '11';
        
    // 【ViewState確認用:transient】
    transient public String v2 = '22';
    
    // 【ViewState確認用:Static】
    public Static String v3 = '33';
    
    // 検索条件
    public Contact condition{ get;set; }
    
    // 検索結果リスト
    public List<Contact> results { get;set; }
    
    /**
     * コンストラクタ
     */
    public ContactListController() {
        this.condition = new Contact();
        this.results = new List<Contact>();
    }

    /**
     * クリアボタン処理
     */
    public PageReference clear(){
        this.condition = new Contact();
        this.results = new List<Contact>();
        return null;
    }

    /**
     * 検索ボタン処理
     */
    public PageReference search() {
        try {
            system.debug('condition:' + condition);
        
            // 入力チェック
            If (String.IsBlank(condition.EMail) && String.IsBlank(condition.AccountId)) {
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'メールまたは取引先名のいずれかを入力してください。'));
                return null;
            }

            // 検索条件からSOQLを作成
            String strSoqlQuery = 'select Name, AccountId, Phone, Email from Contact where Id != null ';

            // メール
            String strEMail;
            If (!String.IsBlank(condition.EMail)) {
                strEMail = '%' + condition.EMail + '%';
                strSoqlQuery = strSoqlQuery + ' and EMail like :strEMail';
            }

            // 取引先名
            String strAccountId;
            If (!String.IsBlank(condition.AccountId)) {
                strAccountId = condition.AccountId;
                strSoqlQuery = strSoqlQuery + ' and AccountId =:strAccountId';
            }

            // 対象データを検索する
            system.debug('strSoqlQuery:'+ strSoqlQuery);
            this.results = database.query(strSoqlQuery);
        } catch (Exception e){
             ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, e.getMessage()));
        } 
        return null;
    }
}

#3.Visualforce Page 実装

ContactListPage.page
<apex:page controller="ContactListController">
    <apex:form id="theForm">
        <apex:pageBlock title="取引先責任者検索画面">
            <apex:pageMessages id="messagearea" showDetail="false" />
            <apex:pageblockSection id="conditionSection" title="検索" columns="2">
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="メール" />
                    <apex:inputField value="{!condition.EMail}" />
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="取引先名" />
                    <apex:inputField value="{!condition.AccountId}" />
                </apex:pageBlockSectionItem>
            </apex:pageblockSection>
            <apex:pageBlockButtons id="buttonSection" location="bottom">
                <apex:commandButton value="検索" action="{!search}" style="font-size:12pt;width:100px;height:30px;" reRender="searchresult,messagearea" />
                <apex:commandButton value="クリア" action="{!clear}" style="font-size:12pt;width:100px;height:30px;" reRender="conditionSection,searchresult,messagearea" />
            </apex:pageBlockButtons>
        </apex:pageBlock>
        <apex:outputpanel id="searchresult">
            <apex:pageBlock title="検索結果:" rendered="{!(results.size == 0)}">検索条件に該当するデータがありません</apex:pageBlock>
            <apex:pageBlock id="resultBlock" title="取引先責任者一覧"
                rendered="{!(results.size > 0)}">
                <apex:outputtext style="width:110px" value="結果 : {!results.size}件" />
                <apex:pageblockTable id="resultTable" value="{!results}" var="item">
                    <apex:column >
                        <apex:facet name="header">取引先責任者</apex:facet>
                        <apex:outputLink value="{!URLFOR('/' + item.Id)}">
                            <apex:outputField value="{!item.Name}" />
                        </apex:outputlink>
                    </apex:column>
                    <apex:column >
                        <apex:facet name="header">取引先名</apex:facet>
                        <apex:outputField value="{!item.AccountId}" />
                    </apex:column>
                    <apex:column >
                        <apex:facet name="header">携帯</apex:facet>
                        <apex:outputField value="{!item.Phone}" />
                    </apex:column>
                    <apex:column >
                        <apex:facet name="header">メール</apex:facet>
                        <apex:outputField value="{!item.Email}" />
                    </apex:column>
                </apex:pageblockTable>
            </apex:pageBlock>
        </apex:outputpanel>
    </apex:form>
</apex:page>

5
2
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?