AngularJS Group By Directive without External Dependencies

前端 未结 9 987
长发绾君心
长发绾君心 2020-11-29 01:01

I\'m new to Angular and would like to learn the best way to handle a problem. My goal is to have a reusable means to create group by headers. I created a solution which wor

9条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-29 01:20

    This is a modification of Darryl's solution above, that allows multiple group by parameters. In addition it makes use of $parse to allow the use of nested properties as group by parameters.

    Example using multiple, nested parameters

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

    HTML

    Multiple Grouping Parameters

    {{item.groupfield}} {{item.deep.category}}

    • {{item.whatever}}

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

提交回复
热议问题