Skip to content

Commit 742690c

Browse files
committed
docs(collectionRepeat): add documentation
1 parent da0ad75 commit 742690c

File tree

6 files changed

+161
-71
lines changed

6 files changed

+161
-71
lines changed

demos/collection-repeat/index.html

Lines changed: 6 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
<body ng-controller="MainCtrl">
1818
<ion-header-bar class="bar-positive">
1919
<h1 class="title">3000 Contacts</h1>
20+
<div class="button" ng-click="scrollBottom()">
21+
Scroll Bottom
22+
</div>
2023
</ion-header-bar>
2124
<ion-header-bar class="bar-light bar-subheader">
2225
<input type="search"
@@ -32,61 +35,17 @@ <h1 class="title">3000 Contacts</h1>
3235
</ion-header-bar>
3336
<ion-content>
3437
<div class="list">
35-
<a class="item"
38+
<a class="item my-item"
3639
collection-repeat="item in getContacts()"
3740
collection-item-height="getItemHeight(item)"
38-
collection-item-width="getItemWidth(item)"
41+
collection-item-width="100 + '%'"
3942
ng-href="https://www.google.com/#q={{item.first_name + '+' + item.last_name}}"
40-
ng-style="{height: getItemHeight(item), 'line-height': getItemHeight(item) + 'px', 'padding-top': 0, 'padding-bottom': 0, width: getItemWidth(item)}"
43+
ng-style="{'line-height': getItemHeight(item) + 'px'}"
4144
ng-class="{'item-divider': item.isLetter}">
4245
<img ng-if="!item.isLetter" ng-src="http://placekitten.com/60/{{55 + ($index % 10)}}">
4346
{{item.letter || (item.first_name+' '+item.last_name)}}
4447
</a>
4548
</div>
4649
</ion-content>
47-
<!-- <div class="letters has-header has-subheader" ng-if="!searchFocused"> -->
48-
<!-- <div class="letter" -->
49-
<!-- ng-repeat="letter in letters" -->
50-
<!-- ng-click="goToLetter(letter)" -->
51-
<!-- ng-style="{top: (100/letters.length)*$index + '%'}"> -->
52-
<!-- {{letter}} -->
53-
<!-- </div> -->
54-
<!-- </div> -->
55-
<style>
56-
.letters {
57-
right: 0;
58-
bottom: 0;
59-
width: 50px;
60-
position: absolute;
61-
}
62-
.letter {
63-
text-align: center;
64-
font-size: 15px;
65-
height: 3.85%;
66-
padding-top: 1.625%;
67-
border: 1px solid black;
68-
width: 100%;
69-
cursor: pointer;
70-
right: 0;
71-
position: absolute;
72-
z-index: 100;
73-
}
74-
.button.button-icon.input-button {
75-
position: absolute;
76-
right: 0;
77-
top: 5px;
78-
color: #bbb;
79-
}
80-
.item img {
81-
height: 60px;
82-
width: 60px;
83-
float: left;
84-
margin-top: 10px;
85-
margin-right: 10px;
86-
}
87-
.list {
88-
height: 100%;
89-
}
90-
</style>
9150
</body>
9251
</html>

demos/collection-repeat/script.js

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,11 @@ angular.module('contactsApp', ['ionic'])
3939

4040
//Letters are shorter, everything else is 52 pixels
4141
$scope.getItemHeight = function(item) {
42-
return item.isLetter ? 45 : '25%';
42+
return item.isLetter ? 40 : 100;
4343
};
44-
$scope.getItemWidth = function(item) {
45-
return item.isLetter ? '100%' : '50%';
44+
45+
$scope.scrollBottom = function() {
46+
$ionicScrollDelegate.scrollBottom(true);
4647
};
4748

4849
var letterHasMatch = {};
@@ -73,25 +74,6 @@ angular.module('contactsApp', ['ionic'])
7374
});
7475
};
7576

76-
//We have to figure out which scrollValue to go to for the letter
77-
//Luckily, we already supply the height of every item to collection-repeat,
78-
//so we will just use that!
79-
$scope.goToLetter = function(letter) {
80-
var scrollValue = 0;
81-
var contacts = $scope.getContacts();
82-
//Find the height of every item until we hit the given letter
83-
for (var i = 0, ii = contacts.length; i < ii; i++) {
84-
if (contacts[i].isLetter && contacts[i].letter === letter) {
85-
break;
86-
}
87-
scrollValue += $scope.getItemHeight(contacts[i]);
88-
}
89-
$ionicScrollDelegate.scrollTo(0, scrollValue);
90-
};
91-
92-
$scope.scrollTop = function() {
93-
$ionicScrollDelegate.scrollTop();
94-
};
9577
$scope.clearSearch = function() {
9678
$scope.search = '';
9779
};

demos/collection-repeat/style.css

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
1-
.item {
1+
.button.button-icon.input-button {
2+
position: absolute;
3+
right: 0;
4+
top: 5px;
5+
color: #bbb;
6+
}
7+
.item img {
8+
height: 60px;
9+
width: 60px;
10+
float: left;
11+
margin-top: 20px;
12+
margin-right: 10px;
13+
}
14+
.list .my-item.item {
215
left: 0;
316
right: 0;
17+
padding-top: 0;
18+
padding-bottom: 0;
419
}

docs/templates/api_menu_version.template.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,12 @@
238238
</a>
239239
</li>
240240

241+
<li>
242+
<a href="{{ page.versionHref }}/api/directive/collectionRepeat/">
243+
collection-repeat
244+
</a>
245+
</li>
246+
241247
<li>
242248
<a href="{{ page.versionHref }}/api/service/$ionicListDelegate/">
243249
$ionicListDelegate

js/angular/directive/collectionRepeat.js

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,131 @@
1+
/**
2+
* @ngdoc directive
3+
* @module ionic
4+
* @name collectionRepeat
5+
* @restrict A
6+
* @description
7+
* `collection-repeat` is a directive that allows you to render lists with
8+
* thousands of items in them, and experience little to no performance penalty.
9+
*
10+
* Demo:
11+
*
12+
* The directive renders onto the screen only the items that should be currently visible.
13+
* So if you have 1,000 items in your list but only ten fit on your screen,
14+
* collection-repeat will only render into the DOM the ten that are in the current
15+
* scroll position.
16+
*
17+
* Here are a few things to keep in mind while using collection-repeat:
18+
*
19+
* 1. The data supplied to collection-repeat must be an array.
20+
* 2. You must explicitly tell the directive what size your items will be in the DOM
21+
* (pixel amount or percentage), using directive attributes (see below).
22+
* 3. The elements rendered will be absolutely positioned: be sure to let your CSS work with this (see below).
23+
* 4. Keep the HTML of your repeated elements as simple as possible. As the user scrolls down, elements
24+
* will be lazily compiled. Resultingly, the more complicated your elements, the more likely it is that
25+
* the on-demand compilation will cause jankiness in the user's scrolling.
26+
* 5. The more elements you render on the screen at a time, the slower the scrolling will be.
27+
* It is recommended to keep grids of collection-repeat list elements at 3-wide or less.
28+
* 6. Each collection-repeat list will take up all of its parent scrollView's space.
29+
* If you wish to have multiple lists on one page, put each list within its own
30+
* {@link ionic.directive:ionScroll ionScroll} container.
31+
*
32+
*
33+
*
34+
* @usage
35+
*
36+
* #### Basic Usage (single rows of items)
37+
*
38+
* Notice two things here: we use ng-style to set the height of the item to match
39+
* what the repeater thinks our item height is. Additionally, we add a css rule
40+
* to make our item stretch to fit the full screen (since it will be absolutely
41+
* positioned).
42+
*
43+
* ```html
44+
* <ion-content ng-controller="ContentCtrl">
45+
* <div class="list">
46+
* <div class="item my-item"
47+
* collection-repeat="item in items"
48+
* collection-item-width="100%"
49+
* collection-item-height="getItemHeight(item, $index)"
50+
* ng-style="{height: getItemHeight(item, $index)}">
51+
* {% raw %}{{item}}{% endraw %}
52+
* </div>
53+
* </div>
54+
* </div>
55+
* ```
56+
* ```js
57+
* function ContentCtrl($scope) {
58+
* $scope.items = [];
59+
* for (var i = 0; i < 1000; i++) {
60+
* $scope.items.push('Item ' + i);
61+
* }
62+
*
63+
* $scope.getItemHeight = function(item, index) {
64+
* //Make evenly indexed items be 10px taller, for the sake of example
65+
* return (index % 2) === 0 ? 50 : 60;
66+
* };
67+
* }
68+
* ```
69+
* ```css
70+
* .my-item {
71+
* left: 0;
72+
* right: 0;
73+
* }
74+
* ```
75+
*
76+
* #### Grid Usage (three items per row)
77+
*
78+
* ```html
79+
* <ion-content>
80+
* <div class="item item-avatar my-image-item"
81+
* collection-repeat="image in images"
82+
* collection-item-width="33%"
83+
* collection-item-height="33%">
84+
* <img ng-src="{{image.src}}">
85+
* </div>
86+
* </ion-content>
87+
* ```
88+
* ```css
89+
* .my-image-item {
90+
* height: 33%;
91+
* width: 33%;
92+
* }
93+
* ```
94+
*
95+
* @param {expression} collection-repeat The expression indicating how to enumerate a collection. These
96+
* formats are currently supported:
97+
*
98+
* * `variable in expression` – where variable is the user defined loop variable and `expression`
99+
* is a scope expression giving the collection to enumerate.
100+
*
101+
* For example: `album in artist.albums`.
102+
*
103+
* * `variable in expression track by tracking_expression` – You can also provide an optional tracking function
104+
* which can be used to associate the objects in the collection with the DOM elements. If no tracking function
105+
* is specified the collection-repeat associates elements by identity in the collection. It is an error to have
106+
* more than one tracking function to resolve to the same key. (This would mean that two distinct objects are
107+
* mapped to the same DOM element, which is not possible.) Filters should be applied to the expression,
108+
* before specifying a tracking expression.
109+
*
110+
* For example: `item in items` is equivalent to `item in items track by $id(item)'. This implies that the DOM elements
111+
* will be associated by item identity in the array.
112+
*
113+
* For example: `item in items track by $id(item)`. A built in `$id()` function can be used to assign a unique
114+
* `$$hashKey` property to each item in the array. This property is then used as a key to associated DOM elements
115+
* with the corresponding item in the array by identity. Moving the same object in array would move the DOM
116+
* element in the same way in the DOM.
117+
*
118+
* For example: `item in items track by item.id` is a typical pattern when the items come from the database. In this
119+
* case the object identity does not matter. Two objects are considered equivalent as long as their `id`
120+
* property is same.
121+
*
122+
* For example: `item in items | filter:searchText track by item.id` is a pattern that might be used to apply a filter
123+
* to items in conjunction with a tracking expression.
124+
*
125+
* @param {expression} collection-item-width The width of the repeated element. Can be a number (in pixels) or a percentage.
126+
* @param {expression} collection-item-height The height of the repeated element. Can be a number (in pixels), or a percentage.
127+
*
128+
*/
1129
IonicModule
2130
.directive('collectionRepeat', [
3131
'$collectionRepeatManager',

js/angular/service/scrollDelegate.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ IonicModule
114114
'anchorScroll',
115115
/**
116116
* @ngdoc method
117-
* @name $ionicScrollDelegate.#getScrollView
117+
* @name $ionicScrollDelegate#getScrollView
118118
* @returns {object} The scrollView associated with this delegate.
119119
*/
120120
'getScrollView',

0 commit comments

Comments
 (0)