2014-12-21 30 views
0

幾乎一切工作良好旁邊兩件事情項目僅在鍵入後纔會顯示?角JS

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>

當我開始輸入到過濾器領域,因爲我看到分頁正在積極更新後,我的對象只顯示代碼隨着打字,但隨後發生的奇怪,分頁顯示頁面與弊端?

所以我想讓項目顯示已經沒有開始鍵入過濾器,並使這些缺點消失。

感謝;)

+1

我不明白你的問題並沒有什麼與代碼段發生。謹慎解釋呢? – simeg

+0

在我的例子中,我嘗試創建自動更新的表格,現在我所取得的成績是,表格只在做了一些搜索之後才工作,我鍵入字母並顯示項目。我知道爲什麼會發生這種情況,這是因爲var過濾只在打字後纔開始工作,所以當我開始過濾時會顯示對象,我正在嘗試解決它,所以在加載模板時已經顯示對象,然後我可以執行活動用改變的分頁過濾。 – IOR88

+0

難道你不想在啓動時用$ scope.list填充$ scope.filtered嗎? – blt

回答

1

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>
解決

相關問題