Zamówienie niestandardowe przy użyciu orderBy w ng-repeat
Mam takie obiekty:
students = {name: 'Aa_Student', class: 'A_Class'},
{name: 'Ab_Student', class: 'A_Class'},
{name: 'Ac_Student', class: 'B_Class'},
{name: 'Ba_Student', class: 'B_Class'},
{name: 'Bb_Student', class: 'C_Class'},
{name: 'Bc_Student', class: 'C_Class'}
Powiedzmy, że obiekt uczniów jest tasowany. Używam ng-repeat, aby pokazać dane. Chcę sortować obiekty w niestandardowej kolejności.
Na przykład chcę pokazać dane w ten sposób:
Name Class
-----------------------------
Ac_Student B_Class
Ba_Student B_Class
Aa_Student A_Class
Ab_Student A_Class
Bb_Student C_Class
Bc_Student C_Class
Więc w zasadzie, Chcę zamówić według klasy ucznia, ale najpierw b_class, potem a_class, potem C_Class. Również, chcę zamówić przez studentów nazwy w porządku alfabetycznym. Jak mogę to zrobić to?
HTML:
<table>
<tr ng-repeat="student in students | orderBy:customOrder">
...
</tr>
</table>
Kontroler:
$scope.customOrder = function(student) {
$scope.students = $filter('orderBy')(student, function() {
});
};
16
Author: Kimchi Man, 0000-00-00
3 answers
Cześć możesz utworzyć własny filtr sortowania zobacz tutaj http://jsbin.com/lizesuli/1/edit
Html:
<p ng-repeat="s in students |customSorter:'class'">{{s.name}} - {{s.class}} </p>
</div>
Filtr Angularjs:
app.filter('customSorter', function() {
function CustomOrder(item) {
switch(item) {
case 'A_Class':
return 2;
case 'B_Class':
return 1;
case 'C_Class':
return 3;
}
}
return function(items, field) {
var filtered = [];
angular.forEach(items, function(item) {
filtered.push(item);
});
filtered.sort(function (a, b) {
return (CustomOrder(a.class) > CustomOrder(b.class) ? 1 : -1);
});
return filtered;
};
});
20
Author: sylwester,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-08-17 09:43:08
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-08-17 09:43:08
Wiem, że to stare,ale może się przydać innym...
Można również utworzyć prostą funkcję sortowania niestandardowego. "Nie do końca filtr": {]}
$scope.customOrder = function (item) {
switch (item) {
case 'A_Class':
return 2;
case 'B_Class':
return 1;
case 'C_Class':
return 3;
}
};
A potem używaj tak jak chciałeś:
<table>
<tr ng-repeat="student in students | orderBy:customOrder">
...
</tr>
10
Author: ShaneRoode,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-04-12 04:03:09
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-04-12 04:03:09
Aby ustawić orderBy
jako właściwość obiektów, po prostu Zacytuj tę nazwę właściwości w znacznikach:
ng-repeat="student in students |orderBy:'name' | orderBy:'class'"
-1
Author: charlietfl,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-07-21 13:58:48
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-07-21 13:58:48