Using "ng-show" inside "ng-repeat", - angularjs

I'm developping web appliccation using AngularJS.
Applicattion show a list of items in a table.
This is the code which prints the table:
<table>
<tr ng-repeat="x in content.listaOperaciones>
<td>{{ x.idOperacion }} </td>
<td>{{ x.idGan }}</td>
<td>{{ x.idEm}}</td>
<td>{{ x.idGate}}</td>
<td>{{ x.idRf }}</td>
<td>{{ x.peso }}</td>
<td>{{ x.duracion }}</td>
<td>{{ x.timestampInicioOperacion | date:'dd-MM-yy HH:mm'}}</td>
</tr>
</table>
This is how the table looks like once is printed in html.
I would like to implement multiple filters in each element in table by using some variables that can be accessed by user.
My idea is to filter each elements inside "ng-repeat" statement by using "ng-show" or "ng-if" statement.
This is the html code where user can modify variables:
<input ng-model="tiempoMinimo">
<input ng-model="$ctrl.PesoMinimo">
<input ng-model="$ctrl.PesoMaximo">
And this is how I'm modifying the table constructor:
<tr ng-repeat="x in content.listaOperaciones | orderBy : $ctrl.orderProp | filter:$ctrl.Busc" ng-if="x.peso > $ctrl.PesoMinimo && x.peso < $ctrl.PesoMaximo">
<div ng-show="x.duracion < $parent.tiempoMinimo">
<td>{{ x.idOperacion }} </td>
<td>{{ x.idGan }}</td>
<td>{{ x.idEm}}</td>
<td>{{ x.idGate}}</td>
<td>{{ x.idRf }}</td>
<td>{{ x.peso }}</td>
<td>{{ x.duracion }}</td>
<td>{{ x.timestampInicioOperacion | date:'dd-MM-yy HH:mm'}}</td>
</tr>
Filtering by using "ng-if" inside "ng-repeat" clausule is working.
Filtering by using "ng show" nested in "ng-repeat" loop is not working...
Since I want to use multiple filters (maybe 10 or 20) I would prefer using "ng-show" directive nested inside "ng-repeat" loop, so code would like easier to understand...
Here you have fiddle with source code working:
https://jsfiddle.net/DSG00/f94sjtty/
Thank you in advance.
Didac

Your fiddle isn't working because you can't use div inside tr. Move your ng-show to the tr element where your ng-repeat is. Also change the condition like that !$parent.tiempoMinimo || x.peso > $parent.tiempoMinimo so when your input is empty it won't hide the whole table.

Related

Calculating and storing data after each ngFor in Angular 2

I am creating a transaction tracker as a MEAN project and is stuck in here. I need to show all the transactions using ngFor. I want to show the Difference of credit and debit amount with the previous balance added to it after each transaction. Any idea how to do it?
This is my ngFor Code.
<tbody>
<tr *ngFor="let trans of transactions; index as i;">
<td>{{ trans.date | date:'dd-MM-yyyy'}}</td>
<td>{{ trans.billNo }}</td>
<td>{{ trans.description }}</td>
<td>{{ trans.creditAmt }}</td>
<td>{{ trans.debitAmt }}</td>
<td>{{ trans.debitAmt + trans.creditAmt + (DONT KNOW HOW TO ADD THE PREVIOUS BALANCE)}} </td>
</tr>
</tbody>
In my table now you can see, the balance is not being calculated properly.
How can I do this? How to store balance after each ngFor iteration and display it? None of my logic seems to be working.
<tbody>
<tr *ngFor="let trans of transactions; index as i;">
<td>{{ trans.date | date:'dd-MM-yyyy'}}</td>
<td>{{ trans.billNo }}</td>
<td>{{ trans.description }}</td>
<td>{{ trans.creditAmt }}</td>
<td>{{ trans.debitAmt }}</td>
<td>{{ trans.debitAmt + + (DONT KNOW HOW TO ADD THE PREVIOUS BALANCE)}} </td>
</tr>
</tbody>
Well, you have the index. If i != 0..
(trans.creditAmt - trans.debitAmt) + (transactions[i - 1].debitAmt + trasnactions[i - 1].creditAmt)
You should do this in the component, though. As you'll need to create a variable to keep track of the previous balance, as it's also a calculation.

Changing the display value based on a row value in ng-repeat

I wanted to display a value in a row in ng-repeat based on the value from the database. i wanted to do some thing like this displayed below
I wanted to display if value T for TRAINER and M for MANAGER. could you give me a simple and efficient way to do it.
I think it is easy if you are used ng-if
<tr ng-repeat="item in list">
<td>{{ item.number }}</td>
<td ng-if="{{item.Role}} == 'TRAINER'">T</td>
<td ng-if="{{item.Role}} == 'MANAGER'">M</td>
</tr>
code not tested. just scratch to get an idea...
You can use ternaries in {{ }}
<tr ng-repeat="data in datas">
<td>{{ data.number }}</td>
<td>{{ data.value == 'T' ? 'TRAINER' : (data.value == 'M' ? 'MANAGER' : data.value) }}</td>
</tr>

I have a angularjs table populated using ng-repeat from a JSON obj. How do I show the table rows with different colors based on a column value?

My code is something like below..
<tr ng-repeat="x in employee">
<td>{{ x.name }}</td>
<td>{{ x.age }}</td>
<td>{{ x.company }}</td>
</tr>
Name Age Company
John 25 Apple
Jim 26 Apple
Joe 27 Microsoft
Sam 28 Microsoft
Now I want the first 2 rows to be displayed in a color say red and the last 2 rows to be displayed in another color say blue based on the company name. I want to do this without hard coding the company name.
Use ngClass directive like:
<tr ng-repeat="x in employee" ng-class="{{x.company | lowercase}}">
<td>{{ x.name }}</td>
<td>{{ x.age }}</td>
<td>{{ x.company }}</td>
</tr>
Then define those classes in your CSS:
.apple{
background-color:red;
}
.microsoft{
background-color:blue;
}

AngularJS searching from navbar in view

I have a navbar on my page that has an input on it that I want to search a view, that should load in index.html after the user types in their search items. I can only figure out how to search with an input if it's in the same page that the table of ng-repeat items is in. Is there a way to search the table outside of the view? I've created a plnkr. It doesn't work. I'm not sure how to make it work. http://plnkr.co/edit/nqChzn5OATNMeSZL7ItJ?p=preview
Here is some of my code:
navbar
<input type="text" class="form-control" placeholder="Search" ng-model="vm.query">
Here is my table where the data displays.
<table ng-if="query" class="table table-hover table-responsive" >
<thead>
<tr>
<th>
({{filteredResults.length}}) Results Found
</th>
</tr>
<tr>
<td>Acc. ID</td>
<td>Acc. Name</td>
<td>Acc Address</td>
<td>City</td>
<td>Zip</td>
<td>Phone</td>
<td>Parent Name</td>
<td>Account Type</td>
<td>Account Status</td>
<td>Credit Term</td>
</tr>
</thead>
<tbody>
<tr ng-repeat="result in vm.results | filter:query as filteredResults">
<td>{{ result.accountId }}</td>
<td>{{ result.accountName }}</td>
<td>{{ result.address }}</td>
<td>{{ result.city }}</td>
<td>{{ result.state }}</td>
<td>{{ reuslt.zip }}</td>
<td>{{ result.phone }}</td>
<td>{{ result.parentName }}</td>
<td>{{ result.accountType }}</td>
<td>{{ result.accountStatus }}</td>
<td>{{ result.accountStatus }}</td>
</tr>
</tbody>
</table>
Is it possible to do what I want to do?
I've looked at your plunker and I don't believe that your code hasn't initialized angular. I can't see an ng-app tag anywhere in your code.
That a side, you will be able to use any input to filter/search (via. various implementations) so long as the input and table are contained with your ng-app and controller parts of the DOM. Otherwise you won't be able to access it the controller.
Can I suggest that you distill your question down? give the smallest amount of code that demonstrates your problem.
here's a rough outline:
<body ng-app="app" ng-controller="cntrl">
<!-- nav bar -->
<div>
<input ng-model="filterVal"/>
</div>
<!-- table -->
<div>
<table>
<tr ng-repeat="r in data.rows | filter:filterVal">....</tr>
</table>
</div>
</body>
Here's a plunker where the default filter functionality is working: http://plnkr.co/edit/GDJs5xTCpqq48SDFTk67
If you need to have your nav bar outside of your app/controller or in an different controller (etc.) then there are solutions to this.
Assuming you are using ui-route, this is how to use a service to communicate between controllers:
functioning example:
http://plnkr.co/edit/fZZLUvlHO9zUFwQYd1an?p=preview
Eggheads video:
https://egghead.io/lessons/angularjs-sharing-data-between-controllers

AngularJs ngSwitch with ngRepeatStart / End

I'm tring to figure out how I could us the ngSwitch with ngRepeatStart and End on two table rows, where second row is only shown when you click on the top one.
Here's what I've got and want to make work (doesn't work):
<tr
data-ng-repeat-start="user in users | filter : { name: search, active: searchActive }"
data-ng-click="selectUser(user)"
data-ng-switch on="isSelected(user)">
<td>{{ user.name }}</td>
<td>{{ user.email }}</td>
<td>{{ user.active }}</td>
<td>{{ user.dob | date: 'EEEE, dd MMMM yyyy' }}</td>
</tr>
<tr data-ng-repeat-end data-ng-switch-when="true">
<td colspan="4">
{{ user.description }}
</td>
</tr>
and here's what works:
<tbody
data-ng-repeat="user in users | filter : { name: search, active: searchActive }"
data-ng-click="selectUser(user)"
data-ng-switch on="isSelected(user)">
<tr>
<td>{{ user.name }}</td>
<td>{{ user.email }}</td>
<td>{{ user.active }}</td>
<td>{{ user.dob | date: 'EEEE, dd MMMM yyyy' }}</td>
</tr>
<tr data-ng-switch-when="true">
<td colspan="4">
{{ user.description }}
</td>
</tr>
</tbody>
I would like to avoid the wrapping tbody tag and instead use ngRepeatStart / End.
Is there a way of modifying my first example to make it work?
Don't use ng-switch, just use ng-show or ng-if. Here's a plunker
Edit
Okay, I guess if you really want you can use ng-switch, though I would suggest not doing it if all you're doing is checking true/false. Here's the updated plunker. The trick is that the switch needs to be on the second <tr>, and the ng-switch-when needed to be on the <td>.
Now if you need each switch to be a table row, then you're just going to have to use ng-if. The ng-switch attribute needs to be on the parent of all the ng-switch-when's. That's why the switch only works when you put it on <tbody>, or when you switch over <td>'s.

Resources