我有一堆数据,我需要一个使用Grails的数据过滤器 [英] I have a bunch of data and I need a data filter using Grails

查看:188
本文介绍了我有一堆数据,我需要一个使用Grails的数据过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的数据制作一个过滤器,这样其他人就可以看到它,可以说我有一大堆事件,就像一个日历一样,我希望看到所有名称或标题都带有足球。
我一直在想如何做到这一点,但没有为我工作,我也尝试使用filterPane插件,但没有工作得很好,我宁愿自己写一些东西,也许上传到Grails插件网页的未来引用,

任何帮助将不胜感激。

解决方案

  def matches = Book.findAllByTitleLike(wonderful)

如果您不想担心大写/小写:

  def matches = Book.findAllByTitleILike(wonderful)


$ b $如果你的书名在一个集合中:

  def books = ['Wonderful Tonight','Atlas Shrugged' ,双城记] 
def matches = books.findAll {it.indexOf(wonderful)> -1} //返回[]
matches = books.findAll {it.indexOf(Wonderful)> -1} //返回['Wonderful Tonight']
matches = books.findAll {it.toLowerCase()。indexOf(wonderful)> -1} //返回['Wonderful Tonight']

更新:



所以现在我们有一个更完整的问题描述,让我们找出一个简化的方法来解决它。



今天,您希望通过活动标题和日期进行搜索,所以我们将从那里开始,然后看看我们如何概括这一点,并制定更完整的解决方案。



我们的一般流程将如下所示:


  1. 将搜索表单显示给用户

  2. 用户输入日期和/或标题以搜索并提交表单

  3. 找到匹配的条目

  4. 显示匹配条目到用户

首先,我们为搜索表单创建一个视图,在grails-app / views / entry /中创建一个文件用下面的内容调用search.gsp:

 < g:form controller ='entry'action ='searchResults'> 
条目标题:< g:textField name =titlevalue =$ {title}/>
分录日期:< g:datePicker name =dayvalue =$ {day}precision =day/>
< g:submitButton name =submitvalue =Search/>
< / g:表格>

接下来,我们将创建控制器。在grails-app / controllers /创建一个新的控制器EntryController.groovy。如果你还没有创建EntryController,你可以通过执行'grails create-controller Entry'来完成。



现在我们创建两个控制器闭包:

  def search = {
render(view:'search')
}

  def searchResults = {
def entryCriteria = Entry.createCriteria()
def results = entryCriteria.list {$ b $ if(params?.title){
ilike {title, $ {params.title}%

if(params?.day){
eq(eventDate,params.day)
}
}
render(view:'searchResults',model:['results':results])
}

最后,让我们在grails-app / views / entry中创建一个searchResults.gsp

 < h1> Results< ; / H1> 
$ {it}
< / g:每个>

所以,现在把它放在一起:


  1. 运行您的应用程序并转至localhost:8080 / $ {appName} / entry / search,然后显示您的搜索表单。 输入标题和/或日期,提交表单,并将您的表单数据发送到您的控制器中的searchResults闭包

  2. 闭包中的条件搜索将查找符合您的搜索条件的所有条目

  3. 您呈现搜索结果视图并将匹配的条目传递给它,然后在gsp上我们遍历每个结果并显示它。

作为免责声明,我在堆栈溢出文本编辑器中编写了这段代码,所以如果有一些我错过的语法问题,我不会感到惊讶。希望这个解释足以让你继续前进。现在让我们来谈谈采取这个基本的例子,并抛出一个更加可敬的解决方案。



我会建议你在得到这个基本的例子之后试着做下面的事情:


  1. 尝试构建一个命令对象来表示您的搜索表单,它将允许您验证用户的输入,并且比直接处理请求参数更容易管理。

  2. 为您的命令对象和控制器流构建一些单元测试;检查错误消息是否正在为您的各种验证填充。
  3. 在searchResults闭包中定义条件搜索,并将其移出到单独的Grails服务中,传递您的命令搜索反对服务来获得你的结果。然后,如果您以后需要使用搜索功能,则可以重新使用搜索功能。

  4. 为您的服务构建一些单元测试;模拟出Entry域对象并验证您的搜索功能是否正常工作。

如果您正努力将上述解决方案工作,尝试进一步简化。消除日期搜索,只是按标题搜索。将searchResults操作中的所有条件逻辑替换为:

  def results = Entry.findByTitleILike(params?.title)

开始简单并改进,您将很快掌握它。



祝您好运!


I am trying to make a filter for my data so other people can see it, lets say I have a bunch of Events, like a calendar, and I want to see all that have a name or title with the word "Soccer". I've been trying to figure out how to do it but nothing works for me, I also tried to use filterPane plugin but did not work quite as well and I prefer to write something myself and maybe upload it to the Grails plugins webpage for future references,

Any help would be greatly appreciated.

解决方案

If it's in a domain class try:

def matches = Book.findAllByTitleLike("wonderful")

of if you don't want to worry about upper/lower case:

def matches = Book.findAllByTitleILike("wonderful")

If your book titles are in a collection:

def books = ['Wonderful Tonight', 'Atlas Shrugged', 'A Tale of Two Cities']
def matches = books.findAll{it.indexOf("wonderful") > -1} // returns []
matches = books.findAll{it.indexOf("Wonderful") > -1} // returns ['Wonderful Tonight']
matches = books.findAll{it.toLowerCase().indexOf("wonderful") > -1} // returns ['Wonderful Tonight']

Update:

So now that we have a more complete description of your problem, let's figure out a simplified way to solve it.

Today, you want to search by event title and its date so we'll start there then look at how we might generalize this and make more complete solution down the road.

Our general flow is going to be something like:

  1. You display a search form to your user
  2. User enters a date and/or title to search for and submits the form
  3. You find the matching entries
  4. You display the matching entries to the user

First, let's create a view for the search form, in grails-app/views/entry/ create a file called search.gsp with the following content:

<g:form controller='entry' action='searchResults'>
   Entry Title: <g:textField name="title" value="${title}" />
   Entry Date: <g:datePicker name="day" value="${day}" precision="day" />
   <g:submitButton name="submit" value="Search" />
</g:form>

Next, we'll create the controller. In grails-app/controllers/ create a new controller EntryController.groovy. You can do this by executing the 'grails create-controller Entry' if you don't already have the EntryController created.

Now, we'll create two controller closures:

def search = {
  render(view:'search')
}

and

def searchResults = {
  def entryCriteria = Entry.createCriteria()
  def results = entryCriteria.list {
    if(params?.title) {
      ilike{"title","%${params.title}%"
    }
    if(params?.day) {
      eq("eventDate", params.day)
    }
  }
  render(view:'searchResults', model:['results':results])
}

Finally, let's create a searchResults.gsp in grails-app/views/entry

<h1>Results</h1>
<g:each in=${results}>
  ${it}
</g:each>

So, now putting it all together:

  1. Run your app and go to localhost:8080/${appName}/entry/search, and that should bring up your search form.
  2. Enter a title and/or date, submit the form and that should send your form data into the searchResults closure in your controller
  3. The criteria search in the closure will find all the Entries that match your search criteria
  4. You render the search results view and pass it your matching entries, then on the gsp we loop through each of the results and display it.

As a disclaimer, I'm writing this code on the fly in the stack overflow text editor, so I won't be surprised if there are a couple syntax issues that I missed. Hopefully the explanation will be enough to get you on your way. Now let's talk about taking this basic example and polishing it up to a more respectable solution.

I would recommend you try to do the following after you get the hang of this basic example:

  1. Try building a command object to represent your search form, it will allow you to validate your user's input and is a more manageable approach than dealing directly with the request parameters like I did above.
  2. Build some unit tests for your command object and the controller flow; check that error messages are being populated for your various validations.
  3. Take the criteria search that we have defined in the searchResults closure, and move it out to a separate Grails Service, pass your command search object to the service to get your results. Then, you can re-use the search functionality if you need it else where in your application later.
  4. Build some unit tests for your service; mock out the Entry domain object and verify that your search functionality is working correctly.

If you're struggling to get the above solution to work, try to simplify it even further. Eliminate the date search and just search by title. Replace all the criteria logic in the searchResults action with:

def results = Entry.findByTitleILike(params?.title)

Start simple and improve and you'll get the hang of it in no time.

Good Luck!

这篇关于我有一堆数据,我需要一个使用Grails的数据过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆