検索項目を [Visualforce Article Search (Visualforce 記事検索)] ページに追加する
記事リストの上にキーワード検索項目を作成します。この項目にテキストを入力すると、リストが更新されて、そのキーワードを含む記事のみが表示されます。
検索項目を使用する場合、検索文字列を設定および取得する新しいメソッドが必要になります。そのため、[Visualforce Article Search (Visualforce 記事検索)] ページのコードを変更する前に新しいコントローラを追加します。
- [設定] で、 をクリックします。
- [新規] をクリックします。
- Visualforce キーワード検索コントローラのコードサンプルの内容をコピーして貼り付けます。
- [保存] をクリックします。
- [Visualforce Article Search (Visualforce 記事検索)] タブのフッターで、[ArticleList] をクリックして Visualforce 開発モードのページエディタを表示します。
- Visualforce 開発モードのページエディタで既存のマークアップをすべて削除し、「Visualforce キーワード検索のコードサンプル」に表示されているマークアップに置き換えます。
Visualforce キーワード検索のコードサンプル
太字の行については、コードサンプルの後に説明があります。
1swfobject.registerObject("clippy.codeblock-0", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17<apex:page sidebar="false" title="Article List" controller="vfKeywordSearchController">
18 <style>
19 td{
20 vertical-align : top;
21 text-align: left;
22 }
23 </style>
24 <apex:form >
25 <apex:pageBlock title="Search" >
26 <apex:inputText value="{!searchstring}" id="theSearchstring" maxlength="100" size="110"/>
27 <apex:commandButton value="Go" id="submitButton" style="width:30" reRender="theSearchResults" />
28 </apex:pageBlock>
29 <apex:messages />
30 <apex:pageBlock title="Article List" >
31
32 <apex:panelGroup id="theSearchResults" >
33 <apex:panelGrid width="100%">
34 <table width="99%">
35 <tr>
36 <th width="33%">Title</th>
37 <th width="33%">Article Type</th>
38 <th width="33%">Summary</th>
39 </tr>
40 </table>
41 <knowledge:articleList articleVar="article" pageNumber="{!currentPageNumber}" Keyword="{!searchstring}" hasMoreVar="false" pageSize="10">
42 <table width="99%">
43 <tr>
44 <td width="33%">
45 <apex:outputLink target="_blank" value="{!URLFOR($Action.KnowledgeArticle.View, article.id,['popup' = 'true'])}">{!article.title}</apex:outputLink>
46 </td>
47 <td width="33%"><apex:outputText >{!article.articleTypeLabel}</apex:outputText></td>
48 <td width="33%"><apex:outputText >{!article.abstract}</apex:outputText></td>
49 </tr>
50 </table>
51 </knowledge:articleList>
52 </apex:panelGrid>
53 <apex:panelGrid columns="2">
54 <apex:commandLink action="{!previous}" value="Previous" style="{!IF(prevRequired = true,'display:block','display:none')}" reRender="theSearchResults"/>
55 <apex:commandLink action="{!next}" value="Next" style="{!IF(nextRequired = true,'display:block','display:none')}" reRender="theSearchResults"/>
56 </apex:panelGrid>
57 </apex:panelGroup>
58 </apex:pageBlock>
59 </apex:form>
60</apex:page>次のコードフラグメントでは、Keyword 属性を使用します。この属性の値で、vfKeywordSearchController コントローラの searchstring メソッドを呼び出します。
1<knowledge:articleList articleVar="article" pageNumber="{!currentPageNumber}" Keyword="{!searchstring}" hasMoreVar="false" pageSize="10">Visualforce キーワード検索コントローラのコードサンプル
太字の行については、コードサンプルの後に説明があります。
1swfobject.registerObject("clippy.codeblock-2", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17public with sharing class vfKeywordSearchController {
18
19 //Page Size
20 private Static Final Integer PAGE_NUMBER = 10;
21
22 //Search String used in ArticleList tag
23 public String searchstring { get; set; }
24
25 public vfKeywordSearchController() {
26 String qryString = 'SELECT Id, title, UrlName, LastPublishedDate,LastModifiedById FROM KnowledgeArticleVersion WHERE (PublishStatus = \'online\' and Language = \'en_US\')';
27 List<KnowledgeArticleVersion> articleList= Database.query(qryString);
28 maxSize = articleList.size() ;
29 }
30
31 //Keeps track of current page & max size of article list
32 Integer currentPage = 1;
33 Integer maxSize = 1;
34
35 // Returns whether we need to see previous button or not
36 public boolean getPrevRequired() {
37 return currentPage > 1;
38 }
39
40 // Returns whether we need to see next button or not
41 public boolean getNextRequired() {
42 return currentPage * PAGE_NUMBER < maxSize;
43 }
44
45 //Returns current page number
46 public Decimal getCurrentPageNumber() {
47 return this.currentPage;
48 }
49
50 //action for next click
51 public PageReference next() {
52 if(maxSize > this.currentPage * PAGE_NUMBER) {
53 this.currentPage = this.currentPage + 1;
54 }
55 return null;
56 }
57
58 //action for previous click
59 public PageReference previous() {
60 if(this.currentPage > 1)
61 this.currentPage = this.currentPage - 1;
62 return null;
63 }
64
65
66}次のコードフラグメントで、検索キーワードが設定および取得されます。
1public String searchstring { get; set; }これまでの作業を確認する
[Visualforce Article Search (Visualforce 記事検索)] ページの記事リストの上に検索項目が表示されます。