筛选器无法正常执行ng-repeat [英] Filter not working ng-repeat
问题描述
我在棱角分明的应用程序中使用 Salvattore ng-repeat
中的过滤器选项不起作用.
我认为这是因为 Salvattore 将每个重复的项目包装在单独的div
中以形成网格(在我的示例中是div.col-4
)
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 :)
问题是: 我应该怎么做才能使其正常工作?我必须创建一个自定义过滤器吗?
我为此创建了一个代码笔: http://codepen.io/anon/pen/MpebNV
I've created a codepen for this: http://codepen.io/anon/pen/MpebNV
谢谢.
推荐答案
我尝试编写自己的指令,它似乎运行得很好. 我在salvattore指令中添加了以下内容.
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?
这里是代笔: http://codepen.io/anon/pen/MpebNV
这篇关于筛选器无法正常执行ng-repeat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!