Add a class by checking a condition in angular 6 - angular6

I need to add a class 'test1' if the values in 'emp_code' and 'emp' is same.
I had tried with the code,
class="{{ emp_empcode == emp ? 'test1' : 'test2' }}"
I had tried with this and this
Any other way? Thanks a lot in advance.

[class.test1]="emp_code == emp"
should definitively work. You can also take a look at https://toddmotto.com/ng-class-angular-classes for more varieties

Related

Angular display conditional text in html including conditional data

I know in Angular2+ we can create condition in html like this:
<p>{{ dynamicData.value? dynamicData.value : dynamicData.default}}</p>
Also we can do something like this:
<p>{{ dynamicData.value? 'text 1' : 'text 2'}}</p>
But I would like to combine this two solutions and I have no idea how to do this. In general I would like to do something like this:
<p>{{ dynamicData.value? 'Dynamic data value is equal: {{dynamicData.value}}' : 'no dynamic data'</p>
Have you any idea how to handle this text interpolation?
You do not need to use interpolation within interpolation, you already have access to variables and can try something as below:
<p>{{ dynamicData?.value ? ('Dynamic data value is equal: ' + dynamicData?.value) : 'no dynamic data'}}</p>
<p *ngIf = "dynamicData.value"> 'Dynamic data value is equal' {{dynamicData.value}} </p>
<p *ngIf = "!dynamicData.value"> 'no dynamic data' </p>
You can use variable in your .ts file or you can use + interpolation 'Dynamic data value is equal: ' + dynamicData.default.
But I like more answer posted by FedMice ;)
I hope this works.
<p>{{ dynamicData.value ? (`Dynamic data value is equal: ${dynamicData?.value}`) : `no dynamic data`}}</p>

Angular - Cannot concatenate values in HTML page

In our Angular project, we use interpolation as shown below, but we also need to use this interpolated value in the [state] property. But we haev not managed so far. Any idea?
If we set id values as shown below, there is no problem.
<a routerLink="/ticket/details/" [state]="{ id: '5' }" >{{row.TicketId}}</a>
But we cannot get dynamically by obtaining row.TicketId (it is obtained as label in {{row.TicketId}}) but cannot concatenate with id parameter.
<a routerLink="/ticket/details/" [state]="{ id: {{row.TicketId}} }" >{{row.TicketId}}</a>
The brackets in [state]="..." tell Angular to evaluate the template expression, so you cant use interpolation there. So, as I said in comment it should be:
[state]="{ id: row.TicketId }"
Try [state]="{ id: row.TicketId }"

Jsonpath query nested objects

I've got this this json data that I want to look up a certain value from with a query:
http://pastebin.com/Vf59Cf9Q
I want to find description == "chassis" in this path:
$.entries..nestedStats.entries..nestedStats.entries.type
However, I just can't get it to work.
Tried these two alternatives, but no luck:
$.entries..nestedStats.entries..nestedStats.entries[?(#.type.description == 'chassis')]
$.entries..nestedStats.entries..nestedStats.entries.type[?(#.description == 'chassis')]
Have tried this on these sites:
http://jsonpath.com/
https://jsonpath.curiousconcept.com/
Any help would be appreciated!
/Patrik
This did the trick:
$.entries.*.nestedStats.entries.*.nestedStats.entries.type[?(# == "chassis")

How to find id which is string with semicolon

I want to find assigned jury id from the table.Problem is all that id are joint with semicolon
Here are the records.
id jury_id
1 2;4;6
I want to find jury_id=4. How can i find that record ?
Any help will be appreciated.
Use find_in_set():
where find_in_set(?, replace(jury_id, ';', ',')) > 0
Replace the ? with your target value.
Note that to use this function the search string must be a CSV, so you must replace the semicolons with commas.
there are two ways
1.
let say jury_id came as $ids;
$array=explode(',',$ids);
if(in_array('4',$array)
{
echo "id is in there";
}
2.
let say jury_id came as $ids;
if(strpos($ids,'4')==true)
{
echo "id is in there";
}
Like what F. Muller said, you can try it like
SELECT id,jury_id from table WHERE jury_id LIKE %$the_jury_id%
First remark is that it is an absolutely terrible database design.
To answer the question, you should be able to solve your problem with the LIKE operator:
id LIKE '%;4;%' or id LIKE '4;%' or id LIKE '%;4'
Try with
select * from tbl_name where jury_id like '%jury_id%'

Using Thymeleaf when the value is null

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'" />