TRABALHANDO COM DATA EM CAMPO DO TIPO INPUT
15/09/2014 14:54
2
SOU INICIANTE EM GRAILS/GROOVY E TIVE DIFICULDADES EM TROCAR O CAMPO <g:datePicker... por um <g:textField... depois de algumas pesquisas encontrei esta solução:

1 - acrescentar no arquivo "grails-app\conf\Config.groovy" a seguinte linha:
grails.databinding.dateFormats = [ 'MMddyyyy', 'yyyy-MM-dd HH:mm:ss.S', "yyyy-MM-dd'T'hh:mm:ss'Z'"] 


2 - dentro da classe de domínio importar "import org.grails.databinding.BindingFormat"
Ex.:

//import aqui
import org.grails.databinding.BindingFormat
class Pessoa{
...
@BindingFormat('MMddyyyy')
Date dataNascimento
...
Tags: DATA


1
Eu fiz uma taglib para ajudar em meus projetos.
Segue código.
class DataTagLib {

/**
* Cria um campo texto comum com mascara de DATA e um calendar para escolha com mouse.
*
* @emptyTag
*
* @attr name REQUIRED the field name
* @attr value the field value
*/
Closure data = { attrs ->
attrs.type = "text"
attrs.tagName = "data"
fieldImpl(out, attrs)
eventImpl(out, attrs)
}

def fieldImpl(out, attrs) {
resolveAttributes(attrs)
// out << "<input type=\"${attrs.remove('type')}\" "
// outputAttributes(attrs, out, true)
// out << "/>"

//Create date text field and supporting hidden text fields need by grails
String aux = ""
if (attrs.value != ""){
aux = Utils.getDataComoString(attrs.value, "dd/MM/yyyy")
}
out << "<input type=\"text\" name=\"${attrs.name}_\" id=\"${attrs.name}_\" value=\"${aux}\" "
outputAttributes(attrs, out, true)
out << "/>"
out << "<input type=\"hidden\" name=\"${attrs.name}\" id=\"${attrs.name}\" value=\"data.struct\" />"
out << "<input type=\"hidden\" name=\"${attrs.name}_day\" id=\"${attrs.name}_day\" />"
out << "<input type=\"hidden\" name=\"${attrs.name}_month\" id=\"${attrs.name}_month\" />"
out << "<input type=\"hidden\" name=\"${attrs.name}_year\" id=\"${attrs.name}_year\" />"
}

def eventImpl(out, attrs){
out.println "<script type=\"text/javascript\"> "
out.println "\$(function(){"
if (( attrs.value )&&(attrs.value != "")){
out.println " \$(\"#${attrs.name}_month\").attr(\"value\",\"${Utils.getDataComoString(attrs.value, "MM")}\");"
out.println " \$(\"#${attrs.name}_day\").attr(\"value\",\"${Utils.getDataComoString(attrs.value, "dd")}\");"
out.println " \$(\"#${attrs.name}_year\").attr(\"value\",\"${Utils.getDataComoString(attrs.value, "yyyy")}\");"
}
out.println " \$(\"#${attrs.name}_\").mask(\"99/99/9999\");"
out.println " \$(\"#${attrs.name}_\").datepicker({"
out.println " onClose: function(dateText, inst) {"
out.println " var dia = dateText.substring(0, 2); "
out.println " var mes = dateText.substring(3, 5); "
out.println " var ano = dateText.substring(6, 10); "
out.println " \$(\"#${attrs.name}_month\").attr(\"value\",mes);"
out.println " \$(\"#${attrs.name}_day\").attr(\"value\",dia);"
out.println " \$(\"#${attrs.name}_year\").attr(\"value\",ano);"
out.println " }"

//If you want to customize using the jQuery UI events add an if block an attribute as follows
if (attrs.minDate != null) {
out.println " ,"
out.println " minDate: ${attrs.minDate}"
}

if (attrs.showDay != null) {
out.println " ,"
out.println " beforeShowDay: function(date){"
out.println " var day = date.getDay();"
out.println " return [day == ${attrs.showDay},\"\"];"
out.println " }"
}
out.println ", changeMonth: true "
out.println ", changeYear: true "
out.println ", yearRange: '${Utils.getAno(Utils.somaAnos(Utils.getDataAtual(), -60))}:${Utils.getAno(Utils.getDataAtual())}'"
out.println " });"
out.println "})</script>"
}

/**
* Dump out attributes in HTML compliant fashion.
*/
void outputAttributes(attrs, writer, boolean useNameAsIdIfIdDoesNotExist = false) {
attrs.remove('tagName') // Just in case one is left
attrs.each { k, v ->
writer << k
writer << '="'
writer << v.encodeAsHTML()
writer << '" '
}
if(useNameAsIdIfIdDoesNotExist) {
outputNameAsIdIfIdDoesNotExist(attrs, writer)
}
}

/**
* Check required attributes, set the id to name if no id supplied, extract bean values etc.
*/
void resolveAttributes(attrs) {
if (!attrs.name && !attrs.field) {
throwTagError("Tag [${attrs.tagName}] is missing required attribute [name] or [field]")
}

attrs.remove('tagName')

def val = attrs.remove('bean')
if (val) {
if (attrs.name.indexOf('.')) {
attrs.name.split('\.').each {val = val?."$it"}
}
else {
val = val[name]
}
attrs.value = val
}
attrs.value = attrs.value != null ? attrs.value : "" // can't use ?: since 0 is groovy false

// Some attributes can be treated as boolean, but must be converted to the
// expected value.
booleanToAttribute(attrs, 'disabled')
booleanToAttribute(attrs, 'checked')
booleanToAttribute(attrs, 'readonly')
}

/**
* Some attributes can be defined as Boolean values, but the html specification
* mandates the attribute must have the same value as its name. For example,
* disabled, readonly and checked.
*/
private void booleanToAttribute(def attrs, String attrName) {
def attrValue = attrs.remove(attrName)
// If the value is the same as the name or if it is a boolean value,
// reintroduce the attribute to the map according to the w3c rules, so it is output later
if (Boolean.valueOf(attrValue) ||
(attrValue instanceof String && attrValue?.equalsIgnoreCase(attrName))) {
attrs.put(attrName, attrName)
} else if (attrValue instanceof String && !attrValue?.equalsIgnoreCase('false')) {
// If the value is not the string 'false', then we should just pass it on to
// keep compatibility with existing code
attrs.put(attrName, attrValue)
}
}

private outputNameAsIdIfIdDoesNotExist(attrs, out) {
if(!attrs.containsKey('id') && attrs.containsKey('name')) {
out << 'id="'
out << attrs.name?.encodeAsHTML()
out << '" '
}
}

}
16/09/2014 14:26


0
Valeu Shalom!

Resolveu meu problema!
26/09/2014 10:11



Ainda não faz parte da comunidade???

Para se registrar, clique aqui.


Aprenda Groovy e Grails com a Formação itexto!

Newsletter Semana Groovy

Assinar

Envie seu link!


Livro de Grails


/dev/All

Os melhores blogs de TI (e em português) em um único lugar!

 
Creative Commons
RSS Grails Brasil é mantido por itexto Consultoria.
Em caso de problemas contacte Henrique Lobo Weissmann (Kico) por e-mail: kico@itexto.com.br
Todo o conteúdo presente neste site adota o Creative Commons como licença padrão.
Ver: 4.14.0
itexto