How to concat 2 values inside a loop thymeleaf - loops

I have some problems concatenating 2 values in thymeleaf
I do the following:
<tr th:each="p , in: ${info}">
<td th:utext="${p.nombre}">...</td>
<td th:utext="${p.preferencias.get(0).getNombrePref()} +'-'+ ${p.preferencias.get(1).getNombrePref()}">...</td>
<td th:utext="${p.email}">...</td>
</tr>
This is how the page looks like
As you can see this is not dynamic because if p.preferencias size is bigger than 2 this will be a problem and the same if is less than 2
this what i tried :
<tr th:each="p , in: ${info}">
<td th:utext="${p.nombre}">...</td>
<td th:each="a , arcd : ${p.preferencias}" th:utext="${p.preferencias.get(arcd.index).getNombrePref()}">...</td>
<td th:utext="${p.email}">...</td>
</tr>
This is what i got:
And got this
As you can see the second value move to email :( then i thought in concat the values and try this :
<tr th:each="p , in: ${info}">
<td th:utext="${p.nombre}">...</td>
<td th:each="a , arcd : ${p.preferencias}" th:utext="${p.preferencias.get(arcd.index).getNombrePref()} + '-' +${p.preferencias.get(arcd.index).getNombrePref()} " >...</td>
<td th:utext="${p.email}">...</td>
</tr>
And this is the result:
Result 3
I dont know how to concat the values and keep those in one cell
How can i execute the loop to get all the values inside p.preferencias variable ?
EDIT:
post wrong image in result 3 now is the correct image

Finally solve the problem the tag td stay open thats why the secon value in the each iteration fill the next cell solve it using a span tag and iterate inside
<tr th:each="p , in: ${info}">
<td th:utext="${p.nombre}">...</td>
<td>
<span th:each="a , arcd : ${p.preferencias}" th:utext="${p.preferencias.get(__${arcd.index}__).getNombrePref()} + (${arcd.size-1 > arcd.index}? ', ':'')"></span>
</td>
<td th:utext="${p.email}">...</td>
</tr>
Output

Related

Protractor can't find value from a table

I have a table that displays search results, and it goes something like this:
<table>
<tbody _ngcontent-c4="">
<tr _ngcontent-c4="">
<td _ngcontent-c4="" class="owner">company name</td>
<td _ngcontent-c4="" class="name"> other name/a></td>
<td _ngcontent-c4="" class="status">kasutusel</td>
<td _ngcontent-c4="" class="approval-status">kooskõlastamata</td>
<td _ngcontent-c4="" class="topics">value</td>
<td _ngcontent-c4="" class="last-modified text-nowrap">value</td>
</tr>
<tr _ngcontent-c4="">
<td _ngcontent-c4="" class="owner">value</td>
<td _ngcontent-c4="" class="name"></td>
<td _ngcontent-c4="" class="name"></td>
<td _ngcontent-c4="" class="status">kasutusel</td>
<td _ngcontent-c4="" class="approval-status">some value/td>
<td _ngcontent-c4="" class="topics">some value</td>
<td _ngcontent-c4="" class="last-modified text-nowrap">table value</td>
</tr>
</tbody>
</table>
And I can't get protractor to find the first element, like "owner", my code is:
var rows = tabledata.all(by.tagName("tr"));
var cells = rows.all(by.className("owner"));
await expect(cells.get(0).getText()).to.eventually.equal("company name");
I've get: Trying to access element at index: 0, but there are only 0 elements that match locator By(css selector, .owner)
The testcase is like this: 1. go to mainpage, 2. search by string, 3. go to results page and verify the results from the table. I have a feeling it doesn't get any elements from that redirected page, although I've tried browser sleep and wait for angular. When I run the test though, it does go to the redirected page, displays the table, and then ends.
You can directly find out all .owner td as below showing:
var owners = tabledata.all(by.css('tr > td.owner'));
expect(owners.get(0).getText()).to.eventually.equal("company name");

AngularJS : Use of ng-if and ng-class combination

I have a table like this :
<tr>
<th scope="row">QUANTITÉ PRODUITE</th>
<td>{{hoursValues[0][0][0]}}</td>
<td>{{hoursValues[0][0][1]}}</td>
<td>{{hoursValues[0][0][2]}}</td>
<td>{{hoursValues[0][0][3]}}</td>
<td>{{hoursValues[0][0][4]}}</td>
<td>{{hoursValues[0][0][5]}}</td>
<td>{{hoursValues[0][0][6]}}</td>
<td>{{hoursValues[0][0][7]}}</td>
</tr>
<tr>
<th scope="row">OBJECTIF QUANTITÉ</th>
<td>{{hoursValues[0][2][0]}}</td>
<td>{{hoursValues[0][2][1]}}</td>
<td>{{hoursValues[0][2][2]}}</td>
<td>{{hoursValues[0][2][3]}}</td>
<td>{{hoursValues[0][2][4]}}</td>
<td>{{hoursValues[0][2][5]}}</td>
<td>{{hoursValues[0][2][6]}}</td>
<td>{{hoursValues[0][2][7]}}</td>
</tr>
I want to compare each column of a row with the other corresponding column, for example : hoursValues[0][0][0] && hoursValues[0][2][0] and according to this comparison, I want to apply the classes.
I mean, if hoursValues[0][0][0] > hoursValues[0][2][0], I want to display the result in green else red.
Can anyone help me ? What should I write in my controller ?
in each td just do this
<td ng-class="{true:'green', false:'red'}[hoursValues[0][0][0] > hoursValues[0][2][0]]">
obvius just change each column with corresponding one.
next td
<td ng-class="{true:'green', false:'red'}[hoursValues[0][0][1] > hoursValues[0][2][1]]">

Xpath axis not working as expected

Given this snippet:
<tr class="ng-scope" ng-repeat="config in list.elements">
<td class="ng-binding">6grxe</td>
<td class="ng-binding">ComplexTest</td>
<td class="ng-binding">2016-11-25 10:35:03</td>
</tr>
<tr class="ng-scope" ng-repeat="config in list.elements">
<td class="ng-binding">l7yc</td>
<td class="ng-binding">SimpleTest</td>
<td class="ng-binding">2016-11-25 10:35:15</td><!--how to locate this ?-->
</tr>
I tried to locate the marked element on firefox console using xpath like this:
$x("//following-sibling::td[text()='SimpleTest']")
However, the returned element is
<td class="ng-binding">SimpleTest</td>
Whats wrong with my xpath?
I think you want the path //td[. = 'SimpleTest']/following-sibling::td[1] to select the td element immediately following the td with contents SimpleTest.
You XPath expands to
/descendant-or-self::node()/following-sibling::td[text()='SimpleTest']
Basically you are creating the set of all td elements in the document which contain the text SimpleTest in a complicated way.

How to evaluate expression in view

In my following code i am creating an HTML table with table headers as First, second, Third and than row for each student.
Depending upon score, I need to put X in cell under relevant column header.
Example
If score 10 than put X under First
If score 9 than put X under second
If score 8 than put X under third
Can i re-write<td>{{score.Position}}</td> based on above requirement as following.
e.g.
<td>{{score.Position}} == 10 ? X :'' </td>
<td>{{score.Position}} == 9 ? X :'' </td>
Code:
<table>
<thead>
<tr>
<td></td>
<td>First</td>
<td>Second</td>
<td>Tbhird</td>
</tr>
</thead>
<tbody>
<tr data-ng-repeat="student in Students">
<td>{{student.Name}}</td>
<td>{{score.Position}}</td>
</tr>
</tbody>
</table>
Do this
<td>{{score.Position == 10 ? 'X' : ''}}</td>
Or this
<td><span data-ng-if="score.Position==10">X</span></td>

Angularjs - add additional row inside a tr ng-repeat

Ng-repeat is present on a table row
My query is how can we achieve the following:
<tr ng-repeat="x in y">
Looping here....
</tr>
Now as data object is looping on a <tr>. I have a scenario where I have to display data of 1 row in two <tr>.
Eg.
Table
Data1 data1.2 data1.3 data1.4
Data2 data2.2
Data2b data2.3 data2.4
Data3 data3.2 data3.3 data3.4
Data4 data4.2 data4.3
I.e. display data of 1 row in two
Can't use ng-repeat-end
1) You can combine ng-if with ng-repeat and 2) ng-repeat supports multi-element with ng-repeat-start and ng-repeat-end:
<tr ng-repeat-start="item in [1, 2, 3, 4, 5, 6]">
<td>{{item}}</td><td>something else</td>
</tr>
<tr ng-if="item % 2 === 0" ng-repeat-end>
<td colspan="2">-even</td>
</tr>
Demo
I modified the solution New Dev provided for my purposes & thought I'd share since it really saved me.
I needed a solution to repeat my table header row after every nth row as an alternative to a "frozen/fixed upon scrolling" header row, which just isn't feasible for my use case.
In this example, I'm showing the header row after every 25 rows, but not if it's going to be the last row in the table: (($index+1) != itemCollection.length).
<table class="table table-bordered">
<thead>
<tr>
<th>Column Header 1 - Value</th>
<th>Column Header 2 - Index</th>
</tr>
</thead>
<tbody>
<tr ng-repeat-start="item in itemCollection">
<td>{{item}}</td>
<td>{{$index}}</td>
</tr>
<tr ng-repeat-end="" ng-if="(($index+1) % 25 === 0) && ($index+1) != itemCollection.length">
<th>Column Header 1 - Value</th>
<th>Column Header 2 - Index</th>
</tr>
</tbody>
</table>
Check out the modified demo. For simplicity's sake, I used an inline collection, so the "not if it's going to be the last row in the table" logic isn't included (as you'll see with the unneeded column header at the very bottom), but you get the picture.
You need to repeat tbody instead of tr.
<tbody ng-repeat="x in y" >
<tr>
<td>{{X. row data}}</td>
<td>{{X. row data 2}}</td>
</tr>
<tr>
<td colspan="2">
{{X. secondRowData}}
</td>
</tr>
</tbody>
<tr ng-repeat-start=x in y>
<td>selectbox here
<tr\>
<tr ng-repeat-end ng-if=somecondition>
<td>label data <\td>
<\tr>
So now we have already used ng-repeat-end. Inside ng-repeat-end i have to display 2 rows for a single iteration.
<tr><td indexis1>selectbox<\td><\tr>
<tr><td indexis2>label primary<\td><\tr>
<tr><td indexis2>label secondary<\td><\tr>
This is a rough code snippet and there are numerous td already present in code.

Resources