D
D
desuvin2015-08-07 17:35:12
Angular
desuvin, 2015-08-07 17:35:12

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}">

But unfortunately it doesn't work. And I don’t know how to insert this into ng-repeat later

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2015-08-07
@desuvin

<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) {
            // логика сортировки тут
       });
   }
}

ps stop solving problems stupidly in templates, it will be much easier and less pain.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question