按字母顺序排列数组 - Angularjs

时间:2017-01-04 05:37:05

标签: javascript angularjs filter angular-ngmodel angular-filters

我的结果显示在这里

<div ng-repeat="o in vm.gridOptions.data | filter:filt | oderBy : 'title'">
    <span>{{o.title}}</span>
</div>

显示

<div ng-repeat="o in vm.gridOptions.data">
    <span>aaa</span>
    <span>ggg</span>
    <span>ccc</span>
    <span>bbb</span>
</div>

我有一个选择下拉列表。我选择“Alphabetical”选项

后,我应该能够按字母顺序对结果进行排序
<select ng-model="filt">
    <option value="-1">vsf</option>
    <option>Alphabetical</option>
    <option>sccc</option>                                    
</select>

最初我已经把orderBy:'title'。但这应该在我选择下拉列表后发生。如何实现这个目标?

5 个答案:

答案 0 :(得分:2)

很简单..

您希望按字母顺序排序..表示您必须将值 from CUA in de.ComputerUserApplication join x in de.ApplicationTypeMasters on CUA.RecordType equals x.Code into g from ATM in g.DefaultIfEmpty() select new ApplicationModel() { ApplicationNo = CUA.ApplicationNo, ApplicationCode = CUA.RecordType, ApplicationTypeCode = "", ApplicationName = ATM?.Title + " - " + CUA.Description, Status = CUA.Status }; 传递给您的选项'title'。 因为基于'title',结果将排序

'Alphabetical'

然后在你的div中

<select ng-model="filt">
    <option value="-1">vsf</option>
    <option value="title">Alphabetical</option>
    <option>sccc</option>                                    
</select>

<div ng-repeat="o in vm.gridOptions.data | orderBy:filt"> 仅针对您的orderBy: filt执行。这就是排序发生的地方

答案 1 :(得分:1)

你可以试试这个:

<div ng-repeat="o in vm.gridOptions.data | filter:filt | oderBy : filt?'title':''">

答案 2 :(得分:1)

<select ng-model="filt">
  <option>Alphabetical</option>
  <option>k</option>
</select>
<p ng-if="filte=(filt=='Alphabetical')?'':'o.title' "></p>
<div ng-repeat="o in vm.gridOptions.data | orderBy: filte">
<span>{{o.title}}</span>

答案 3 :(得分:1)

最好是在controller中进行过滤和排序。 您可以根据您的要求更改Array.prototype.sort()功能

控制器

$scope.data=[{
    "title" : "gggg"
},{
    "title" : "cccc"
},{
    "title" : "zzzz"
},{
    "title" : "aaaa"
}];
$scope.sortvalue = "None";

$scope.changeSortOrder = function()
{
  if($scope.sortvalue == "Alphabetically")
  {
    $scope.data.sort(function(a, b){
      if(a.title < b.title) return -1;
      if(a.title > b.title) return 1;
      return 0;
    });
  }
}

HTML

<div ng-controller="myCtrl">
    <div ng-repeat="o in data">
      <span>{{o.title}}</span>
    </div>
    <div>
     <select ng-model="sortvalue" ng-change="changeSortOrder()">
      <option value="None">Select an option</option>
      <option value="Alphabetically">Alphabetically</option>
     </select>
    </div>
  </div>

<强> FULL EXAMPLE

Here是使用Array.prototype.sort()对数组进行排序的示例列表

答案 4 :(得分:1)

尝试这种方式:

var myApp = angular.module('myApp',[]);

myApp.controller('MyCtrl',function($scope) {
    $scope.data = [{
        "title" : "aaa"
    },{
        "title" : "ggg"
    },{
        "title" : "ccc"
    },{
        "title" : "bbb"
    }];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="MyCtrl">
  <select ng-model="filt">
    <option value="-1">vsf</option>
    <option value="title">Alphabetical</option>
    <option>sccc</option>                                    
  </select>
  <div ng-repeat="o in data | orderBy:filt">
   {{o.title}}
</div>