Filter not working ng-repeat

ぃ、小莉子 提交于 2019-12-25 05:48:09

问题


I'm using Salvattore (masonry like grid) in my angular app but the filter option in ng-repeat does not work. I think it's because Salvattore wraps each repeated item in a separate div to make a grid (in my example it's div.col-4)

<input type="text" ng-model="search">
<div class="row grid" salvattore>
  <div class="entry" ng-repeat="e in data | filter:search">
    {{e}}
  </div>
</div>

The output of the above is similar to the following:

<div class="row grid" salvattore="" data-columns="3">
  <div class="col-4">
    <div class="entry">e</div>
    <div class="entry">e</div>
  </div>
  <div class="col-4">
    <div class="entry">e</div>
    <div class="entry">e</div>
  </div>
  <div class="col-4">
    ..
  </div>
</div>

I think this is what causes the problem with filtering... but I'm not really sure to be honest :)

The question is: What should I do to make it work? Do I have to crate a custom filter?

I've created a codepen for this: http://codepen.io/anon/pen/MpebNV

Thanks.


回答1:


So the salvattore incompatible with angular. You can write a directive to implement its function. Or use jquery to implement a "filter" function.




回答2:


I tried to write my own directive and it seems to be working quite good. I've added the following to the salvattore directive.

app.directive('salvattore', function($timeout, $window,$filter) {
  return {
    restrict: 'A',
    link: function(scope, element, attrs) {

      // First load (does not work without it
      $timeout(function(){
        element.attr('data-columns', true);
        $window.salvattore.register_grid(element[0]);            
      },0);



    scope.$watch('search', function(newValue, oldValue) {


    // I needed this hack because the grid didn't render properly when $scope.search was empty
    if(newValue=='') {
      var filteredData = scope.data;
      var items = '';
      jQuery.each(filteredData, function(index, entry){
        var ITEM_TEMPLATE = '<div class="entry">{{e}}</div>';
          var content = ITEM_TEMPLATE.replace(/\{\{(\w+)\}\}/g, function (match, g1) {
            switch (g1) {
              case 'e': return entry; break;
            }
          });
          items = items.concat(content);
      });
      jQuery('.grid').html(items);
      $window.salvattore.register_grid(element[0]);
    }


    if (newValue) {
      var filteredData = $filter('filter')(scope.data, scope.search);
      var items = '';
      jQuery.each(filteredData, function(index, entry){
        var ITEM_TEMPLATE = '<div class="entry">{{e}}</div>';
          var content = ITEM_TEMPLATE.replace(/\{\{(\w+)\}\}/g, function (match, g1) {
            switch (g1) {
              case 'e': return entry; break;
            }
          });
          items = items.concat(content);
      });
      jQuery('.grid').html(items);
      $window.salvattore.register_grid(element[0]);
    }

}, true);


    }
  };
});

I know it looks primitive but it is what it is, but I also know it can be simpler. Maybe someone can check this and improve a little bit, please?

Here's codepen: http://codepen.io/anon/pen/MpebNV



来源:https://stackoverflow.com/questions/42595787/filter-not-working-ng-repeat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!