2011-06-10 64 views
3

編輯:搜索似乎以非常基本的方式工作,在相關模型的所有字符串成員字段中搜索輸入的任何文本,而無需特殊的搜索運算符。在Play的CRUD模塊中自定義搜索功能

如果有什麼方法來定製搜索的工作方式,我想知道。現在看起來我只是不得不忽略內置搜索的東西,並推出自己的。


由於添加了notes字段,它開始不返回任何內容,而是返回任何內容。 到目前爲止,唯一的工作是在搜索框中輸入「test」,該搜索框在notes欄中返回帶有「test」的記錄。搜索「筆記:測試」或「筆記=測試」不起作用,也不會嘗試搜索agentstatus字段。我已經嘗試了諸如「1400」之類的東西,這是我通過phpMyAdmin在代理字段中驗證的值,以及狀態字段中的「0」或「VALID」之類的東西,這就是所有記錄的內容。

示範田:

public class AgentShift extends Model { 
    public enum Status { VALID, RESCHEDULED, EXTENDED, DELETED } // REDUCED? 

    @Required @Min(1000) @Max(99999) 
    public int agent; 
    public Status status = Status.VALID; 

    @Required 
    @Columns(columns={@Column(name="start_time"),@Column(name="end_time")}) 
    @Type(type="org.joda.time.contrib.hibernate.PersistentInterval") 
    public Interval scheduled; 

    @Type(type="org.joda.time.contrib.hibernate.PersistentLocalTimeAsTime") 
    public LocalTime agent_in; 
    @Type(type="org.joda.time.contrib.hibernate.PersistentLocalTimeAsTime") 
    public LocalTime agent_out; 

    public String notes; 
} 

修改CRUD列表頁:

#{extends 'main.html' /} 
#{set title: 'Agent Shifts' /} 

<div id="crudList" class="${type.name}"> 

    <h2 id="crudListTitle">&{'crud.list.title', type.name}</h2> 

    <div id="crudListSearch"> 
     #{crud.search /} 
    </div> 

    <div id="crudListTable"> 
     #{crud.table fields:['id','agent','scheduled','status','notes'] } 
      #{crud.custom 'scheduled'} 
       #{if object.scheduled.getStart().toDateMidnight().equals(object.scheduled.getEnd().toDateMidnight())} 
        ${org.joda.time.format.DateTimeFormat.forStyle("SS").printTo(out, object.scheduled.getStart())} to 
        ${org.joda.time.format.DateTimeFormat.forStyle("-S").printTo(out, object.scheduled.getEnd())} 
       #{/if} 
       #{else} 
        ${org.joda.time.format.DateTimeFormat.forStyle("SS").printTo(out, object.scheduled.getStart())} to 
        ${org.joda.time.format.DateTimeFormat.forStyle("SS").printTo(out, object.scheduled.getEnd())} 
       #{/else} 
      #{/crud.custom} 
      *{ 
      #{crud.custom 'entered_by'} 
       #{if object.entered_by} 
        ${object.entered_by} 
       #{/if} 
       #{else} ? 
       #{/else} 
      #{/crud.custom} 
      #{crud.custom 'created'} 
       ${org.joda.time.format.DateTimeFormat.forStyle("SS").printTo(out, object.created)} 
      #{/crud.custom} 
       }* 
     #{/crud.table} 
    </div> 

    <div id="crudListPagination"> 
     #{crud.pagination /} 
    </div> 

    <p id="crudListAdd"> 
     <a href="@{blank()}">&{'crud.add', type.modelName}</a> 
    </p> 

</div> 
+0

任何想法如何搜索任何成員字段中的文本?而不僅僅是絃樂。有點卡在那個問題現在。 – sanz 2013-03-14 05:59:13

回答

2

Here你可以看到如何自定義CRUD頁面,包括如何顯示在屏幕上的物體的某些字段。

搜索字符串應該是在格式:

<field name>:<value> 

例如:

name:Liam 

,搜索過濾器將其名稱包含利亞姆所有對象。字段名稱必須是列表頁面中顯示的字段。我不確定它是否適用於@Lob,但通常這不是您想要在列表頁面中顯示的字段,因此它不應該是一個問題。

我不得不說,在Play 1.1中,我對某些列有些問題,其中開始搜索沒有工作(它引發了一個錯誤),我無法解決它。它似乎並沒有在1.2.1發生(我不知道,如果它是一個修復或只是一個改變我也沒有注意到)

編輯:

更新的問題,在列表頁面似乎是正確的。

一個大膽的想法:你是否檢查過數據庫是否有適當的列?我記得我在使用Hibernate時遇到了一些問題,在我更改了一些模型和一些未正確更新的列時會造成奇怪的行爲。完全刪除表並讓Play重新創建它可能是值得的。

如果這並不能解決問題,那麼很可能這是CRUD控制器中的播放錯誤,您需要查找源代碼。

我首先關心的是Interval上缺少rel註釋,以及LocalTime和枚舉狀態的使用。這應該不重要,但是...恐怕我只能建議你做一個增量測試,以區域設置問題:

  • 刪除代理和筆記除外,並嘗試搜索。
  • 如果失敗,請提高Play的Lighthouse上的錯誤,因爲它不應該如此
  • 如果有效,請不斷添加字段並重新測試搜索。這將是快一起玩,你可以檢測註釋/場造成的問題,播放的Lighthouse
+0

到目前爲止它仍然返回所有內容 – 2011-06-16 17:47:10

+0

@bemace然後我們需要你編輯添加模型類和CRUD頁面代碼的問題,如果你修改它的話。以及您正在使用的示例查詢和返回的數據 – 2011-06-17 10:40:29

2

您可以使用彈性搜索模塊上的報告。

1)定義控制器:

@ElasticSearchController.For(ElasticSearchSampleModel.class) 
public class ElasticSearchExample extends ElasticSearchController { 

} 

2)定義標準JPA模型@ElasticSearchable註釋:

@Entity 
@ElasticSearchable 
public class ElasticSearchSampleModel extends Model { 

    /** The Constant serialVersionUID. */ 
    private static final long serialVersionUID = 1L; 

    /** The field1. */ 
    public String field1; 

    /** The field2. */ 
    public String field2; 

    /** 
    * To String 
    * 
    * @see play.db.jpa.JPABase#toString() 
    */ 
    @Override 
    public String toString() { 
     return "ElasticSearchSampleModel [field1=" + this.field1 + ", field2=" + this.field2 + "]"; 
    } 

} 

您可以在http://geeks.aretotally.in/play-framework-module-elastic-search-distributed-searching-with-json-http-rest-or-java找到更多的信息。