按名称排序

时间:2017-07-25 08:10:02

标签: javascript jquery angularjs angularjs-ng-repeat

我不知道! 我有一个使用Angular的工作Itemlist。现在我想要一个选择框排序。

应该是这样的:preview

这是一个Plunker:https://embed.plnkr.co/JYF0u9jBbsfyIlraH3BJ/

<div id="ideaList">
<div ng-controller="destinationsCtrl">
    <div class="filter">
        <p>Suchbegriff eingeben</p>
        <input type="text" ng-model="search" />
    </div>
    <div class="clearfix"></div>
    <div>Sort by:
        <select>
            <option value="idea.title">A-Z</option>
            <option value="destination.Dauer">Duration</option>
        </select>
    </div>
    <div class="clearfix"></div>
    <div class="container">
        <div ng-if="item.name == 'Thailand'" ng-repeat="item in items">
            <div class="row" ng-repeat="idea in item.ideas | orderBy: | filter:search">
                <div class="col-xs-4 col-md-2">
                    <div class="previewImg" style="background-image: url('{{idea.imageUrl}}') !important;"></div>
                </div>
                <div class="col-xs-8 col-md-8">
                    <h4>{{idea.title}}</h4>
                    <p class="truncate"><span ng-repeat="destination in idea.destinations"><b>{{destination.Dauer}}</b> {{destination.Ort}} </span>
                    </p>
                </div>
                <div class="col-xs-12 col-md-2"> <a href="{{idea.ideaUrl}}" class="btn btn-default btn-lightblue"><span ng-show="price in item.ideas">schon ab XXXX €  <i class="fa fa-angle-right" aria-hidden="true"></i></span></a>
                </div>
            </div>
        </div>
    </div>
</div>

这是我的JS:

angular.module('destinationsApp', []).controller('destinationsCtrl', 

function ($scope, $http) {
    $http.get('https://raw.githubusercontent.com/MAHUKI-Webdesign/suntrips.github.io/master/data.json').then(function (itemsResponse) {
        $scope.items = itemsResponse.data;
    });
});

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

据我所知,JSON响应的每个项目中都有项目数组和目标数组,因此您无需将其转换为数组。

希望这会让你知道该怎么做:

在模板中

...
<div>
    Sort by: 
    <select ng-model="sortExpression">
        <option value="title">A-Z</option>
        <option value="duration">Duration</option>
    </select>
</div>

...
<div ng-repeat="idea in item.ideas | orderBy : mySortFunction | filter:search">...

在控制器

$scope.mySortFunction = function(idea) {
    switch ($scope.sortExpression) {
        case 'title':
            return idea.title;
            break;
        case 'duration':
            // You might have to add more logic here to pick a proper destination from the array, not just the 'idea.destinations[0]' item
            return idea.destinations[0].Dauer;
            break;
    }
};