forked from angular/code.angularjs.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathangular.Array.orderBy.html
78 lines (72 loc) · 4.05 KB
/
angular.Array.orderBy.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<h1>angular.Array.orderBy</h1>
<fieldset class="workInProgress"><legend>Work in Progress</legend>
This page is currently being revised. It might be incomplete or contain inaccuracies.</fieldset>
<h2>Description</h2>
<p>Orders <code>array</code> by the <code>expression</code> predicate.</p>
<p>Note: this function is used to augment the Array type in angular expressions. See
<a href="#!angular.Array"><code>angular.Array</code></a> for more info.</p><h2>Usage</h2>
<div ng:non-bindable=""><pre class="brush: js; html-script: true;">angular.Array.orderBy(array, expression, reverse);</pre>
</div>
<h3>Parameters</h3>
<ul class="parameters"><li><code ng:non-bindable="">array – {Array} – </code>
The array to sort.</li>
<li><code ng:non-bindable="">expression – {function()|string|Array.<(function()|string)>} – </code>
A predicate to be used by the
comparator to determine the order of elements.</p>
<p>Can be one of:</p>
<ul>
<li><code>function</code>: JavaScript's Array#sort comparator function</li>
<li><code>string</code>: angular expression which evaluates to an object to order by, such as 'name' to
sort by a property called 'name'. Optionally prefixed with <code>+</code> or <code>-</code> to control ascending
or descending sort order (e.g. +name or -name).</li>
<li><code>Array</code>: array of function or string predicates, such that a first predicate in the array
is used for sorting, but when the items are equivalent next predicate is used.</li>
</ul></li>
<li><code ng:non-bindable="">reverse<i>(optional)</i> – {boolean} – </code>
Reverse the order the array.</li>
</ul>
<h3>Returns</h3>
<code ng:non-bindable="">Array</code>
– Sorted copy of the source array.<h2>Example</h2>
<doc:example><doc:source><div ng:init="friends = [{name:'John', phone:'555-1212', age:10},
{name:'Mary', phone:'555-9876', age:19},
{name:'Mike', phone:'555-4321', age:21},
{name:'Adam', phone:'555-5678', age:35},
{name:'Julie', phone:'555-8765', age:29}]"></div>
<pre>Sorting predicate = {{predicate}}</pre>
<hr/>
<table ng:init="predicate='-age'">
<tr>
<th><a href="" ng:click="predicate = 'name'">Name</a>
(<a href ng:click="predicate = '-name'">^</a>)</th>
<th><a href="" ng:click="predicate = 'phone'">Phone</a>
(<a href ng:click="predicate = '-phone'">^</a>)</th>
<th><a href="" ng:click="predicate = 'age'">Age</a>
(<a href ng:click="predicate = '-age'">^</a>)</th>
<tr>
<tr ng:repeat="friend in friends.$orderBy(predicate)">
<td>{{friend.name}}</td>
<td>{{friend.phone}}</td>
<td>{{friend.age}}</td>
<tr>
</table></doc:source>
<doc:scenario>it('should be reverse ordered by aged', function() {
expect(binding('predicate')).toBe('Sorting predicate = -age');
expect(repeater('.doc-example table', 'friend in friends').column('friend.age')).
toEqual(['35', '29', '21', '19', '10']);
expect(repeater('.doc-example table', 'friend in friends').column('friend.name')).
toEqual(['Adam', 'Julie', 'Mike', 'Mary', 'John']);
});
it('should reorder the table when user selects different predicate', function() {
element('.doc-example a:contains("Name")').click();
expect(repeater('.doc-example table', 'friend in friends').column('friend.name')).
toEqual(['Adam', 'John', 'Julie', 'Mary', 'Mike']);
expect(repeater('.doc-example table', 'friend in friends').column('friend.age')).
toEqual(['35', '10', '29', '19', '21']);
element('.doc-example a:contains("Phone")+a:contains("^")').click();
expect(repeater('.doc-example table', 'friend in friends').column('friend.phone')).
toEqual(['555-9876', '555-8765', '555-5678', '555-4321', '555-1212']);
expect(repeater('.doc-example table', 'friend in friends').column('friend.name')).
toEqual(['Mary', 'Julie', 'Adam', 'Mike', 'John']);
});</doc:scenario>
</doc:example>