angular 5 *ngFor two array (merge objects of two arrays to make single array of objects) - arrays

I am getting array from two get method in angular 5. What i want is to combine those two arrays (JSON) into one array and print it in a single for loop
my two arrays:-
servers3 =[
{
sub:'sub',
subDesc:'subDesc'
}];
public tableData:any;
i am using concat two combine two arrays like follows
<table>
<tr>
<th>fileName</th>
<th>icon</th>
<th>fullPath</th>
<th>sub</th>
<th>subDesc</th>
<th>image</th>
</tr>
<tr *ngFor="let item of tableData.concat(servers3)">
<td>{{item?.fileName}}</td>
<td>{{item?.icon}} </td>
<td>{{item?.fullPath}}</td>
<td>{{item?.sub}}</td>
<td>{{item?.subDesc}}</td>
<td><img src="{{item?.fullPath}}" width="200px" height="200px" alt="adawdawd"/></td>
</tr>
</table>
</div>
And i am getting output as follows:-
as you can see its not showing in one line
each array completes individually
please give me solution if any one know the answer
thank you in advance

You can use following keeping in mind that length of both the arrays should be same otherwise it will generate error.
this.tableData.map((item, index) => {
return Object.assign(item, servers3[index]));
});
HTML
...
<tr *ngFor="let item of tableData">
...

Related

AngularJS: Display two arrays in separate columns of table

I have two arrays of variable lenghts containing html which need to be displayed side by side in a table by index. So arrayA[0] should be in the same row as arrayB[0], and so on.
I figured I could do this with two ng-repeat-start directives, e.g.:
<table>
<tbody ng-repeat-start="arrayA">
<tr ng-repeat-start="arrayB">
<td bind-html="arrayA.Details"></td>
<td bind-html="arrayB.Details"></td>
<tr ng-repeat-end></tr>
<tbody ng-repeat-end></tbody>
</table>
However this does not seem to work. Any ideas?
Thanks

Angular - create a dynamic template for ng-repeat over different objects

Suppose I have an array of objects. I need to create dynamically a table using ng-repeat. The thing is, I don't want to hardcode every property of the object to the html. I created an array which contain the relevant keys to the current object (the input can sometimes be different). How can I create ng-repeat which will run over each object of the data array and printing each property of the keys array I created earlier?
xlsxTableProps is an array with strings which are keys of every object inside parsedXslxData array of objects.
This is how I create the keys for the input (which again, can be different each time)
JS:
Object.keys(this.parsedXslxData[0]).forEach(key => {
this.xlsxTableProps.push(key)
});
HTML:
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col" ng-repeat="header in $ctrl.xlsxTableProps">{{header}}</th>
</tr>
</thead>
<tr ng-repeat="item in $ctrl.parsedXslxData">
<td>{{$index}}</td>
<td ng-repeat="tableItem in $ctrl.xlsxTableProps"></td>
<td>{{item[{{tableItem}}]}}</td>
</tr>
</table>
Problem is with your syntax here {{item[{{tableItem}}]}}, no need to use extra pair of brackets. Simply use {{item[tableItem]}}.

How can i use ng-repeat inside ng-repeat but inner ng-repeat should print one value of an array at a time

In the code below, mydt data is coming from a json and chkdur data is coming from a different array. I want to print the mydt and chkdur values simultaneously...
My Code
<tr ng-repeat="sdetail in mydt">
<td>{{sdetail.pmsg}}</td>
<td>{{sdetail.displaytime}}</td>
<td ng-repeat="st in chkdur">{{st}}</td>
</tr>
You should use $index to print respective json data from second array
<tr ng-repeat="sdetail in mydt">
<td>{{sdetail.pmsg}}</td>
<td>{{sdetail.displaytime}}</td>
<td>{{chkdur[$index]}}</td>
</tr>
Try This
<tr ng-repeat="(pIndex, sdetail) in mydt">
<td>{{sdetail.pmsg}}</td>
<td>{{sdetail.displaytime}}</td>
<td >{{chkdur[pIndex]}}</td>
</tr>

Angular Pagination - Filtered results not updating total-items

I have two filters, one for pagination and one for a search query. It searches first, then paginates on the result.
Here is the html:
<table class="table table-striped">
<tbody>
<tr ng-repeat="pot in (filteredPots = (pots | search:potQuery | paginate:currentPageNumber.pots:itemsPerPage))">
...
</tr>
</tbody>
</table>
<pagination ng-model="currentPageNumber.pots" ng-change="paging(pots)" items-per-page="{{itemsPerPage}}" total-items="filteredPots.length" class="pagination-sm"></pagination>
From what I understand this is working correctly (I can add {{ filteredPots.length }} and it shows the value 10).
What I am trying to do is have the total-items value as the total array length (i.e. without the pagination which limits it to itemsPerPage).
Any help is greatly appreciated, thanks!
Figured it out... took a lot of trial and error.
Html is updated to:
<table class="table table-striped">
<tbody>
<tr ng-repeat="pot in (filteredPots = (searchedPots = (pots| search:potQuery) | paginate:currentPageNumber.games:itemsPerPage))">
...
</tr>
</tbody>
</table>
<pagination ng-model="currentPageNumber.pots" ng-change="paging(pots)" items-per-page="{{itemsPerPage}}" total-items="searchedPots.length" class="pagination-sm"></pagination>
What's changed? Instead of filtering the array pots with two seperated filters, I'm filtering the array pots on the filter search:potQuery and saving the resulting array as searchedPots, then the second filter uses searchedPots as the array, and saves that result as filteredPots (which isn't used).
I then have the length of the searched pots BEFORE pagination as searchedPots.

Angularjs ng-repeat shows n items n times

I'm attempting to use ng-repeat in a couple of elements on my page.
For some reason it's showing each of the n elements n times and I cannot figure out why.
My array looks something like this:
$scope.fieldNames = ["Last_Name","First_Name","Email","Home_Phone","Cell_Phone"];
I have a select box that is filling n^2 times (I have tried using ng-options but cannot get it to work with the array):
<td>
<select ng-model="currentField" style="width: 100%">
<option ng-repeat="field in fieldNames">{{field}}</option>
</select>
<td>
Additionally this happens with the header row of my table.
But not with the member rows or the cells contained within.
<table class = "main-table">
<thead>
<tr>
<th ng-repeat = "field in fieldNames">{{field}}</th>
</tr>
</thead>
<tbody>
<tr ng-repeat = "member in members" >
<td ng-repeat = "field in fieldNames">{{member[field]}}</td>
</tr>
</tbody>
</table>
If someone could please explain to me why this is happening and how to fix it I would be grateful. A working example of the ng-options would also be appreciated.
Thanks in advance for your time.
To work with a list of strings in ng-options, you need the comprehension expression looks like this
<select ng-model="currentField" ng-options="field for field in fieldNames"></select>
DEMO
So it turns out that the reason each item was repeating n times is that both my page and the layout template I was using both had
<script type = "text/javascript" src = https://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.min.js"> </script>
As a result angular was apparently loading twice and running the ng-repeat on the ng-repeat, hence all the additional entries.

Resources