javascript - 项目只有在输入后才显示? Angular js

标签 javascript angularjs

除了两件事之外,几乎一切都运转良好

代码

var app = angular.module("MyApp", []);

app.filter('offset', function() {
  return function(input, start) {
    start = parseInt(start, 10);
    return input.slice(start);
  };
});

app.controller("MyControler", function($scope, $http, $filter) {
      $http.get("http://127.0.0.1:8000/cars/?format=json").
        success(function(data) {
              $scope.list = data;
          });

  $scope.itemsPerPage = 1;
  $scope.currentPage = 0;
  $scope.items = [];

  for (var i=0; i<50; i++) {
    $scope.items.push({ id: i, name: "name "+ i, description: "description " + i });
  }

  $scope.range = function() {
    var rangeSize = 3;
    var ret = [];
    var start;

    start = $scope.currentPage;
    if ( start > $scope.pageCount()-rangeSize ) {
      start = $scope.pageCount()-rangeSize+1;
    }

    for (var i=start; i<start+rangeSize; i++) {
      ret.push(i);
    }
    return ret;
  };

  $scope.prevPage = function() {
    if ($scope.currentPage > 0) {
      $scope.currentPage--;
    }
  };

  $scope.prevPageDisabled = function() {
    return $scope.currentPage === 0 ? "disabled" : "";
  };

  $scope.pageCount = function() {
    return Math.ceil($scope.filtered.length/ $scope.itemsPerPage)-1;
  };

  $scope.nextPage = function() {
    if ($scope.currentPage < $scope.pageCount()) {
      $scope.currentPage++;
    }
  };

  $scope.nextPageDisabled = function() {
    return $scope.currentPage === $scope.pageCount() ? "disabled" : "";
  };

  $scope.setPage = function(n) {
    $scope.currentPage = n;
  };


  var filterBy = $filter('filter');
  $scope.$watch('search', function (newValue) { $scope.filtered = filterBy($scope.list, newValue); }, true);

});
<!DOCTYPE html>
        {% load staticfiles %}
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>



</head>
<body>

{% verbatim %}

  <div ng-app="MyApp" ng-controller="MyControler">
    <table class="table table-striped">
      <thead>
            <tr>
        <th><input ng-model="search.name" ></th>
        <th><input ng-model="search.years"></th>
        <th><input ng-model="search.owners"></th>
        <th><input ng-model="search.accidents"></th>
        <th><input ng-model="search.description"></th>
    </tr>
        <tr>

          <th>Name</th>
              <th>Years</th>
          <th>Owners</th>
              <th>Accidents</th>
            <th>Description</th>
        </tr>
      </thead>
      <tbody>
        <tr ng-repeat="cars in filtered| offset:currentPage*itemsPerPage | limitTo: itemsPerPage">

          <td>{{cars.name}}</td>
          <td>{{cars.years}}</td>

          <td>{{cars.owners}}</td>
          <td>{{cars.accidents}}</td>

          <td>{{cars.description}}</td>

        </tr>
      </tbody>
      <tfoot>
        <td colspan="3">
          <div class="pagination">
            <ul>
              <li ng-class="prevPageDisabled()">
                <a href ng-click="prevPage()">« Prev</a>
              </li>
              <li ng-repeat="n in range()" ng-class="{active: n == currentPage}" ng-click="setPage(n)">
                <a href="#">{{n+1}}</a>
              </li>
              <li ng-class="nextPageDisabled()">
                <a href ng-click="nextPage()">Next »</a>
              </li>
            </ul>
          </div>
        </td>
      </tfoot>
    </table>
  </div>







{% endverbatim %}
</body>

 <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>
  <script src="{% static 'js/app2.js' %}"></script>
</html>

只有当我开始在过滤器字段中输入内容时,才会显示我的对象,正如我在分页通过输入主动更新后看到的那样,但随后发生了奇怪的事情,分页也显示了带有缺点的页面?

所以我想让项目在不开始输入过滤器的情况下就已经显示,并使这些缺点消失。

谢谢;)

最佳答案

var app=angular.module('MyApp', []);

app.controller("MyControler", function($scope, $http, $filter) {
      $http.get("http://127.0.0.1:8000/cars/?format=json").
        success(function(data) {
              $scope.list = data;
          });
    $scope.currentPage = 0;
    $scope.pageSize = 1;


    $scope.numberOfPages=function(){
        var myFilteredData = $filter('filter')($scope.list, $scope.search);
        return Math.ceil(myFilteredData.length/$scope.pageSize);
    };

});


app.filter("offset", function() {
    return function(input, start) {
        start = +start;
        return input.slice(start);
    };
});
<!DOCTYPE html>
        {% load staticfiles %}
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>



</head>
<body>

{% verbatim %}

  <div ng-app="MyApp" ng-controller="MyControler">

        <table>
            <tr>
        <th><input type="text" ng-model="search.name" class="form-control input-sm" placeholder="SEARCH" ></th>
        <th><input type="text" ng-model="search.years" class="form-control input-sm" placeholder="SEARCH"></th>
        <th><input type="text" ng-model="search.owners" class="form-control input-sm" placeholder="SEARCH"></th>
        <th><input type="text" ng-model="search.accidents" class="form-control input-sm" placeholder="SEARCH"></th>
        <th><input type="text" ng-model="search.description" class="form-control input-sm" placeholder="SEARCH"></th>
    </tr>
        <tr>

          <th>Name</th>
              <th>Years</th>
          <th>Owners</th>
              <th>Accidents</th>
            <th>Description</th>
        </tr>

        <tr ng-repeat="cars in list | filter:search|offset:currentPage*pageSize | limitTo:pageSize">

                    <td>{{cars.name}}</td>
          <td>{{cars.years}}</td>

          <td>{{cars.owners}}</td>
          <td>{{cars.accidents}}</td>

          <td>{{cars.description}}</td>

        </tr>
      </table>


    <button ng-disabled="currentPage == 0" ng-click="currentPage=currentPage-1">
        Previous
    </button>
    
    {{currentPage+1}}/{{numberOfPages()}}
    <button ng-disabled="(currentPage + 1) == numberOfPages()" ng-click="currentPage=currentPage+1">
        Next
    </button>

  </div>







{% endverbatim %}
</body>

 <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>
  <script src="{% static 'js/app2.js' %}"></script>
</html>
已解决

关于javascript - 项目只有在输入后才显示? Angular js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27594175/

相关文章:

javascript - Ionic 3 减少启动时间

javascript - 对 ng-model 使用 $index

javascript - 如何使用 $q promise 在 angular js 中并行处理多个(循环内)异步 api 调用?

javascript - ng 样式/动态照片网格上的 Angular 摘要循环

javascript - 在 Controller 中使用 'this' 或 "var Vm = this"声明的变量未反射(reflect)使用指令访问时的值

javascript - AngularJS:超时 promise 调用

javascript - 我应该将 $(document).height() 和/或 $(window).height() 存储为变量吗?

javascript - 更改链接的 href

javascript - 动态管理单元小部件

javascript - 如何将javascript生成的新div插入现有的div?