自定义 Play 的 CRUD 模块中的搜索功能

发布于 2024-11-15 01:48:57 字数 2931 浏览 5 评论 0 原文

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

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


自从添加 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;
}

修改增删改查列表页面:

#{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>

Edit: searching appears to work in a very basic manner, searching for whatever text is entered in all the String member fields of the relevant model, with no special search operators.

If there's any way to customize how the search works, I'd like to know. Right now it's looking like I'll just have to ignore the built-in search stuff and roll my own.


Since adding the notes field, it's started returning nothing instead of everything.
The only thing that was worked so far is entering "test" in the search box, which returns the record with "test" in the notes field. Searching for "notes:test" or "notes=test" doesn't work, nor does any attempt to search the agent or status fields. I've tried things like "1400" which is a value I've verified is present in the agent field via phpMyAdmin, and things like "0" or "VALID" for the status field, which is what all the records have.

model fields:

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;
}

modified crud list page:

#{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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

汐鸠 2024-11-22 01:48:57

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

搜索字符串的格式应为:

 <field name>:<value>

例如:

name:Liam

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

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

编辑:

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

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

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

我首先担心的是 Interval 上缺少 rel 注释,以及 LocalTime 和枚举 Status 的使用。应该没关系,但是......恐怕我只能建议您进行增量测试来定位问题:

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

Here you can see how to customize the CRUD pages, including how to show certain fields of the object in the screen.

The search strings are supposed to be in the format:

 <field name>:<value>

For example:

name:Liam

That search would filter all objects whose name contains Liam. The field name must be a field displayed in the list page. I'm not sure if it works with @Lob, but usually that's not a field you want to display in the list page so it shouldn't be an issue.

I have to say that in Play 1.1 I had some issues with some columns, in which starting the search didn't work (it raised an error) and I couldn't solve it. It doesn't seem to happen in 1.2.1 (I have no idea if it is a fix or simply a change I did without noticing)

EDIT:

On the updated question, the list page seems right.

One bold idea: have you checked that the database has the proper columns? I remember I had some issues with Hibernate not playing fair when I changed some models and a few columns where not updated properly, causing odd behavior. It may be worth it to remove the table completely and let Play recreate it.

If that doesn't solve it most likely this is a Play bug in the CRUD controller, you would need to find the source.

My first concern would be the lack on a rel annotation on Interval, and the usage of LocalTime and the enum Status. It should not matter, but... I'm afraid I can only suggest you to do an incremental test to locale the issue:

  • remove everything except agent and notes, and try the search.
  • If it fails, raise a bug on Play's Lighthouse as it shouldn't
  • If it works, keep adding fields one at a time and retesting the search. This will be fast with Play, and you may detect the annotation/field causing the issue and report on Play's Lighthouse
半山落雨半山空 2024-11-22 01:48:57

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

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

You can use the elastic search module.

1) Define Controller:

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

}

2) Define standard JPA model with @ElasticSearchable annotation:

@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 + "]";
    }

}

You can find more information at http://geeks.aretotally.in/play-framework-module-elastic-search-distributed-searching-with-json-http-rest-or-java.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文