簡體   English   中英

如何使用AngularJS將以標題為第一個字符的聯系人列表按字母順序排序?

[英]How do I use AngularJS to alphabetize a contact list with headers as the first character?

我有這樣的清單

一個人

APerson2

BPerson

我希望它看起來像這樣

一種

Aperson Aperson2

Bperson

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

directory.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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM