Answer the question
In order to leave comments, you need to log in
Managed sorting in angular?
Faced with a problem that I can not solve in any way, although at first glance it seems simple.
The essence is as follows: You need to display a select in which there will be inscriptions under the type
"Sort by price in ascending order"
"Sort by price in descending order"
"Sort by month in ascending order"
All this is tied to some scope.sort
And here is the main question. What to write in the select itself and what to write in ng-repeat, so that by clicking on the select you can specify the column by which it will be searched, as well as desc or asc
I tried this:
<select id="select-sort" ng-model="filter.sort" required ng-options="bool.value as bool.name for bool in boolValues"
ng-init="boolValues = [{value: {type: 'price', desc: true}, name: 'По возрастанию цены'}, {value: {type: 'price', desc: false}, name: 'По убыванию цены'}, {value: {type: 'm', desc: true}, name: 'По дате заезда'}]; filter.sort={type: 'price', desc: true}">
Answer the question
In order to leave comments, you need to log in
<div ng-controller="ProductsController as products">
<select ng-model="products.sort" ng-change="products.updateList()">
</select>
<ul>
<li ng-repeat="item in products.list">{{ item.name }}</li>
</ul>
</div>
function ProductsController {
var vm = this;
vm.sort = 1;
vm.updateList = updateList;
updateList();
function updateList () {
vm.list = vm.list.sort(function (a, b) {
// логика сортировки тут
});
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question