2017-04-05 21 views
0

我正在使用angularjs排序按過濾器排序json。根據優先順序,小寫字母后跟大寫字母,但不適用於單個字符。任何人都可以幫助解決這個問題嗎?Angular js按'排序'不適用於單個字符

$scope.friends = [ 
     {name: 'A'}, 
     {name: 'a'}, 
     {name: 'B'}, 
     {name: 'b'}, 
     {name: 'C'}, 
     {name: 'c'} 
    ]; 
<table class="friends"> 
    <tr> 
     <th>Name</th> 
    </tr> 
    <tr ng-repeat="friend in friends | orderBy:'name'"> 
     <td>{{friend.name}}</td> 
    </tr> 
    </table> 

結果:

Name 
==== 
A 
a 
B 
b 
C 
c 

回答

0

嘗試這種解決方案僅適用於單字符:

angular.module('app', []).controller('MyController', ['$scope', function($scope) { 
 
    $scope.friends = [ 
 
     {name: 'A'}, 
 
     {name: 'a'}, 
 
     {name: 'B'}, 
 
     {name: 'b'}, 
 
     {name: 'C'}, 
 
     {name: 'c'} 
 
    ]; 
 
}]).filter('customOrderBy', function(){ 
 
    return function(input, name) 
 
    { 
 
    var result = [] 
 
    for(var item of input) 
 
    { 
 
     var prop = item[name]; 
 
     var toUpper = prop.toUpperCase().charCodeAt(0);  
 
     result.push({item, code: toUpper + (prop.toLowerCase() == prop ? 0 : 1)});  
 
    } 
 
    return result.sort(function(a, b){return a.code > b.code ? 1 : (a.code == b.code ? 0 : -1);}).map(function(x) { return x.item; });   
 
    } 
 
})
<script src="//code.angularjs.org/snapshot/angular.min.js"></script> 
 

 
<body ng-app="app"> 
 
    <div ng-controller="MyController"> 
 
<table class="friends"> 
 
    <tr> 
 
     <th>Name</th> 
 
    </tr> 
 
    <tr ng-repeat="friend in friends | customOrderBy: 'name'"> 
 
     <td>{{friend.name}}</td> 
 
    </tr> 
 
    </table> 
 
</div> 
 
</body>

+0

工作正常。謝謝。但我添加了一些字符的優先級變化..如果我想用多個字符和單個字符應該如何編碼? – Indhu

+0

在多個字符的情況下,您應該逐一比較它們,實現邏輯,在我的答案或您自己的答案中提供。 –

0

你可以試試這個自定義過濾器像下面?

<body ng-app="MyApp"> 
    <div ng-controller="MyCtrl"> 
     <table class="friends"> 
      <tr> 
      <th>Name</th> 
      </tr> 
     <tr ng-repeat="friend in friends | localeOrderBy:'name'"> 
      <td>{{friend.name}}</td> 
     </tr> 
     </table> 
    </div> 
</body> 


app.filter("localeOrderBy", [function() { 
    return function (array, sortPredicate, reverseOrder) { 
     if (!Array.isArray(array)) return array; 
     if (!sortPredicate) return array; 

     var isString = function (value) { 
      return (typeof value === "string"); 
     }; 

     var isNumber = function (value) { 
      return (typeof value === "number"); 
     }; 

     var isBoolean = function (value) { 
      return (typeof value === "boolean"); 
     }; 

     var arrayCopy = []; 
     angular.forEach(array, function (item) { 
      arrayCopy.push(item); 
     }); 

     arrayCopy.sort(function (a, b) { 
      var valueA = a[sortPredicate]; 
      var valueB = b[sortPredicate]; 

      if (isString(valueA)) 
       return !reverseOrder ? valueA.localeCompare(valueB) : valueB.localeCompare(valueA); 

      if (isNumber(valueA) || isBoolean(valueA)) 
       return !reverseOrder ? valueA - valueB : valueB - valueA; 

      return 0; 
     }); 

     return arrayCopy; 
    } 
}]); 
+0

我只是一片空白.....我可以知道你想在這裏表達什麼? – 2017-04-05 07:36:09

+0

我對代碼進行了一些更改,請檢查並讓我知道 –