grails - 如何使过滤器使用Grails Calendar插件从datePicker中识别值? [英] grails - how to make filter recognize values from datePicker using Grails Calendar Plugin?

查看:157
本文介绍了grails - 如何使过滤器使用Grails Calendar插件从datePicker中识别值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用grails中的常规datePicker,但是我决定使用一个带有日历的textField更容易,我建议使用Grails-UI。



我的问题是,现在,当我点击应用过滤器,它们不工作。不知何故,他们不被认可。



任何帮助将不胜感激。



以下是控制器:

  def searchResults = {
def fromCal
if(params?.lastUpdatedD){
fromCal = Calendar.getInstance()
fromCal.setTime(params?.lastUpdatedD)
fromCal.set(Calendar.HOUR_OF_DAY,0)
fromCal.set(Calendar.MINUTE,0)
fromCal.set(Calendar.SECOND,0)
fromCal.set(Calendar.MILLISECOND,0)
}

def toCal
if(params ?.lastUpdatedH){
toCal = Calendar.getInstance()
toCal.setTime(params?.lastUpdatedH)
toCal.set(Calendar.HOUR_OF_DAY,23)
toCal.set (Calendar.MINUTE,59)
toCal.set(Calendar.SECOND,59)
toCal.set(Calendar.MILLISECOND,999)
}

def entryCriteria = Entry.createCriteria()
def results = entryCriteria.list {
和{if(params?.fechaCambioD&& params?.fechaCambioH){
between(fechaCambio,params .fechaCambioD,par ams.fechaCambioH)
}

if(params?.lastUpdatedD&&& params?.lastUpdatedH){
between(lastUpdated,fromCal.getTime(),toCal.getTime())
}

if(params?.proyectoRutaN){
ilike(proyectoRuta,%$ {params.proyectoRutaN}%)
}
}

}

呈现:'searchResults',model:['results':results,'proyectoRutaN':params?.proyectoRutaN,'fechaCambioD':params?.fechaCambioD,'fechaCambioH':params?.fechaCambioH,'lastUpdatedD':'','lastUpdatedH ':params?.lastUpdatedH])

这里是search.gsp中的一段代码gui:datePicker声明



< gui:datePicker name =fechaCambioHvalue =$ {params?.fechaCambioH}formatString = dd / MMM / yyyy/>



如果我将它们更改为< gui:datePicker name =fechaCambioHvalue =$ {params?.fechaCambioH}precision =daydefault =nonenoSelection =['':'']年=$ {2010..2015}/> ; /> 它工作得很好,但它给了我的3个下拉框为一个月和一年,这个想法是使用只有一个文本字段与其旁边的日历,以帮助它看起来更好。



谢谢提前



更新



这是我在list.gsp中的代码

  def list = {
params.max = Math.min(params.max? params.int('max'):10,100)
[entryInstanceList:Entry.list(params),entryInstanceTotal:Entry.count()]
def today = Calendar.getInstance()
today.setTime(new Date())
today.set(Calendar.HOUR_OF_DAY,0)
today.set(Calendar.MINUTE,0)
today.set(Calendar.SECOND, 0)
today.set(Calendar.MILLISECOND,0)
def results = Entry.findAllByFechaCambioGreaterThanEquals(today.getTime())
render(view:'list',model:['entryInstanceList ':results])

所以我想知道我是否可以做同样的事情,被过滤。因为现在当我点击每一列的标题,被过滤后,它从列表中的每个单个的项目。如果我每当创建新的条目时都会这样做,那么从今天开始(仅仅是你看到的东西),但是当我应用一个过滤器,然后点击它排序列表中的每一个项目,而不仅仅是显示它



我觉得这听起来有点混乱,希望你能帮忙。



提前感谢! / p>

解决方案

将把日期的字符串值传递给控制器​​。您需要将发送到控制器的字符串解析为Date对象。看看Date类的parse()方法( http:// groovy.codehaus.org/groovy-jdk/java/util/Date.html )和SimpleDateFormat类( http://download.oracle.com/docs/cd/E17476_01/javase/1.4.2/docs/api/java /text/SimpleDateFormat.html )。使用SimpleDateFormat中的文档为您的日期创建格式字符串,然后使用Date类将String解析为Date。例如...

  def dateFmt =dd / MMM / yyyy
def fechaCambioDateH
def toCal
if(params?.fechaCambioH){
fechaCambioDateH = Date.parse(dateFmt,params?.fechaCambioH)
toCal = Calendar.getInstance()
toCal.setTime(fechaCambioDateH )
//您可能不需要这些:
toCal.set(Calendar.HOUR_OF_DAY,23)
toCal.set(Calendar.MINUTE,59)
toCal.set Calendar.SECOND,59)
toCal.set(Calendar.MILLISECOND,999)
}

更新:



对不起,不符合评论:



让我解释造成这种行为的原因,你可以自己完成这件事;你似乎开始得到Grails的悬念。当您提交过滤器表单时,您提交所有表单数据 - 日期范围,proyectoRuta等。希望您已经知道这是POST操作,浏览器会将所有数据发送到您的服务器,Grails负责将其打包成一个方便的params对象,您可以访问。但是,当您单击排序链接时,这是一个GET操作。浏览器只是将url发送到你的后端。基本上,它正在达到您的标准搜索,但您检查的所有参数都为空。所以本质上你会得到整个域对象列表。在这一点上,您的工作变得更加复杂,因为您刚才发现自己已经发现自己在一个或那个点:您需要在网络应用程序中管理状态。最简单的方法是将数据存储在用户的会话中。这引入了一些复杂性,更复杂,因为您已经通过几个操作(默认与过滤)将搜索逻辑扩展开来。您可以将搜索结果存储在会话中,并使用比较器进行排序,也可以将搜索条件存储在会话中,并在用户点击排序链接时应用不同的排序/顺序。


I was using regular datePicker from grails but I decided it was easier to use a textField with a calendar next to it and I was recommended to use Grails-UI.

My problem is that now, when I click to apply the filters, they do not work. Somehow they are not being recognized.

Any help would be greatly appreciated.

Here is the code for the controller:

def searchResults = {
  def fromCal
  if(params?.lastUpdatedD) {
fromCal = Calendar.getInstance()
fromCal.setTime(params?.lastUpdatedD)
fromCal.set(Calendar.HOUR_OF_DAY,0)
fromCal.set(Calendar.MINUTE,0)
fromCal.set(Calendar.SECOND,0)
fromCal.set(Calendar.MILLISECOND,0)
}

 def toCal
if(params?.lastUpdatedH) {
toCal = Calendar.getInstance()
toCal.setTime(params?.lastUpdatedH)
toCal.set(Calendar.HOUR_OF_DAY,23)
toCal.set(Calendar.MINUTE,59)
toCal.set(Calendar.SECOND,59)
toCal.set(Calendar.MILLISECOND,999)
}

def entryCriteria = Entry.createCriteria() 
def results = entryCriteria.list {
and{if(params?.fechaCambioD && params?.fechaCambioH) { 
between("fechaCambio", params.fechaCambioD, params.fechaCambioH) 
} 

if(params?.lastUpdatedD && params?.lastUpdatedH) { 
between("lastUpdated", fromCal.getTime(), toCal.getTime()) 
}

if(params?.proyectoRutaN) { 
ilike("proyectoRuta","%${params.proyectoRutaN}%")
}    
 }

 }

render(view:'searchResults', model:['results':results, 'proyectoRutaN':params?.proyectoRutaN, 'fechaCambioD':params?.fechaCambioD, 'fechaCambioH':params?.fechaCambioH, 'lastUpdatedD':'', 'lastUpdatedH':params?.lastUpdatedH]) 

And here is a piece of code in the search.gsp where the gui:datePicker is declared

<gui:datePicker name="fechaCambioH" value="${params?.fechaCambioH}" formatString="dd/MMM/yyyy"/>

If I change them to <<gui:datePicker name="fechaCambioH" value="${params?.fechaCambioH}" precision="day" default="none" noSelection="['':'']" years="${2010..2015}"/>/> it works perfectly fine but it gives me 3 drop down boxes for day month and year, and the idea is to use just one textField with a calendar next to it to help it look nicer.

Thanks in advance

UPDATE

Here is the code I have in the list.gsp

def list = {
    params.max = Math.min(params.max ? params.int('max') : 10, 100)
    [entryInstanceList: Entry.list(params), entryInstanceTotal: Entry.count()]
    def today = Calendar.getInstance()
    today.setTime(new Date())
    today.set(Calendar.HOUR_OF_DAY, 0)
    today.set(Calendar.MINUTE, 0)
    today.set(Calendar.SECOND, 0)
    today.set(Calendar.MILLISECOND, 0)
    def results = Entry.findAllByFechaCambioGreaterThanEquals(today.getTime()) 
    render(view:'list', model:['entryInstanceList':results])

So I wanted to know if I could do the same to sort the things that were filtered. Because now when I click on the title of each column, after being filtered, it orders every single item from the list. If I do it whenever I create new entries, it orders them from today on (only the things that you are seeing), but when I apply a filter and then order it sorts every single item on the list, not just what it being showed.

I think it sounds a little confusing, I hope you can help.

Thanks in advance!

解决方案

The will pass the string value of the date to your controller. You'll need to parse the string that is sent to your controller into a Date object. Look at the parse() method of the Date class (http://groovy.codehaus.org/groovy-jdk/java/util/Date.html) and the SimpleDateFormat class (http://download.oracle.com/docs/cd/E17476_01/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html). Use the documentation in SimpleDateFormat to create a format string for your date and then use the Date class to parse the String into a Date. For instance...

def dateFmt = "dd/MMM/yyyy"
def fechaCambioDateH 
def toCal
if(params?.fechaCambioH) {
   fechaCambioDateH = Date.parse(dateFmt, params?.fechaCambioH)
   toCal = Calendar.getInstance()
   toCal.setTime(fechaCambioDateH)
   // You may not need these anymore:
   toCal.set(Calendar.HOUR_OF_DAY,23)
   toCal.set(Calendar.MINUTE,59)
   toCal.set(Calendar.SECOND,59)
   toCal.set(Calendar.MILLISECOND,999)
}

Update:

Sorry, wouldn't fit in the comment:

Let me explain what's causing that behavior and you may be able to work through this one on your own; you seem like you're starting to get the hang of Grails. When you submit your filter form, you're submitting all your form data - date ranges, proyectoRuta, etc. Hopefully you already know this is a POST operation, the browser takes care of sending all that data to your server, Grails takes care of packaging it up into a convenient params object that you can access. However, when you click on the sort links, that's a GET operation. The browser is just sending the url to your back end. Basically, it's hitting your criteria search, but all the parameters you're checking for are null. So essentially you're getting the entire list of domain objects. At this point, your job gets a lot more complicated because you've just found yourself that we've all found ourselves in at one point or another: you need to manage state in a web app. The easiest way to do that is by storing data in the user's session. This introduces some complexity, more complexity since you've got your search logic spread out over a couple actions (default vs. filtered). You can basically either store the search results in the session and use a Comparator to sort them, or you can store the search criteria in the session and apply a different sort/order when someone clicks on the sort link.

这篇关于grails - 如何使过滤器使用Grails Calendar插件从datePicker中识别值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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