1

私のコードはAPIからの投稿を繰り返し、9つの投稿を連続して表示します。私の目標はグリッドを作成することです - それぞれに3つのポストを持つ3行。私が試した解決策は機能しません。私も 'clearfix'を試みましたが、成功しませんでした。 誰でもこの作業を行う方法を知っていますか?ng-repeatを使用するときにAngularで行を作成する

は、ここでは、コードです:

var myApp = angular.module('myApp', ['ngRoute', 'ui.bootstrap']); 
 

 
    myApp.config(function ($routeProvider) { 
 
     $routeProvider.when('/', { 
 
      templateUrl: 'allposts.htm', 
 
      controller: 'PostsController' 
 
     }).when('/post', { 
 
      templateUrl: 'post.htm', 
 
      controller: 'PostController' 
 
     }).when('/addpost', { 
 
      templateUrl: 'addpost.htm', 
 
      controller: 'AddController' 
 
     }).otherwise({ 
 
      redirectTo: '/' 
 
     }); 
 
    }); 
 

 
    myApp.controller('PostsController', function ($scope) { 
 
    }); 
 

 
    myApp.controller('PostController', function ($scope) { 
 
    }); 
 

 
    myApp.controller('AddController', function ($scope) { 
 
    }); 
 

 

 
    myApp.controller('controller', function ($scope, $http) { 
 
     $scope.title = "My app"; 
 
     $http({ 
 
      method: 'GET', 
 
      url: "http://jsonplaceholder.typicode.com/posts" 
 
     }).then(function (response) { 
 
      $scope.posts = response.data; 
 
      $scope.post = response.data[0]; 
 
      $scope.viewby = 9; 
 
      $scope.totalItems = $scope.posts.length; 
 
      $scope.currentPage = 1; 
 
      $scope.itemsPerPage = $scope.viewby; 
 
      $scope.maxSize = 5; 
 
     }); 
 

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

 
     $scope.setItemsPerPage = function (num) { 
 
      $scope.itemsPerPage = num; 
 
      $scope.currentPage = 1; //reset to first page 
 
     }; 
 

 
     $scope.getRowClass = function (index) { 
 
      if (index % 3 === 0) { 
 
       return "row"; 
 
      } 
 
     }; 
 
    });
<!DOCTYPE html> 
 
<html lang="en"> 
 

 
<head> 
 
    <meta charset="utf-8"> 
 
    <title>App</title> 
 
    <meta name="description" content=""> 
 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
 
    <link rel="stylesheet" href="style.css"> 
 
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" 
 
      integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> 
 
    <script src='https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular.js'></script> 
 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular-route.min.js"></script> 
 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular-animate.js"></script> 
 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/2.1.3/ui-bootstrap-tpls.js"></script> 
 
</head> 
 

 
<body layout="column" ng-app="myApp" ng-cloak ng-controller="controller"> 
 
    <h1>{{title}}</h1> 
 
    <a href="#post">Post</a> | 
 
    <a href="#addpost">Add a post</a> 
 
    <script type="text/ng-template" id="allposts.htm"> 
 
     View 
 
     <select ng-model="viewby" ng-change="setItemsPerPage(viewby)"> 
 
      <option>9</option> 
 
      <option>18</option> 
 
      <option>36</option> 
 
      <option>100</option> 
 
     </select> posts 
 
     <div layout="row"> 
 
      <div class="col-sm-4" ng-class="getRowClass($index)" 
 
       ng-repeat="post in posts.slice(((currentPage-1)*itemsPerPage), ((currentPage)*itemsPerPage))"> 
 

 
       <a href="#post">{{post.title}}</a> 
 
       <hr> 
 
       <p>{{post.body}}</p> 
 
      </div> 
 
      <div class="clearfix" ng-if="$index % 3 == 0"></div> 
 

 
     </div> 
 
     <ul uib-pagination total-items="totalItems" ng-model="currentPage" class="pagination-sm" 
 
      items-per-page="itemsPerPage"></ul> 
 

 
    </script> 
 
    <script type="text/ng-template" id="post.htm"> 
 
      </script> 
 
    <script type="text/ng-template" id="addpost.htm"> 
 
      </script> 
 
    <div ng-view></div> 
 
</body> 
 
</html>

+1

$ httpServiceの関数です。 – robert

+0

ありがとうございます、あなたは正しいですが、修正してもうまくいきません – summerfreeze

答えて

2

問題は、アンギュラマテリアルがブートストラップクラスと混在していることです。次の例では、ブートストラップのみを使用しています。

次の問題は、.row要素に.col-xs-4クラスの6つのディビジョンを使用している場合など、6つのディビジョンの高さがすべて同じ場合にのみ自動的に調整されるということです。さもなければ、ビューはあなたの例のように乱れるでしょう。

この問題を解決するには、複数の.row要素を作成する必要があります。そのためには、ビュー自体で行うか、ビューに渡す前にリストを照合するかのいずれかの指示を書くことができます。

次の例では、コントローラ自身でそれをdoignされる(つまりビューに渡す前に):私はあなたが、その後にあなたsetPageで、setItemsPerPageとgetRowClass関数を宣言していると思う

// Helper function to collate a list 
 
Array.prototype.collate = function(collateSize) { 
 
    var collatedList = []; 
 

 
    if (collateSize <= 0) { 
 
     return []; 
 
    } 
 
    angular.forEach(this, function(item, index) { 
 
     if (index % collateSize === 0) { 
 
      collatedList[Math.floor(index/collateSize)] = [item]; 
 
     } else { 
 
      collatedList[Math.floor(index/collateSize)].push(item); 
 
     } 
 
    }); 
 

 
    return collatedList; 
 
}; 
 

 
var myApp = angular.module('myApp', ['ngRoute', 'ui.bootstrap']); 
 

 
myApp.config(function($routeProvider) { 
 
    $routeProvider.when('/', { 
 
     templateUrl: 'allposts.htm', 
 
     controller: 'PostsController' 
 
    }).when('/post', { 
 
     templateUrl: 'post.htm', 
 
     controller: 'PostController' 
 
    }).when('/addpost', { 
 
     templateUrl: 'addpost.htm', 
 
     controller: 'AddController' 
 
    }).otherwise({ 
 
     redirectTo: '/' 
 
    }); 
 
}); 
 

 
myApp.controller('PostsController', function($scope) {}); 
 

 
myApp.controller('PostController', function($scope) {}); 
 

 
myApp.controller('AddController', function($scope) {}); 
 

 

 
myApp.controller('controller', function($scope, $http) { 
 
    $scope.title = "My app"; 
 
    $http({ 
 
     method: 'GET', 
 
     url: "http://jsonplaceholder.typicode.com/posts" 
 
    }).then(function(response) { 
 
     $scope.posts = response.data; 
 
     $scope.viewby = 9; 
 
     $scope.totalItems = $scope.posts.length; 
 
     $scope.currentPage = 1; 
 
     $scope.itemsPerPage = $scope.viewby; 
 
     $scope.maxSize = 5; 
 
     $scope.collatedPosts = getCollatedPosts($scope.posts); 
 
    }); 
 

 
    function getCollatedPosts(posts) { 
 
     if (!posts) { 
 
      return []; 
 
     } 
 

 
     var paginatedPosts = posts.slice((($scope.currentPage - 1) * $scope.itemsPerPage), (($scope.currentPage) * $scope.itemsPerPage)); 
 
     return paginatedPosts.collate(3); 
 
    } 
 

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

 
    $scope.setItemsPerPage = function(num) { 
 
     $scope.itemsPerPage = num; 
 
     $scope.currentPage = 1; //reset to first page 
 
     $scope.collatedPosts = getCollatedPosts($scope.posts); 
 
    }; 
 

 
    $scope.pageChanged = function(currentPage) { 
 
     $scope.currentPage = currentPage; 
 
     $scope.collatedPosts = getCollatedPosts($scope.posts); 
 
    }; 
 
});
.row { 
 
    /* Using red border just for understanding */ 
 
    border-bottom: 2px solid red; 
 
    margin-bottom: 10px; 
 
    margin-top: 10px; 
 
}
<html> 
 

 
<head> 
 
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> 
 
    <script src='https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular.js'></script> 
 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular-route.min.js"></script> 
 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular-animate.js"></script> 
 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/2.1.3/ui-bootstrap-tpls.js"></script> 
 
</head> 
 

 
<body layout="column" ng-app="myApp" ng-cloak ng-controller="controller"> 
 
    <h1>{{title}}</h1> 
 
    <a href="#post">Post</a> | 
 
    <a href="#addpost">Add a post</a> 
 
    <script type="text/ng-template" id="allposts.htm"> 
 
     View 
 
     <select ng-model="viewby" ng-change="setItemsPerPage(viewby)"> 
 
      <option>9</option> 
 
      <option>18</option> 
 
      <option>36</option> 
 
      <option>100</option> 
 
     </select>posts 
 
     <div class="row" ng-repeat="collatedPostList in collatedPosts"> 
 
      <div class="col-xs-4" ng-repeat="post in collatedPostList"> 
 

 
       <a href="#post">{{post.title}}</a> 
 
       <hr> 
 
       <p>{{post.body}}</p> 
 
      </div> 
 

 
     </div> 
 
     <ul uib-pagination total-items="totalItems" ng-model="currentPage" class="pagination-sm" 
 
      items-per-page="itemsPerPage" ng-change="pageChanged(currentPage)"></ul> 
 

 
    </script> 
 
    <script type="text/ng-template" id="post.htm"> 
 
    </script> 
 
    <script type="text/ng-template" id="addpost.htm"> 
 
    </script> 
 
    <div ng-view></div> 
 
</body> 
 

 
</html>

+0

ありがとうございます、それは素敵なグリッドになりますが、ページネーションは機能しなくなりました。 – summerfreeze

+0

それを修正していた。それは範囲の問題です。更新された答えを今すぐチェックしてください。 –

+0

例をフルページで実行すると、正しく表示されます。私はより明確にするために、境界線の色を赤に変更しました。 –

0

あなたが適切にあなたの変数に基づいて必要な行の合計数を計算するために、あなたの「行」のdivにNGリピートを追加する必要があります。実際にng-repeatを使って何かを出力する必要はありません。配列を使用して必要な要素数を決定してください。次に、現在のng-repeatを調整して、必要な列数の正しい開始位置から配列を適切にスライスします。

関連する問題