th:if condition in same tag

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

th:if condition in same tag

sivaji27
Hi ,

Can one please help me how to write if condition in same tag.

like : <input type="text" th:if( condition : readonly=true : readonly=false) />

I tried like below but it is not working .
<input type="text" th:if="${messageStructures[__${rowStat.index}__].editable ? readonly=true : readonly=false}" />

Thanks in advance .

Thanks
Sivaji
Reply | Threaded
Open this post in threaded view
|

Re: th:if condition in same tag

danielfernandez
Administrator

Actually, the "readonly" attribute should be readonly="readonly" for "true" and nothing at all for false. And this is exactly what the "th:readonly" thymeleaf attribute does, based on the specified boolean condition:

    <input type="text" th:readonly="condition" />

Regards,
Daniel.
Reply | Threaded
Open this post in threaded view
|

Re: th:if condition in same tag

Zemi
Administrator
In reply to this post by sivaji27
Hello,

I think what you want is

   <input type="text" th:readonly="${messageStructures[__${rowStat.index}__].editable}" />

Reply | Threaded
Open this post in threaded view
|

Re: th:if condition in same tag

Zemi
Administrator
Oops, Daniel was faster...

Reply | Threaded
Open this post in threaded view
|

Re: th:if condition in same tag

sivaji27
In reply to this post by Zemi
Hi Zemi,

Thank you so much for your quick reply..

your code is working if is direct field but what i am looking is.

here "editable" boolean varable , based on that true or false i need to enable or disable text box field.

i tried like below ..
<input type="text" class="codeInput" th:field="*{messageStructures[__${rowStat.index}__].referenceNumber}" th:readonly="${messageStructures[__${rowStat.index}__].editable}" style="width:80px" />

Exception:-
org.springframework.expression.spel.SpelEvaluationException: EL1012E:(pos 17): Cannot index into a null value



Thanks in advance ..

Thanks
Sivaji
Reply | Threaded
Open this post in threaded view
|

Re: th:if condition in same tag

danielfernandez
Administrator
Hi,

That error is thrown by Spring Expression Language when you try to access an indexed property inside a null value.

Note that you are accessing that "messageStructures" variable as *{...} in one expression and as ${...} in the other. This, if you have previously specified a th:object=... attribute will have a different meaning (*{...} = property inside the object selected in th:object).

Regards,
Daniel.
Reply | Threaded
Open this post in threaded view
|

Re: th:if condition in same tag

sivaji27
Thanks Daniel

As per your suggestion i tried like below and it is working fine.. :)

<input type="text" class="codeInput" th:field="*{messageStructures[__${rowStat.index}__].ignoreRecord}" th:readonly="*{messageStructures[__${rowStat.index}__].editable}"/>

Thanks u so much...

Regards
Sivaji