LoginSignup
2
0

More than 3 years have passed since last update.

【Salesforce】ある桁数まで入力したら自動で検索してほしい

Posted at

動作確認

未入力の状態

何も入力してない.jpg

4桁入れて自動検索した状態

スクリーンショット 2021-01-17 101243.jpg

実現方法

test.cls
public String name { get; set; }

public void getAccount(){
    this.targetAccList = [
        SELECT
            Id
            ,Name
        FROM Account
        WHERE
            Name = :name
    ];
}
test.js
function checkName(input) {
    // apex:actionFunctionタグで設定しているgetAccountメソッドを呼び出す
    if(input.value.length == 4){
        getAccount();
    }
}
test.page
<apex:form>
    <apex:actionFunction name="getAccount" action="{!getAccount}"/>
    <apex:pageBlock title="取引先検索">
        <apex:outputLabel value="取引先名" for="name"/>
        <!-- onkeyup属性を使用し、入力したらJavaScriptのcheckNameメソッドを呼び出します -->
        <apex:inputText id="name" value="{!name}" html-placeholder="取引先名" html-autofocus="true" onkeyup="checkName(this);"/>
    </apex:pageBlock>
    <apex:pageBlock>
        <table>
            <thead>
                <tr>
                    <th>取引先名</th>
                </tr>
            </thead>
            <apex:repeat value="{!targetAccList}" var="acc">
                <tbody>
                    <tr>
                        <td>
                            <apex:outputField value="{!acc.Name}"/>
                        </td>
                    </tr>
                </tbody>
            </apex:repeat>
        </table>
    </apex:pageBlock>
</apex:form>

参考リンク

apex:inputText

apex:actionFunction

2
0
0

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