如何有条件地禁用表单输入字段 [英] How to conditionally disable a form input field
问题描述
使用grails run-target generate-all Teacher的视图,它会生成一个_form.gsp,用于创建和编辑Teacher。在编辑视图中,我只希望名称是可编辑的,但TeacherType一旦创建就不可修改(这只是一个示例,它是创建后无法更新某些字段的要求)。在创建视图中,TeacherType和name都应该是可编辑的。
既然create.gsp和edit.gsp呈现_form模板,那么这里的首选方法是什么?
- 创建两个单独的模板,即_formCreate.gsp,_formEdit.gsp;或
- 在create.gsp和edit.gsp中传入模型映射,并在_form.gsp中使用它们以有条件地渲染视图?
例如
在create.gsp中:
< fieldset class =form>
< / fieldset>
在edit.gsp中
< fieldset class =form>
< / fieldset> b
$ b 在_form.gsp中> < g:if test =$ {mode =='edit'}>
< / g:if>
< g:else>
< / g:其他>
方法2可行,但我想如果条件语句的数量增加,那么遵循方法可能会更好1和拆分表单。
是否还有另一种我不知道的方法?
< g:select>
(以及许多其他< g:...>
表单字段标记)可以是一个布尔值表达式:
< g:select name =teacherTypefrom =$ {TeacherType?.values()}
keys =$ {TeacherType.values()*。name()} required =
value =$ {teacherInstance?.teacherType?.name()}
disabled =$ {mode =='edit'}/>
这将呈现为 disabled =disabled
如果表达式的计算结果为真,并且缺少禁用的
属性(即该字段不会被禁用),如果表达式为false。您甚至可以在模型中使用布尔条目,例如使用
model =[teacherInstance:teacherInstance,editing:true]渲染模板
(或编辑:false
分别),然后说禁用=
< g:select>
。
=$ {editing}
Say I have a Domain object Teacher with two fields String name, TeacherType teacherType, where TeacherType is an enum containing AssitantProfessor, AssociateProfessor, Professor.
After I generate the views using grails run-target generate-all Teacher, it produces an _form.gsp that is used for both create and edit of Teacher. In the edit view I want only the name to be editable but the TeacherType to be unmodifiable once created (this is just an example, it is a requirement that certain fields can't be updated after creation). In the create view, both TeacherType and name should be editable.
Since both create.gsp and edit.gsp render the _form template, what is the preferred approach here?
- Create two separate templates i.e. _formCreate.gsp , _formEdit.gsp; Or
- Pass in a model map within create.gsp and edit.gsp and use them in _form.gsp to conditionally render the view? e.g.
In create.gsp:
<fieldset class="form">
<g:render template="form" model="[teacherInstance: teacherInstance, 'mode':'create']"/>
</fieldset>
In edit.gsp
<fieldset class="form">
<g:render template="form" model="[teacherInstance: teacherInstance, 'mode':'edit']"/>
</fieldset>
In _form.gsp
<g:if test="${mode == 'edit'}">
<g:select name="teacherType" from="${TeacherType?.values()}" keys="${TeacherType.values()*.name()}" required="" value="${teacherInstance?.teacherType?.name()}" disabled="disabled"/>
</g:if>
<g:else>
<g:select name="teacherType" from="${TeacherType?.values()}" keys="${TeacherType.values()*.name()}" required="" value="${teacherInstance?.teacherType?.name()}" disabled="false"/>
</g:else>
Approach 2 works but I suppose if the number of conditional statements increase it may just be better to follow approach 1 and split the forms.
Is there another approach that I'm not aware of?
The disabled
attribute of <g:select>
(and many other <g:...>
form field tags) can be a boolean-valued expression:
<g:select name="teacherType" from="${TeacherType?.values()}"
keys="${TeacherType.values()*.name()}" required=""
value="${teacherInstance?.teacherType?.name()}"
disabled="${mode == 'edit'}"/>
This will render as disabled="disabled"
if the expression evaluates to true, and as the absence of a disabled
attribute (i.e. the field will not be disabled) if the expression is false. You could even use a boolean entry in the model, e.g. render the template with
model="[teacherInstance: teacherInstance, editing:true]"
(or editing:false
respectively) and then say disabled="${editing}"
on the <g:select>
.
这篇关于如何有条件地禁用表单输入字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!