I have a query regarding my angular web app, which includes an ajax call for json-formatted dates to display them in a table. The app features a search input, two sorting buttons (for age or name), and a profile sidebar element.
I have successfully updated the sidebar info (name, age, pic, phrase, fav animal) when clicking on table rows. However, I need to update the sidebar information in the same way when pressing the sorting buttons (sorting by name or age), as well as when making search inputs. I am looking for a way to populate the sidebar with JSON data based on the clicked sort buttons or search inputs. Thank you in advance!
Here is the HTML snippet
<div class="app container-fluid" ng-controller="mainController">
<form>
<div class="form-group">
<div class="input-group">
<div class="input-group-addon"><i class="fa fa-search"></i></div>
<input type="text" class="form-control" placeholder="Search right person" ng-model="searchAnimal" ng-model="updateAnimal">
</div>
</div>
</form>
<div class="row">
<div class="col-sm-12">
<div class="toolbar">
<button ng-click="sortType = 'name'; sortReverse = !sortReverse; clickAnimal()" class="btn btn-default"><i class="icon fa fa-sort-alpha-asc"></i><span> Sort by name</span>
<span ng-show="sortType == 'name' && !sortReverse"></span>
</button>
<button ng-click="sortType = 'age'; sortReverse = !sortReverse; clickAnimal()" class="btn btn-default"><i class="icon fa fa-sort-numeric-desc"></i><span> Sort by age</span>
<span ng-show="sortType == 'age' && !sortReverse"></span>
</button>
</div>
</div>
</div>
<div class="row" ng-cloak>
<div class="col-sm-4 col-md-3 col-lg-2">
<div class="thumbnail"><img ng-src="{{ animImage }}.svg"/>
<div class="thumbnail-caption"><h3>{{ animName }}</h3><table class="user-info table table-responsive" ><tbody><tr><td>Age:</td><td>{{ animAge }}</td></tr><tr><td>Favorite animal:</td><td>{{ animImage }}</td></tr><tr><td>Phone:</td><td><span>{{ countryCode }} </span>{{ animPhone }}</td></tr></tbody></table><p><b >Favorite phrase:</b><span> </span><span>{{ animPhrase }}</span></p></div>
</div>
</div>
<div class="col-sm-8 col-md-9 col-lg-10">
<table class=" user-list table table-striped">
<thead>
<tr>
<td>
<a href="#">
Image
</a>
</td>
<td>
<a href="#">
Name
</a>
</td>
<td>
<a href="#">
Age
</a>
</td>
<td>
<a href="#">
Phone
</a>
</td>
</tr>
</thead>
<tbody>
<tr ng-cloak ng-click="showAnimal()" ng-repeat="animal in userList | orderBy:sortType:sortReverse |filter:searchAnimal">
<td><img ng-src="{{ animal.image }}.svg" class="user-image"></td>
<td>{{ animal.name }}</td>
<td>{{ animal.age }}</td>
<td>{{ animal.phone }}</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
And here is the Angular part: angular.module('displayApp', []).controller('mainController', function($scope, $http) { $scope.sortType = 'name'; // set the default sort type $scope.sortReverse = false; // set the default sort order $scope.searchAnimal = ''; // set the default search/filter term $http.get("data.json") .then(function(response) { $scope.userList = response.data; $scope.animName = $scope.userList[0].name; $scope.animImage = $scope.userList[0].image; $scope.animAge = $scope.userList[0].age; $scope.animPhone = $scope.userList[0].phone; $scope.animPhrase = $scope.userList[0].phrase; }); $scope.countryCode = "8"; $scope.showAnimal = function(){ $scope.animName = this.animal.name; $scope.animImage = this.animal.image; $scope.animAge = this.animal.age; $scope.animPhone = this.animal.phone; $scope.animPhrase = this.animal.phrase; } });