我有一个问题。我做了一个角度的web应用程序,它涉及一个Ajax调用json格式的日期,以在表中显示它们。它有一个搜索输入,两个按钮(按年龄或姓名排序)。它也有一个配置文件(侧边栏元素),Angular JS根据搜索结果和点击事件更新DOM元素
我已经管理如何在点击表格行上更新侧边栏(名称,年龄,图片,短语,fav动物),但我需要更新侧边栏信息,当我按排序按钮(按名称或年龄排序,当我进行搜索输入时),我必须根据点击的排序按钮或搜索输入以某种方式使用此json数据填充侧栏。提前谢谢了!
这里是HTML的一部分
<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>
这里是角部 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;
} });
所以,如果我没有弄错,每次使用排序功能时需要获取数据,还是只加载一次数据? – mdarmanin
是的,每次点击排序按钮和输入匹配输入时,边栏都需要更新。 –
因此需要重新检索数据? – mdarmanin