栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

AngularJS按指令分组,没有外部依赖性

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

AngularJS按指令分组,没有外部依赖性

这是对以上Darryl解决方案的修改,它允许使用多个by group参数。另外,它使用$ parse允许将嵌套属性用作按参数分组。

使用多个嵌套参数的示例

http://jsfiddle.net/4Dpzj/6/

的HTML

<h1>Multiple Grouping Parameters</h1><div ng-repeat="item in MyList  | orderBy:'groupfield' | groupBy:['groupfield', 'deep.category']">    <h2 ng-show="item.group_by_CHANGED">{{item.groupfield}} {{item.deep.category}}</h2>     <ul>        <li>{{item.whatever}}</li>     </ul></div>

过滤器(Javascript)

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;    };}]);


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/484434.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号