简体   繁体   English

Ionic + AngularJS:ng-repeat过滤器不适用于网格布局,但可以在列表中使用

[英]Ionic + AngularJS: ng-repeat filter not working on grid layout but works in list

I am currently learning building apps with ionic v1 and angular. 我目前正在学习使用ionic v1和angular构建应用程序。

Here's the scenario, I'm trying to make a grid of cards and was able to do it by following the tutorial on this link . 在这种情况下,我正在尝试制作一张纸牌,并能够按照此链接上的教程进行操作。

I was able to populate the grid with cards using json data. 我能够使用json数据在卡片上填充网格。 However, I also need to add a search box in order to filter the results. 但是,我还需要添加一个搜索框以过滤结果。 I used angularJS ng-repeat filter on the view but it is not filtering it correctly. 我在视图上使用了angularJS ng-repeat过滤器,但是过滤不正确。

here's my code: 这是我的代码:

 angular.module('starter')

.factory('Disasters', function() {
   var disasters= [{
       id: 0,
       name: 'Earthquake',
       face: 'img/earthquake/thumbs.PNG',
   }, {
      id: 1,
      name: 'Flash Floods',
      face: 'img/flood/thumbs.png',
   }, {
      id: 2,
      name: 'Typhoon',
      face: 'img/typhoon/thumbs.png',
   }, {
      id: 3,
      name: 'Avalanche',
      face: 'img/avalanche/thumbs.png',
   }];

   return {
     all: function() {
         return disasters;
     },
     remove: function(disaster) {
         disasters.splice(disasters.indexOf(disaster), 1);
     },
     get: function(disasterID) {
        for (var i = 0; i < disasters.length; i++) {
           if (disasters[i].id === parseInt(disasterID)) {
        return disasters[i];
     }
   }
   return null;
  }
 };
})


.controller('DisasterCtrl', function($scope,$state,$ionicSideMenuDelegate,Chats,Facts){

  $scope.disasters = Disasters.all();
})

and here's the code for my view 这是我的看法的代码

<ion-content>
  <input type="text" ng-model="search">
  <div ng-repeat="disaster in disasters | filter: search" ng-if="$index % 2 === 0">
    <div class="row">
      <div class="col col-50 no-padding" ng-if="$index < disasters.length">
      <div class="list card card-cell">
        <div class="item item-image background-image" back-img="{{disasters[$index].slider[0].image}}">
          </div>
        <div class="item item-avatar" ng-click="href({{disasters[$index].id}})">
          <p class="bold uppercase font12">{{disasters[$index].name}}</p>
        </div>
      </div>
    </div>

    <div class="col col-50 no-padding" ng-if="$index < disasters.length">
      <div class="list card card-cell">
        <div class="item item-image background-image" back-img="{{disasters[$index + 1].slider[0].image}}">
          </div>
        <div class="item item-avatar" ng-click="href({{disasters[$index + 1].id}})">
          <p class="bold uppercase font12">{{disasters[$index + 1].name}}</p>
        </div>
      </div>
    </div>
    </div>

  </div>     
</ion-content>

when I type something on the search text box it always shows the first two object in the JSON data. 当我在搜索文本框中键入内容时,它始终会在JSON数据中显示前两个对象。

If I change the view into a list the filter works, however it is required to apply the grid styling on the view. 如果将视图更改为列表,则过滤器可以工作,但是需要在视图上应用网格样式。

Thanks! 谢谢!

Pluker link of the code : plnk code link 代码的Pluker链接: plnk代码链接

The problem with your grid layouting is that you are using the ng-repeat in a row instead of the columns so when the search item is among a particular it returns the full row because thats where your item is located. 网格布局的问题是,您在一行中而不是在列中使用ng-repeat,因此当搜索项在特定项之中时,它会返回整行,因为那是您项所在的位置。 so i created a code to help you sort it in plnk where i used the ng-repeat in a column instead of the row and added a style to the row to make it break which is this 所以我创建了一个代码来帮助您在plnk中对其进行排序,在其中我在列而不是行中使用了ng-repeat,并在行中添加了样式以使其中断,这就是

class="row" style="flex-wrap: wrap;" class =“ row” style =“ flex-wrap:wrap;”

    Input: <input type="text" ng-model="search.name" style="border:1px solid #ccc">   

<br> 
<div class="row" style="flex-wrap: wrap;" >
  <div class="col col-50 no-padding" ng-repeat="disaster in disasters | filter: search" >
      {{$index}}
      <div class="list card card-cell">
        <div class="item item-image background-image" back-img="{{disaster.slider[0].image}}">
          </div>
        <div class="item item-avatar" ng-click="href({{disaster.id}})">
          <p class="bold uppercase font12">{{disaster.name}}</p>
        </div>
      </div>

    </div>
</div>

That should work for you as needed. 这应该为您工作所需。 here is the demo running demo reference from this answer 这是这个答案的演示运行演示参考

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

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