Thymeleaf - Only print row if (th:field is != null)


Thymeleaf - Only print row if (th:field is != null)



I have two questions actually.



: I have this input:







This Id (numEventsByPage), can I use it globally or does it restrict only inside tha tag?



: And, how do i print a row only if this field is !=null ?



For exemple:




// print row...
...



I was trying the way of this exemple, but is was not working or returning any erros.
If anyone could direct me to a good tutorial or the answer itself, i would be veary grateful.





'null' is a string. Check for null: ${ ... != null } and check for empty string: ${ .... != '' }
– Flocke
Jul 2 at 13:46




2 Answers
2



th:field is directly related to th:object. Somewhere in your code you are defining a th:object like this (probably in a form tag).


th:field


th:object


th:object


<form th:object="${whatever} />



A th:field expression is equivalent in this way:


*{numEventsByPage} == ${whatever.numEventsByPage}



Notice that whatever must match your th:object definition. You can also use the #object shortcut to refer to your th:object. So your options are:


whatever


th:object


#object


th:object


<!-- Where whatever is your th:object -->
<div th:if= "${whatever.numEventsByPage != null}">



or


<div th:if= "${#object.numEventsByPage != null}">



1.) Currently as per the code you Pasted here, it is being restricted to this input here only.
And Yes! you can use it Globally however By 'using it Globally' I'm assuming that you want to put this object inside a JavaScript Global Variable. if that's the case use -

Thymeleaf's <script th:inline="javascript"> tag.'
Also, alternatively in order to get this value ,you can always use JQuery or JavaScript.


<script th:inline="javascript">



2.)To answer this question, try this :---


<div th:if="${numEventsByPage != ''}" >






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

PHP contact form sending but not receiving emails

PHP parse/syntax errors; and how to solve them?

iOS Top Alignment constraint based on screen (superview) height