【问题标题】:Customizing search feature in Play's CRUD module在 Play 的 CRUD 模块中自定义搜索功能
【发布时间】:2011-06-10 20:52:56
【问题描述】:

编辑:搜索似乎以非常基本的方式工作,搜索在相关模型的所有字符串成员字段中输入的任何文本,没有特殊的搜索运算符。

如果有任何方法可以自定义搜索的工作方式,我想知道。现在看起来我只需要忽略内置的搜索内容并自己动手。


自从添加了 notes 字段后,它开始什么都不返回,而不是返回所有内容。 到目前为止唯一有效的方法是在搜索框中输入“test”,这将返回备注字段中带有“test”的记录。搜索“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>

【问题讨论】:

  • 知道如何在任何成员字段中搜索文本吗?而不仅仅是字符串。现在有点卡在这个问题上。

标签: search playframework crud


【解决方案1】:

Here 你可以看到如何自定义 CRUD 页面,包括如何在屏幕中显示对象的某些字段。

搜索字符串应采用以下格式:

 <field name>:<value>

例如:

name:Liam

该搜索将过滤名称中包含 Liam 的所有对象。字段名称必须是列表页面中显示的字段。我不确定它是否适用于 @Lob,但通常这不是您希望在列表页面中显示的字段,因此应该不是问题。

我不得不说,在 Play 1.1 中,我遇到了一些列的问题,其中启动搜索不起作用(它引发了错误)并且我无法解决它。它似乎在 1.2.1 中没有发生(我不知道这是修复还是只是我没有注意到的更改)

编辑:

在更新的问题上,列表页面似乎是正确的。

一个大胆的想法:您是否检查过数据库中的列是否正确?我记得当我更改了一些模型和一些未正确更新的列时,我遇到了一些 Hibernate 不公平的问题,从而导致了奇怪的行为。完全删除表格并让 Play 重新创建它可能是值得的。

如果这不能解决问题,这很可能是 CRUD 控制器中的 Play 错误,您需要找到源代码。

我首先担心的是 Interval 上缺少 rel 注释,以及 LocalTime 和枚举状态的使用。没关系,但是……恐怕我只能建议您进行增量测试以对问题进行本地化:

  • 删除除代理和注释之外的所有内容,然后尝试搜索。
  • 如果失败,请在 Play 的 Lighthouse 上提出错误,因为它不应该这样做
  • 如果有效,请继续一次添加一个字段并重新测试搜索。 Play 会很快,您可能会检测到导致问题的注释/字段并在 Play 的Lighthouse 上报告

【讨论】:

  • 到目前为止它仍在返回所有内容
  • @bemace 那么我们需要您编辑问题,添加模型类和 CRUD 页面的代码(如果您修改它)。以及您正在使用的示例查询和返回的数据
【解决方案2】:

您可以使用弹性搜索模块。

1) 定义控制器:

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

}

2) 用@ElasticSearchable注解定义标准JPA模型:

@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找到更多信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-17
    • 2012-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多