javascript - 如何使用 AngularJS 将标题作为第一个字符的联系人列表按字母顺序排列?

标签 javascript angularjs angularjs-ng-repeat angularjs-filter contact-list

我有一个这样的列表

一个人

人物

一个人2

B人

我希望它看起来像这样

一个

一个人 个人2

B

B人

C

人物

我在这里找到了一篇解释它的帖子: http://pmosd.blogspot.com/2013/07/headers-in-angularjs-ng-repeats.html

但作为 Angular 新手,我不确定将函数和过滤器放在 js 文件中的什么位置以及如何将其正确编码到我的 html 中。我尝试关注我链接的帖子,但我的联系人姓名无序列表不会显示在浏览器中。这是我当前的 JS 和 HTML 文件。我曾尝试将过滤器和功能放在不同的地方,但这是我最近的尝试。

Directory.js:

'use strict';

var myApp = angular.module('myappname');

myApp.controller('DirectoryCtrl', function ($scope) {
    $scope.contacts = [{
        'fname': 'Randy',
        'lname': 'Johnson',
        'location': 'Seattle, WA'
    }, {
        'fname': 'Andy',
        'lname': 'Pettite',
        'location': 'Bronx, NY'
    }, {
        'fname': 'Jon',
        'lname': 'Lester',
        'location': 'Boston, MA'
    }];

});


myApp.filter("headerChunk", function () {
    return function (orig, same, getChunkID) {
        if (!(orig instanceof Array)) return orig;
        if (orig.length == 0) return orig;

        var result = [];

        var cur = [];

        var i = 0
        for (i = 0; i < orig.length; i++) {
            if (i == 0 || same(orig[i], orig[i - 1])) {
                cur.push(orig[i]);
            } else {
                result.push({
                    id: getChunkID(orig[i - 1]),
                    items: cur
                });

                cur = [orig[i]];
            }
        }

        result.push({
            id: getChunkID(orig[orig.length - 1]),
            items: cur
        });

        for (i = 0; i < result.length; i++) {
            result[i].$$hashKey = i;
        }

        return result;
    }
});

$scope.filteredData = $filter("headerChunk")(
    $scope.filteredData,

    // The first argument is a function 'same' that is responsible for determining
    // whether two objects of the collection belong in the same category. We desire
    // two persons to go in the same category is their names start with the same first
    // letter:
    function (p1, p2) {
        return (p1.name.charAt(0) == p2.name.charAt(0));
    },

    // The second function 'getChunkID' is responsible for "naming" the resulting chunk.
    // We wish for the chunks to be identified by the letter that their constituents'
    // names start with:
    function (p) {
        return p.name.charAt(0);
    }
);

目录.html:

<div ng-repeat="group in contacts | orderBy:name | headerChunk:sameFunc:idFunc">
<ul>
    <li ng-repeat="item in group.items">
      {{item.lname}}, {{item.fname}}<br>
      {{item.location}}
        <hr>
    </li>
</ul>

不知道该做什么了。

最佳答案

你看评论了吗?第一个人是用 underscore.js 做的,所以用 underscore.js 或 lodash.js 做。

// include the script
<script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.min.js"></script>


<div ng-repeat="(k,v) in sortedcontacts">
<span>{{k}}</span>
<ul>
    <li ng-repeat="item in v">
      {{item.lname}}, {{item.fname}}<br>
      {{item.location}}
        <hr>
    </li>
</ul>
</div>



'use strict';

var myApp = angular.module('myappname');

myApp.controller('DirectoryCtrl', function ($scope) {
$scope.contacts = [
    {'fname': 'Randy',
     'lname':'Johnson',
     'location': 'Seattle, WA'},
    {'fname': 'Andy',
     'lname':'Pettite',
     'location': 'Bronx, NY'},
    {'fname': 'Jon',
     'lname':'Lester',
     'location': 'Boston, MA'}
]; 

$scope.sortedcontacts = _.groupBy($scope.contacts, function(item) {return item.fname[0]; });
});

// you don't need those filter codes

关于javascript - 如何使用 AngularJS 将标题作为第一个字符的联系人列表按字母顺序排列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22481918/

相关文章:

javascript - 刷新 Angular 日期时间选择器

node.js - 如何使用 Node/Express 和 Angular 将 JSON 对象从服务器发送到我的 View

AngularJS 在嵌套的 ngRepeat 中访问 $first of ngRepeat parent

javascript - 推送到数组不会通过 ng-repeat 指令更新 View 列表

javascript - 平滑的页面滚动不适用于所有语言(即更改的 URL 结构)

javascript - 如何在 JavaScript 中提取 BigInt 的 n 次方根?

javascript - 如何链接多个 jquery fancybox?

php - Zend 框架 : How can I add JavaScript element after the scripts in head?

angularjs - $http get 返回与 $resource get 不同

javascript - 如何使用 ng-repeat 迭代使用动态键关联的数组