2016-11-01 11 views
1

入力テキストの値を取得し、配列を持つオブジェクトの値と比較するにはどうすればよいですか?AngularJS by order by function

コメント配列のプロパティ名を入力するとき、入力されたこのテキストで注文する必要があります。

たとえば、著者によるオーダーオーダーを入力します。 これについて検索し、ドキュメントを読んでいますが、解決策が見つかりませんでした。 関数をその順序で渡すことはできますか?私は試してみましたが、動かなかったのです

私はあまりにも多くの例が1つの値を渡すか、入力テキストなしで見つかりました。著者、評価または日付の場合は3つのオプションを渡しますが、入力テキストにこの単語を入力するとします。

なぜ、このように動作しません。代わりにHTMLフィルタの

Sort by: <input type="text" ng-model="**sortBy**"> 
        <div ng-repeat="dishcomment in dishCtrl.dish.comments|filter:**sortBy**"> 

var dish={ 
    name:'Uthapizza', 
    image: 'images/uthapizza.png', 
    category: 'mains', 
    label:'Hot', 
    price:'4.99', 
    description:'A unique combination of Indian Uthappam (pancake) and Italian pizza, topped with Cerignola olives, ripe vine cherry tomatoes, Vidalia onion, Guntur chillies and Buffalo Paneer.', 
    comments: [ 
     { 
      rating:5, 
      comment:"Imagine all the eatables, living in conFusion!", 
      author:"John Lemon", 
      date:"2012-10-16T17:57:28.556094Z" 
     }, 
     { 
      rating:4, 
      comment:"Sends anyone to heaven, I wish I could get my mother-in-law to eat it!", 
      author:"Paul McVites", 
      date:"2014-09-05T17:57:28.556094Z" 
     }, 
     { 
      rating:3, 
      comment:"Eat it, just eat it!", 
      author:"Michael Jaikishan", 
      date:"2015-02-13T17:57:28.556094Z" 
     }, 
     { 
      rating:4, 
      comment:"Ultimate, Reaching for the stars!", 
      author:"Ringo Starry", 
      date:"2013-12-02T17:57:28.556094Z" 
     }, 
     { 
      rating:2, 
      comment:"It's your birthday, we're gonna party!", 
      author:"25 Cent", 
      date:"2011-12-02T17:57:28.556094Z" 
     } 
    ] 
}; 

this.dish = dish; 
Sort by: <input type="text" ng-model="searchBox"> 
      <div ng-repeat="dishcomment in dishCtrl.dish.comments | orderBy:searchBox track by $index"> 
      <blockquote> 
       <p>{{dishcomment.rating}} Stars</p> 
       <p>{{dishcomment.comment}}</p> 
       <small>{{dishcomment.author}} 
       {{dishcomment.date | date:'MMM.dd, yyyy'}}</small> 
      </blockquote> 
+1

あなたはこれを見ましたか?http://stackoverflow.com/questions/27454658/ng-repeat-orderby-object? – DivineTraube

+1

私が質問を完全に誤解しない限り、これは書かれているように機能します:http://plnkr.co/edit/poU4KXsy9GZFeLjnnsZ6。 'rating'とタイプすると、' comment'や 'author'と同じように順序が変わります。 – Claies

+0

これは、心に留めていることではありません。私は、入力の値を受け取り、オプションがng-repeatかどうかをチェックする関数を呼び出したいだけです。テキストボックスの内容を確認するプロパティに対して – gise

答えて

0

我々は、コントローラコードにフィルタを入れて試してみてng-のリストを更新することができます入力の変更。 これは次のようになります。

  1. わかりやすい。
  2. 作成されたウォッチャーの数がhtmlフィルタよりもずっと少ないため、大きなリストの方が良いでしょう。

    $scope.sortBy = function(){ 
    if($scope.dish.comments.hasOwnProperty($scope.sortByProp)){ 
    
        //sort the comments by the property 
        $scope.dish.comments = sortedComments; 
    
    }} 
    

次のHTMLスニペットです:

<input type="text" ng-model="sortByProp" ng-change="sortBy()"> 
+0

ありがとうございます。私はorderbyとやり取りする方法を見つけました。 – gise

1

あなたは、以下のような角度でカスタムフィルタを書くことができます。

app.filter('commentsFilter', function ($filter) {  
    return function (inputArray, orderBy) { 
    var outputArray = []; 

    if (order) { 
     angular.forEach(inputArray, function (input) { 
      if(orderBy === 'author'){ 
       // Sort by author logic goes here 
       // Modify outputArray with sorted data 
      } 
      if(orderBy === 'date'){ 
       // Sort by date logic goes here 
       // Modify outputArray with sorted data 
      }     
     }); 
    } else { 
     return inputArray; 
    } 
    return outputArray; 
    }; 
}); 

HTMLコードスニペットが

Sort by: <input type="text" ng-model="searchBox"> 
     <div ng-repeat="dishcomment in dishCtrl.dish.comments | commentsFilter:searchBox track by $index"> 
     <blockquote> 
      <p>{{dishcomment.rating}} Stars</p> 
      <p>{{dishcomment.comment}}</p> 
      <small>{{dishcomment.author}} 
      {{dishcomment.date | date:'MMM.dd, yyyy'}}</small> 
     </blockquote> 
     </div>  
以下の通りです
+0

ありがとう、しかし、私はそれを行う簡単な方法を見つける:http://stackoverflow.com/questions/37100705/order-by-elements-based-on-input-text-angularjs/38962231#38962231ちょうど文字列の空の解決をassingning問題。そして、もし私がfuntionが働いているが、 – gise