I want to print the address in the following order and I was able to do that. My problem is for some users some address fields are null then it is printing like this (1804 E Broadway, , 223344) and also I want divide the address in to lines.
<td>${addr1 }, ${addr2 }, ${postalCode }</td>
You can achieve your requirement is 3 ways:
1. JSP EL
<!-- here I am using adjacent EL expression because string concat will not work here and if we put EL expression in new line one space is adding-->
<td>${addr1 }${empty addr1? '': ','}
${addr2 }${empty addr2? '': ','}
${postalCode }</td>
2. JSTL
<c:if test="${not empty addr1}">
${addr1},
</c:if>
3. Using java code in JSP
I will not recommend this method because writing java code in JSP is not encouraged.
<%= (addr1 != null && addr1 !="") ? addr1 +",": "" %>
You can use a ternary operator in your JSP like this:
<%= (field != null && field !="") ? field +",": "" %>
Related
I want to replace my "undefined" values in my table with " ", to get only empty cells.
This is what i tried:
<tr *ngFor="let name of data">
<td>
{{name !== undefined ? name : ''}}
</td>
Nothing changed, how can i manage the undefined values?
Try like this.
If name contains any real value it will be used, otherwise empty string
{{name || ''}}
If name contains "undefined" world as string, rule above will print that string
You can do this:
{{ name ? name : ''}}
so whenever name is falsy, and undefined is falsy, it will return an empty string, when it's truthy it will return name.
This question already has an answer here:
String interpolation in Ruby doesn't work?
(1 answer)
Closed 5 years ago.
I've been trying to show random images everytime a user post something.
I have images name "kejang (#).png" # ranging from 1 to 21.
So I have done this.
<div class = 'article_header'>
<div class = 'article_img'>
<% kj_num = rand(20) + 1 %>
<%= kj_num %>
<%= image_tag 'kejang (#{kj_num}).PNG' %>
</div>
It does run on the server but returns no image, only 'x' marked crashed images.
I wondered where it went wrong and altered #{kj_num} with regular number and it worked. (i.e, I put 2 or 3 instead of #{kj_num} and it worked.)
Why is #{kj_num} not working ??
Rails interpretting #{variable} as a simple text. How should I change my code?
#{} vs. single vs. double quotes
Here you need to use double quotes (") rather than single quotes (')
<div class = 'article_header'>
<div class = 'article_img'>
<% kj_num = rand(20) + 1 %>
<%= kj_num %>
<%= image_tag "kejang (#{kj_num}).PNG" %>
</div>
</div>
String interpolation is the process of converting placeholders inside a string to the values they represent, in order to produce a dynamic string. You probably use it all the time without realizing just how cool it works under the hood, and how you can leverage this power in your own classes.
person = 'World'
puts "Hello, #{person}!"
https://kconrails.com/2010/12/08/ruby-string-interpolation/
You are trying to use string interpolation inside single quotes which will not work. You have to use double quotes instead
Here is an example to understand it,
jruby-1.7.13 :001 > name = 'ajinkya'
=> "ajinkya"
jruby-1.7.13 :002 > 'hello, #{name}'
=> "hello, \#{name}"
jruby-1.7.13 :003 > "hello, #{name}"
=> "hello, ajinkya"
More detailed explanation about Double vs single quotes
I am able to pass data to view file, but need to display them in different format with .
I try to use if statement to check the form name.
It returns error said "Operator '==' cannot be applied to operands of type 'System.Web.HtmlString' and 'string'".
Can I add blocks within IF condition? how to validate data within if condition in view file? Thank you! Here is the code:
#{ foreach (var form in #ViewBag.FormContent)
{
if (Html.Raw(form.Name) == "xyz") //pull up the title and text for the form
{
#Html.Raw(form.FormTitle)
<div class="panel-body">
<div style="height: 300px; overflow: auto; padding:15px;">
#Html.Raw(form.FormText)
</div>
</div>
}
}}
HTML.Raw returns an object that implements IHtmlString. It does not return a string. It doesn't even support ToString. Its only member is ToHtmlString(), which returns a string.
If you want to compare the output of Html.Raw with "xyz", you need to convert it to a string first. So instead of
if (Html.Raw(form.Name) == "xyz")
use something like
if (Html.Raw(form.Name).ToHtmlString() == "xyz")
Or... don't even bother with Html.Raw to begin with. Don't think you need it. Just write this:
if (form.Name == "xyz")
SQL (Active column is of type bit):
id Question Active
1 Weather today 1
ASP.net Eval:
<img src='<%# Eval("Active") == "1" ? "images/active.png" : "images/inactive.png" %>' />
HTML:
<img src="images/inactive.png">
Why is the inactive.png image showing and not the active.
Bit fields correspond to boolean. Also you need to do a type conversion to ensure right comparison is done, as Eval outputs just object. So:
(bool)Eval("Active") == true
You could try to cast the result:
((int)Eval("Active")) == 1 ? [...]
or as mentioned in the comments to a bool:
((bool)Eval("Active")) == true ? [...]
I have some values in my database which can be null if they have not already been entered.
But when I use Thymeleaf in my html, it gives an error when parsing null values.
Is there any way to handle this?
The shortest way is using '?' operator. If you have User entity with embedded Address entity in order to access fields of Address entity and print them if address is not null, otherwise here will be an empty column:
<td th:text="${user?.address?.city}"></td>
Sure there is. You can for example use the conditional expressions. For example:
<span th:text="${someObject.someProperty != null} ? ${someObject.someProperty} : 'null value!'">someValue</span>
You can even omit the "else" expression:
<span th:text="${someObject.someProperty != null} ? ${someObject.someProperty}">someValue</span>
You can also take a look at the Elvis operator to display default values like this:-
<span th:text="${someObject.someProperty} ?: 'default value'">someValue</span>
This can also be handled using the elvis operator ?: which will add a default value when the field is null:
<span th:text="${object.property} ?: 'default value'"></span>
You can use 'th:if' together with 'th:text'
<span th:if="${someObject.someProperty != null}" th:text="${someObject.someProperty}">someValue</span>
Also worth to look at documentation for #objects build-in helper:
https://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#objects
There is useful: ${#objects.nullSafe(obj, default)}
You've done twice the checking when you create
${someObject.someProperty != null} ? ${someObject.someProperty}
You should do it clean and simple as below.
<td th:text="${someObject.someProperty} ? ${someObject.someProperty} : 'null value!'"></td>
<p data-th-text ="${#strings.defaultString(yourNullable,'defaultValueIfYourValueIsNull')}"></p>
you can use this solution it is working for me
<span th:text="${#objects.nullSafe(doctor?.cabinet?.name,'')}"></span>
I use
<div th:text ="${variable != null} ? (${variable != ''} ? ${variable} : 'empty string message') : 'null message' "></div>
The shortest way! it's working for me,
Where NA is my default value.
<td th:text="${ins.eValue!=null}? ${ins.eValue}:'NA'" />