# Generating list with dividers dynamically

**URL:** <https://forum.ionicframework.com/t/generating-list-with-dividers-dynamically/341>\
**Category:** ionic-v1\
**Created:** [December 25, 2013, 9:19pm UTC](https://forum.ionicframework.com/t/generating-list-with-dividers-dynamically/341 "2013-12-25T21:19:12Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![pmirshad](https://avatars.discourse-cdn.com/v4/letter/p/977dab/32.png) [@pmirshad](https://forum.ionicframework.com/u/pmirshad)\
**Post date:** [December 25, 2013, 9:19pm UTC](https://forum.ionicframework.com/t/generating-list-with-dividers-dynamically/341/1 "2013-12-25T21:19:12Z")

</div>

I’m just getting started with AngularJS and ionic. Is there a way to generate a list with dividers dynamically from an array of objects, each having a boolean to denote if that item is a divider or not ?

---

<div class="post-metadata">

**Author:** ![adam](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/adam/32/63947_2.png) [@adam](https://forum.ionicframework.com/u/adam)\
**Post date:** [December 26, 2013, 3:30pm UTC](https://forum.ionicframework.com/t/generating-list-with-dividers-dynamically/341/2 "2013-12-26T15:30:48Z")

</div>

At this time no, but that’s not a bad idea. I’ll add it to our issues: [https://github.com/driftyco/ionic/issues/348](https://github.com/driftyco/ionic/issues/348)

---

<div class="post-metadata">

**Author:** ![pmirshad](https://avatars.discourse-cdn.com/v4/letter/p/977dab/32.png) [@pmirshad](https://forum.ionicframework.com/u/pmirshad)\
**Post date:** [December 26, 2013, 3:48pm UTC](https://forum.ionicframework.com/t/generating-list-with-dividers-dynamically/341/3 "2013-12-26T15:48:41Z")

</div>

Thanks Adam. Appreciate it.

---

<div class="post-metadata">

**Author:** ![MarvinFLC](https://avatars.discourse-cdn.com/v4/letter/m/73ab20/32.png) [@MarvinFLC](https://forum.ionicframework.com/u/MarvinFLC)\
**Post date:** [January 24, 2014, 1:05pm UTC](https://forum.ionicframework.com/t/generating-list-with-dividers-dynamically/341/4 "2014-01-24T13:05:05Z")

</div>

Hey guys,

ive been testing this all morning and I cant figure out how to combine this technique with a ng-repeat. Adding the class doesn’t override the href. So this technique clashes with dynamic population of lists. In the github issue it states that the issue is closed.

---

<div class="post-metadata">

**Author:** ![Calendee](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/calendee/32/13656_2.png) [@Calendee](https://forum.ionicframework.com/u/Calendee)\
**Post date:** [January 24, 2014, 2:29pm UTC](https://forum.ionicframework.com/t/generating-list-with-dividers-dynamically/341/5 "2014-01-24T14:29:12Z")

</div>

Instead of trying to get the GUI portion of your app to do this, why not use JavaScript to accomplish it? I have a similar scenario in my app. I display a user’s entire Address Book in a list. I needed the first letter dividers as you see in iOS address book.

So, I collect all the contacts (using PhoneGap API’s), then I iterate through them adding them to an object that has the first letters as properties.

Then, I use 2 `ng-repeats`. One repeats for the first letters in the contacts object. Inside there, I repeat for each actual contact.

Example :

```auto
var contacts = [ 
	{'givenName' : 'Lisa',	'familyName' : 'Adams'	},
	{'givenName' : 'Bob',	'familyName' : 'Arnet'	},
	{'givenName' : 'Frank',	'familyName' : 'Able'	},
	{'givenName' : 'John',	'familyName' : 'Crow'	},
	{'givenName' : 'Bill',	'familyName' : 'Ward'	}
];

var $scope.contacts = {};

var contactsLength = contacts;
var firstLetter;

for(var i = 0; i < contactsLength; i++) {
	firstLetter = contacts[i].familyName.substring(0,1).toUpperCase();

	if(!$scope.contacts[firstLetter]) $scope.contacts[firstLetter] = [];

	$scope.contacts[firstLetter].push ( contacts[i].givenName + ' ' + contacts[i].familyName );
}

```

Then, I end up with something that looks like this:

```auto
$scope.contacts = {
	'A' : [
		'Lisa Adams',
		'Bob Arnet',
		'Frank Able'
	],
	'C' : [
		'John Crow'
	],
	'W' : [
		'Bill Ward'
	]
}

```

In your HTML, do something like this:

```auto
<div class="list">

    <div class="item item-divider"
        ng-repeat-start="(firstLetter, contacts) in fc.contacts">
        {{firstLetter}}
    </div>

    <div class="item"
         ng-repeat="contact in contacts"
         bindonce
         ng-click="contactSelected(contact.id)">{{ contact }}
    </div>

    <div ng-repeat-end></div>

</div>

```

You end up with this:  
 ![](https://sea1.discourse-cdn.com/ionicframework/uploads/default/62/2c61ccfe32dc65fb.PNG)

---

<div class="post-metadata">

**Author:** ![MarvinFLC](https://avatars.discourse-cdn.com/v4/letter/m/73ab20/32.png) [@MarvinFLC](https://forum.ionicframework.com/u/MarvinFLC)\
**Post date:** [January 24, 2014, 2:40pm UTC](https://forum.ionicframework.com/t/generating-list-with-dividers-dynamically/341/6 "2014-01-24T14:40:41Z")

</div>

That looks totally feasible, our data structure already has the items listed with a character so I didnt consider this option. Thanks!

---

<div class="post-metadata">

**Author:** ![thomasvdbossche](https://avatars.discourse-cdn.com/v4/letter/t/a183cd/32.png) [@thomasvdbossche](https://forum.ionicframework.com/u/thomasvdbossche)\
**Post date:** [March 10, 2014, 10:15pm UTC](https://forum.ionicframework.com/t/generating-list-with-dividers-dynamically/341/7 "2014-03-10T22:15:50Z")

</div>

Thanks for the example! But I was wondering how to make the items link to another page? Because of the structure of $scope.contacts, I am unable to retrieve the id. Thanks to the push command, room only exists with no properties:

```
href="#/tab/room/{{room.id}}">{{room}}

```

Thanks to the push command, room only exists and represents the name-property we pushed in the controller-section.  
Is it possible to redirect using ng-click like in your example?

Thanks

---

<div class="post-metadata">

**Author:** ![Calendee](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/calendee/32/13656_2.png) [@Calendee](https://forum.ionicframework.com/u/Calendee)\
**Post date:** [March 11, 2014, 5:46am UTC](https://forum.ionicframework.com/t/generating-list-with-dividers-dynamically/341/8 "2014-03-11T05:46:15Z")

</div>

I’m not sure I’m understanding room vs contact and whether or not you have an id, but you can still link as much as you like.

This was written a good while ago. I don’t think I was using ui-router when I show this example. I’m assuming you are using a more recent version of Ionic.

So, you can do this two ways:

ng-click method :

```auto
<div class="list">
   <a class="item" ng-repeat="room in rooms" ng-click="goToRoom(room)">{{room}}</a>
</div>

```

ui-router method : See : [http://angular-ui.github.io/ui-router/site/#/api/ui.router.state.directive:ui-sref](http://angular-ui.github.io/ui-router/site/#/api/ui.router.state.directive:ui-sref)

```auto
<div class="list">
   <a class="item" ng-repeat="room in rooms" ui-sref="rooms({ "roomName" : room})">{{room}}</a>
</div>

```

---

<div class="post-metadata">

**Author:** ![thomasvdbossche](https://avatars.discourse-cdn.com/v4/letter/t/a183cd/32.png) [@thomasvdbossche](https://forum.ionicframework.com/u/thomasvdbossche)\
**Post date:** [March 11, 2014, 8:09am UTC](https://forum.ionicframework.com/t/generating-list-with-dividers-dynamically/341/9 "2014-03-11T08:09:37Z")

</div>

Thanks for the reply! I’m using rooms instead of contacts but the principle remains the same.

The problem I have is that there is no way I can access the room id; before I used your code example, I just used `$scope.rooms = rooms;`  
in my controller so I could access all the properties of a room (id, name, capacity).

But when I use your code example to generate my lists with dividers dynamically, I am unable to access this properties. Because of the command `$scope.rooms[firstLetter].push(rooms[i].name)`  
the room property in my html directly refers to the name which means [room.id](http://room.id) doesn’t exist.

I need the [room.id](http://room.id) to redirect to a specific page when a user click on a room.  
I hope I was able to explain my problem.

---

<div class="post-metadata">

**Author:** ![elm](https://avatars.discourse-cdn.com/v4/letter/e/57b2e6/32.png) [@elm](https://forum.ionicframework.com/u/elm)\
**Post date:** [March 11, 2014, 8:36am UTC](https://forum.ionicframework.com/t/generating-list-with-dividers-dynamically/341/10 "2014-03-11T08:36:31Z")

</div>

You just need to change this:

```
$scope.rooms[firstLetter].push(rooms[i].name)

```

to this:

```
$scope.rooms[firstLetter].push(rooms[i])

```

et voilà there you have your room with all it’s properties.

---

<div class="post-metadata">

**Author:** ![fdedeh](https://avatars.discourse-cdn.com/v4/letter/f/c37758/32.png) [@fdedeh](https://forum.ionicframework.com/u/fdedeh)\
**Post date:** [June 2, 2014, 12:48am UTC](https://forum.ionicframework.com/t/generating-list-with-dividers-dynamically/341/11 "2014-06-02T00:48:19Z")

</div>

Hi all!..

I’m resurrecting the post to ask you if someone have an idea about how to apply list divider based in months/year…

for example, I have a payment list, and want to use the dividers to separate the payments by month/year:

* * *

## **june 2014**

customer 1 _06/01/2014_

* * *

## **may 2014**

customer 1 _05/25/2014_

customer 2 _05/20/2014_

* * *

**april 2014**

* * *

customer 1 _04/22/2014_

customer 2 _04/19/2014_

* * *

I would like to thank you since now for your attention…

SOLVED!

Just used the solution given by @Calendee … thank you dude!..

```
    var payments = [ 
        		{'name' : 'Lisa Simpson',	'id' : '81', 'value' : '$ 25,00', 'date' : '25/03/2014', 'month' : '03', 'year' : '2014'	},
        		{'name' : 'John Kennedy',	'id' : '83', 'value' : '$ 25,00', 'date' : '15/03/2014', 'month' : '03', 'year' : '2014'	},
        		{'name' : 'Michael J. Fox',	'id' : '77', 'value' : '$ 25,00', 'date' : '12/04/2014', 'month' : '04', 'year' : '2014'	},
        		{'name' : 'Aaron Dobson',	'id' : '65', 'value' : '$ 25,00', 'date' : '10/02/2014', 'month' : '02', 'year' : '2014'	},
        		{'name' : 'Julian Edelman',	'id' : '88', 'value' : '$ 25,00', 'date' : '07/02/2014', 'month' : '02', 'year' : '2014'	}
        			];
$scope.payments = {};
var month;

for(var i = 0; i < payments.length; i++) {
	month = payments[i].month +'/'+ payments[i].year;
				

	if(!$scope.payments[month]) $scope.payments[month] = [];

	$scope.payments[month].push ( payments[i].name + ' ' + payments[i].date );
}

```

the view:

```
<div class="list">

	<div class="item item-divider"
		ng-repeat-start="(month, payments) in payments">
					{{month}}
	</div>

	<div class="item"
		ng-repeat="payment in payments"
		ng-click="contactSelected(contact.id)">{{ payment }}
	</div>

	<div ng-repeat-end></div>
</div>

```

---

<div class="post-metadata">

**Author:** ![als](https://avatars.discourse-cdn.com/v4/letter/a/e68b1a/32.png) [@als](https://forum.ionicframework.com/u/als)\
**Post date:** [October 31, 2014, 1:17am UTC](https://forum.ionicframework.com/t/generating-list-with-dividers-dynamically/341/12 "2014-10-31T01:17:45Z")

</div>

This worked great for me, the only difference is that I took a number from the array and made it my key like so:

```
{ '1': 
 [ { name: 'BCS McZainasheff\'s Wee', id: '71' },
 { name: 'Slurry Fluerious', id: '72' },
 { name: 'English Mild Brown Ale', id: '73' },
 { name: 'Sparkling Cider', id: '74' },
 { name: 'Unicorn Blood', id: '75' },
 { name: 'ZK experiments 1, 2, 3', id: '76' } ],
 '2': 
 [ { name: '10w30', id: '85' },
 { name: 'Not Old Chubb', id: '86' },
 { name: 'Lupulin XX', id: '87' } ],
'11': 
 [ { name: 'Buddy\'s Award Winning Pumpkin Spice Ale', id: '118' },
 { name: 'No Passport Needed', id: '119' },
 { name: 'White IPA', id: '120' } ],

```

Unfortunately it ng-repeat puts these out of order and I cannot for the life of me figure out how to fix it. Any help?

(ng-repeat puts it in this order: 1,11,12, etc)

---

<div class="post-metadata">

**Author:** ![techrover\_user](https://avatars.discourse-cdn.com/v4/letter/t/dbc845/32.png) [@techrover\_user](https://forum.ionicframework.com/u/techrover_user)\
**Post date:** [September 24, 2015, 6:45am UTC](https://forum.ionicframework.com/t/generating-list-with-dividers-dynamically/341/13 "2015-09-24T06:45:58Z")

</div>

hello sir what is fc.contacts ?

---

<div class="post-metadata">

**Author:** ![rohitaneja8](https://avatars.discourse-cdn.com/v4/letter/r/71e660/32.png) [@rohitaneja8](https://forum.ionicframework.com/u/rohitaneja8)\
**Post date:** [July 14, 2016, 9:38am UTC](https://forum.ionicframework.com/t/generating-list-with-dividers-dynamically/341/14 "2016-07-14T09:38:19Z")

</div>

Wow, very well explained! Thanks

---

<div class="post-metadata">

**Author:** ![rohitaneja8](https://avatars.discourse-cdn.com/v4/letter/r/71e660/32.png) [@rohitaneja8](https://forum.ionicframework.com/u/rohitaneja8)\
**Post date:** [July 14, 2016, 9:39am UTC](https://forum.ionicframework.com/t/generating-list-with-dividers-dynamically/341/15 "2016-07-14T09:39:17Z")

</div>

Ignore “fc” it will be only contacts…

---

<div class="post-metadata">

**Author:** ![rohitaneja8](https://avatars.discourse-cdn.com/v4/letter/r/71e660/32.png) [@rohitaneja8](https://forum.ionicframework.com/u/rohitaneja8)\
**Post date:** [July 14, 2016, 9:39am UTC](https://forum.ionicframework.com/t/generating-list-with-dividers-dynamically/341/16 "2016-07-14T09:39:44Z")

</div>

same here as well !!

---

<div class="post-metadata">

**Author:** ![diamond\_dunn](https://avatars.discourse-cdn.com/v4/letter/d/5e9695/32.png) [@diamond\_dunn](https://forum.ionicframework.com/u/diamond_dunn)\
**Post date:** [April 11, 2017, 11:47am UTC](https://forum.ionicframework.com/t/generating-list-with-dividers-dynamically/341/17 "2017-04-11T11:47:02Z")

</div>

Does @Calendee solution still work?

```
splitAlphabetically() {

    var firstLetter;

    for (var i = 0; i < this.activeUsersCount; i++) 
    {
      firstLetter = this.activeUsers[i].user_email.substring(0, 1).toUpperCase();

      if (!this.filteredContacts[firstLetter])
      {
        this.filteredContacts[firstLetter] = [];
      }

      this.filteredContacts[firstLetter].push(this.activeUsers[i]);
    }
  }

```

which displays my filteredContacts array alphabetically as below,

```
Object {A: Array(2), B: Array(1), D: Array(1)}
A : Array(2)
  0 : Object
    user_email : "admin@gmail.com"
  1 : Object
    user_email : "admintwo@gmail.com"
B : Array(1)
  0 : Object
    user_email : "bob@gmail.com"
D : Array(1)
  0 : Object
    user_email : "dave@gmail.com"

```

But when I try to populate on the html side, it can’t find user\_email?

```
<ion-list-header ng-repeat-start="(firstLetter, contacts) in filteredContacts">
  {{firstLetter}}
</ion-list-header>
<ion-item-sliding ng-repeat="contact in contacts">
	<ion-item>
		<h2>{{contact.user_email}}</h2>
	</ion-item>

```

Can anyone help?

---

<div class="post-metadata">

**Author:** ![thefoyer](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/thefoyer/32/120671_2.png) [@thefoyer](https://forum.ionicframework.com/u/thefoyer)\
**Post date:** [December 10, 2018, 7:08pm UTC](https://forum.ionicframework.com/t/generating-list-with-dividers-dynamically/341/18 "2018-12-10T19:08:38Z")

</div>

Okay so I have a solution that works with a slightly different data structure. Using the examples above here is how I worked it out:

```auto
var contacts = [
    {'givenName':'Lisa', 'familyName':'Adams', 'groupName':'Group 1'},
    {'givenName':'Bob', 'familyName':'Arnet', 'groupName':'Group 1'},
    {'givenName':'Frank', 'familyName':'Able', 'groupName':'Group 1'},
    {'givenName':'John', 'familyName':'Crow', 'groupName':'Group 2'},
    {'givenName':'Bill', 'familyName':'Ward', 'groupName':'Group 3'}
];

```

In order to group and sort the array I run it through these two functions:

```auto
let sContacts = sortArray(contacts, 'groupName', 1);
this.contacts = groupBy(sContacts, 'groupName', 'familyName');

var groupBy = function groupByArray(xs, key, sortKey) {
    return xs.reduce(function (rv, x) {
        let v = key instanceof Function ? key(x) : x[key];
        let el = rv.find(r => r && r.key === v);

        if (el) {
            el.values.push(x);
            el.values.sort(function (a, b) {
                return a[sortKey].toLowerCase().localeCompare(b[sortKey].toLowerCase());
            });
        } else {
            rv.push({ key: v, values: [x] });
        }

        return rv;
    }, []);
};

function sortArray(array, property, direction) {
    direction = direction || 1;
    array.sort(function compare(a, b) {
        let comparison = 0;
        if (a[property] > b[property]) {
            comparison = 1 * direction;
        } else if (a[property] < b[property]) {
            comparison = -1 * direction;
        }
        return comparison;
    });
    return array;
}

```

Now we have something like:

```auto
{
    'key' : 'Group 1',
    'values' : [
		['givenName': 'Lisa Adams', 'familyName': 'Adams'], 
		['givenName': 'Bob', 'familyName': 'Arnet'],
		['givenName': 'Frank', 'familyName': 'Able']
    ],
    'key' : 'Group 2',
    'values' : [
        ['givenName': 'John', 'familyName': 'Crow']
    ],
    'key' : 'Group 3',
    'values' : [
        ['givenName': 'Bill', 'familyName': 'Ward']
    ]
}

```

And the html file simply looks like this:

```auto
    <ion-item-group *ngFor="let group of contacts">
      <ion-item-divider color="light">{{group.key}}</ion-item-divider>
      <button ion-item (click)="itemSelected(contact)" *ngFor="let contact of group.values">
        {{ contact.givenName }} {{ contact.familyName }}
      </button>
    </ion-item-group>

```
