2016-04-28 63 views
2

我有一个问题。我做了一个角度的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; 

} });

+0

所以,如果我没有弄错,每次使用排序功能时需要获取数据,还是只加载一次数据? – mdarmanin

+0

是的,每次点击排序按钮和输入匹配输入时,边栏都需要更新。 –

+0

因此需要重新检索数据? – mdarmanin

回答

0

当您单击某个排序按钮时,您需要调整clickAnimal()函数以更新$ scope.showAnimal函数中的数据。这意味着当点击排序按钮时,将动物数据设置为表中的第一项。

阅读本关于如何改变$范围值更见识:http://jimhoskins.com/2012/12/17/angularjs-and-apply.html

,并就如何$ scope.apply使用这个例子:http://jsfiddle.net/k19966h8/

一个片段:

function Ctrl($scope) { 
    $scope.message = "Waiting 2000ms for update"; 

    setTimeout(function() { 
     $scope.$apply(function() { 
      $scope.message = "Timeout called!"; 
     }); 
    }, 2000); 
} 
+0

谢谢,所以我应该寻找适用和手表? –

+0

是的,据我所知,应用和手表是此更新过程的组成部分。 – mdarmanin

+0

我想知道如果我写了排序函数,它会返回一个排序元素的数组,然后生病为我的侧栏设置变量的新值 –