Grails -Gsp - How to make a checkbox checked based on a field value

I have a status attribute in my domain that is of type String , can have either one of two Applied , NotApplied

I have two checkboxes for entering this value. on my edit page I want to show these two checkboxes.

If the status value is Applied , you must select the corresponding check box.

my code

  <g:message code="publicRuleInstance.course.label" default="Applied" /> <g:checkBox name="status " value="${publicRuleInstance?.status }" /> <g:message code="publicRuleInstance.course.label" default="NotApplied" /> <g:checkBox name="status " value="${publicRuleInstance?.status }" /> 

but both flags are checked here.

there should be a way to check ie, if status = Applied, then this percussion flag must be drawn otherwise it must be unchecked.

Is there any way to do this?

+6
source share
2 answers

Use the checked attribute to control the state of your checkbox, as described in docs . Here you can add any expression to determine the state of g:checkBox :

 <g:message code="publicRuleInstance.course.label" default="Applied" /> <g:checkBox name="status " value="Applied" checked="${publicRuleInstance?.status == 'Applied'}"/> <g:message code="publicRuleInstance.course.label" default="NotApplied" /> <g:checkBox name="status " value="NotApplied" checked="${publicRuleInstance?.status == 'NotApplied'}"/> 

If you just want to allow one of the values ​​- Applied or NotApplied a g: radioGroup would be the best choice. Using checkBox, the user could select both Applied and NotApplied .

+4
source

CheckBox value must be boolean

 <g:message code="publicRuleInstance.course.label" default="Applied" /> <g:checkBox name="status " value="${publicRuleInstance?.status =="Applied"}" /> <g:message code="publicRuleInstance.course.label" default="NotApplied" /> <g:checkBox name="status " value="${publicRuleInstance?.status == "NotApplied" }" /> 
+2
source

Source: https://habr.com/ru/post/921405/


All Articles