简体   繁体   English

Angular JS根据搜索结果和单击事件更新DOM元素

[英]Angular JS update DOM element based on search results and click events

I've got a question. 我有一个问题。 I made an angular web app which involves an ajax call for json-formatted dates to display them in a table. 我制作了一个有角度的Web应用程序,其中涉及对json格式的日期进行ajax调用以将其显示在表格中。 It has a search input, two buttons (sort by age or name). 它有一个搜索输入,两个按钮(按年龄或名称排序)。 It also has a profile(sidebar element), 它还有一个配置文件(侧边栏元素),

I've managed how to update sidebar (name, age, pic, phrase, fav animal) on click table rows, but I need to update sidebar info the same when I press the sorting buttons (sort by name or age and when I make search inputs) I have to fill sidebar somehow with this json data, based on clicked sort buttons or search inputs. 我已经设法在点击表行上更新侧边栏(名称,年龄,图片,词组,收藏夹动物),但是当我按排序按钮(按名称或年龄以及进行排序时)时,我需要更新侧边栏信息搜索输入),我必须根据单击的排序按钮或搜索输入以某种方式用此json数据填充边栏。 Many thanks in advance! 提前谢谢了!

here is html part 这是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>

Here is angular part angular.module('displayApp', []) 这是角零件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;

} }); }});

You need to adjust the clickAnimal() function to update the data in the $scope.showAnimal function when you click one of the sort buttons. 单击排序按钮之一时,需要调整clickAnimal()函数以更新$ scope.showAnimal函数中的数据。 This would mean setting the animal data to the first item in the table when a sort button is clicked. 这意味着单击排序按钮时,将动物数据设置为表格中的第一项。

Read this for more iformation on how to change $scope values: http://jimhoskins.com/2012/12/17/angularjs-and-apply.html 请阅读以下内容以获取有关如何更改$ scope值的更多信息: http : //jimhoskins.com/2012/12/17/angularjs-and-apply.html

And this example on how $scope.apply is used: http://jsfiddle.net/k19966h8/ 以及有关如何使用$ scope.apply的示例: http : //jsfiddle.net/k19966h8/

A snippet: 摘录:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM