如何将单一品牌的所有产品组合在一起——angular JS
How to group all product of single brand together - angular JS
请看这个JS小提琴链接。
http://jsfiddle.net/4Dpzj/174/这是group by
的逻辑app.filter('groupBy', ['$parse', function ($parse) {
return function (list, group_by) {
var filtered = [];
var prev_item = null;
var group_changed = false;
// this is a new field which is added to each item where we append "_CHANGED"
// to indicate a field change in the list
//was var new_field = group_by + '_CHANGED'; - JB 12/17/2013
var new_field = 'group_by_CHANGED';
// loop through each item in the list
angular.forEach(list, function (item) {
group_changed = false;
// if not the first item
if (prev_item !== null) {
// check if any of the group by field changed
//force group_by into Array
group_by = angular.isArray(group_by) ? group_by : [group_by];
//check each group by parameter
for (var i = 0, len = group_by.length; i < len; i++) {
if ($parse(group_by[i])(prev_item) !== $parse(group_by[i])(item)) {
group_changed = true;
}
}
}// otherwise we have the first item in the list which is new
else {
group_changed = true;
}
// if the group changed, then add a new field to the item
// to indicate this
if (group_changed) {
item[new_field] = true;
} else {
item[new_field] = false;
}
filtered.push(item);
prev_item = item;
});
return filtered;
};
我想把所有的产品归在一起。
我需要做哪些更改?
我是这么想的。不使用任何自定义过滤器
我只是使用这个ng-repeat语法:
ng-repeat="(key,item) in MyList | orderBy:orderKey"
由于它,我可以得到键值与前一个对象的值进行比较。
这是我的ng-show属性。可以通过在其他地方(如控制器中)对列表进行排序来改进
<h2 ng-show="(MyList | orderBy:orderKey)[key-1][orderKey] !== (MyList | orderBy:orderKey)[key][orderKey]"
多亏了这个,你可以用你的任何属性名填充你的var "orderKey",这将工作。
查看它在这个JSFiddle中的工作
希望有帮助。
编辑:我认为使用一个临时列表来管理视觉顺序会更简洁一些(参见这个JSFiddle):
JS:
$scope.orderList = function(){
$scope.orderedList = $filter('orderBy')($scope.MyList,$scope.orderKey);
}
HTML: ng-change="orderList()"
触发列表排序
清洁器ng-repeat/ng-show
<div ng-repeat="(key,item) in orderedList">
<h2 ng-show="orderedList[key-1][orderKey] !== orderedList[key][orderKey]">{{item[orderKey]}} </h2>
<ul>
<li>{{item.ProductName}}</li>
</ul>
</div>
看看这个:http://jsfiddle.net/4Dpzj/176/
<div ng-repeat="item in MyList | orderBy:['SubCategoryName','BrandName'] | groupBy:['SubCategoryName']" >
<h2 ng-show="item.group_by_CHANGED">{{item.SubCategoryName}} </h2>
<ul>
<li>{{item.ProductName}} --- {{item.BrandName}}</li>
</ul>
</div>
相关文章:
- 如何将具有相同功能的两个select html标签的两个JS组合在一起
- 不同对象之间的递归,并将它们唯一地组合在一起,不重复
- 在ASP.NET MVC3中将多个视图组合在一起
- 将json值组合在一起并创建csv
- Meteor 与 Session.set 和 jQuery 在一个调用中组合在一起
- jQuery、jQuery UI和jQuery Mobile如何组合在一起
- 是否可以将相同属性的元素与 querySelectorAll 组合在一起
- 在 jQuery 中将多个函数与相同的代码组合在一起
- 将图像组合在一起并在窗口加载时对图像进行动画处理
- 将“if/then.else”语句与“switch”语句组合在一起
- JQuery - 将所选元素添加在一起 - 组合子元素
- 如何将两个函数的代码与不同的参数组合在一起
- 是否可以在javascript中将for循环与setTimeOut组合在一起
- 将两个正则表达式组合在一起
- 在 JavaScript 中用多个分隔符拆分公式字符串,并保留分隔符,然后将字符串重新组合在一起
- 按字母表列出,将条目较少的字母组合在一起(PHP或JS)
- 将两个对象与多个节点组合在一起——为什么这段代码有效
- 将这些脚本组合在一起工作,Javascript/AAJAX和PHP
- 在PaperJS中,可以将多个项目组合在一起,使它们作为一个项目移动
- jQuery循环将多个数组组合在一起-Mapbox-map.setFilter()