【问题标题】:grails input tag: placeholder not shown if value is a whitespacegrails输入标签:如果值是空格,则不显示占位符
【发布时间】:2014-08-21 01:43:14
【问题描述】:

我的 gsp(Grails 项目)中有一个输入标签字段,我想在字段为空时使用占位符来显示一些文本:

<input type="text" name="textField" id="textField" value="${receiptInstance?.patient?.surname} ${receiptInstance?.patient?.name}" placeholder=<g:message code="patient.choose" default="Insert patient..." />/>

当我创建一个新对象时,value 不是空的,但它有一个空格,所以不会显示占位符。 如何更改这种行为以使用占位符?有没有办法消除value 中的空格?

【问题讨论】:

  • 你不能嵌入这样的标签。您可能想要 &lt;input type="text" name="textField" id="textField" value="${receiptInstance?.patient?.surname} ${receiptInstance?.patient?.name}" placeholder="${message(code: 'patient.choose', default:'Insert patient'Insert patient...)}"/&gt;`
  • 它不起作用。空白仍然存在
  • 对。我看的不够近,被跳出来的 GSP 标签的无效嵌套分散了注意力。对于那个很抱歉。我在下面发布了一个答案,其中包含几个可行的选项。

标签: grails input placeholder gsp


【解决方案1】:

解决方法可以是:

<g:set var="myVal" value="${receiptInstance?.patient?.surname?:''} ${receiptInstance?.patient?.name?:''}"/>
<input type="text" name="textField" id="textField" value="${myVal?.trim()}" placeholder='<g:message code="patient.choose" default="Insert patient..." />'/>

注意:- 在您的占位符中添加 ',正如我添加的那样。或者您也可以使用以下代码

<input type="text" name="textField" id="textField" value="${myVal?.trim()}" placeholder="${g.message(code: 'patient.choose', default: 'Insert Patient...')}"/>

【讨论】:

  • 它没有按预期工作......该字段始终包含一个空格......只有当我删除它时,我才会看到占位符。
  • 抱歉,我遗漏了代码中的一件事。我已经更新了我的答案。再试一次。
【解决方案2】:

你可以这样做:

<g:set var="tmpValue" value="${receiptInstance?.patient?.surname} ${receiptInstance?.patient?.name}"/>
<g:textField name="textField"
             value="${tmpValue.trim()}"
             placeholder="${message(code: 'patient.choose', default:'Insert patient')}"/>

如果你想摆脱临时变量,你可以这样做:

<g:textField name="textField"
             value="${(receiptInstance?.patient?.surname + ' ' + receiptInstance?.patient?.name).trim()}"
             placeholder="${message(code: 'patient.choose', default:'Insert patient')}"/>

无论哪种方式,如果receiptInstance?.patient?.surname 或receiptInstance?.patient?.name 最终为null,您都需要处理出现的文字“null”。这很简单,但这是一个单独的问题,我将其省略以使上面的代码专注于所提出的问题。

希望对你有帮助。

编辑:

你也可以使用一个简单的标签来帮助:

// grails-app/taglib/com/demo/PatientTagLib.groovy
package com.demo

class PatientTagLib {
    static defaultEncodeAs = [taglib:'html']

    static namespace = 'patient'

    def fullName = { attrs ->
        def patient = attrs.patient
        if(patient) {
            def fullNameStr = patient.name ?: '' +
                              ' ' +
                              patient.surname ?: ''
            out << fullNameStr.trim()
        }
    }
}

然后在您的 GSP 中:

<g:textField name="textField"
             value="${patient.fullName(patient: receiptInstance?.patient)}"
             placeholder="${message(code: 'patient.choose', default:'Insert patient')}"/>

【讨论】:

    【解决方案3】:

    可以使用 .with() 的一些魔法:

    <input type="text" name="textField" id="textField" value="${receiptInstance?.patient?.with{surname +' '+ name}}" placeholder="<g:message code='patient.choose' default='Insert patient...' />"/>
    

    【讨论】:

    • 请解释你的答案。谢谢!
    猜你喜欢
    • 2021-11-28
    • 2019-11-07
    • 2018-09-02
    • 1970-01-01
    • 1970-01-01
    • 2020-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多