2016-05-21 32 views
3

我正在使用Angular的前端,我想弄清楚如何做一個過濾器,以便在結尾處包含2個屬性的結果。可能是一個或其他財產。如何做一個過濾器爲了把結果包含null屬性?

例如:

<div ng-repeat="show in shows | filter:query | orderBy:'rating':true"> 
     <a href="/shows/{{show._id}}"> 
     <img ng-src="{{show.poster}}" width="100%"/> 
     </a> 
     <div> 
     <a href="/shows/{{show._id}}">{{show.name}}</a> 
     <p> 
      Episodes: {{show.episodes.length}} <br> 
      <span>Rating: {{show.rating}}</span> 
     </p> 
     </div> 
    </div> 

我在HTML做什麼,是由等級排序,你可以看到。問題是,如果rating屬性爲null,那麼該元素將位於結果的頂部,然後具有最高評分的元素就像我想要的那樣進入列表中。

這是我收到的對象:

{ 
    "_id": 310243, 
    "name": "Five Star Babies: Inside the Portland Hospital", 
    "airsDayOfWeek": "Wednesday", 
    "airsTime": "21:00", 
    "firstAired": "2016-04-13T00:00:00.000Z", 
    "network": "BBC Two", 
    "overview": "An exclusive glimpse inside the UK's only private maternity hospital.\r\n", 
    "rating": null, // THIS PROPERTY COMES NULL 
    "ratingCount": 0, 
    "status": "Continuing", 
    "poster": "/someImage/path", 
    "subscribers": [] 
} 

對象來自這裏:

$scope.shows = Show.query(); 

$scope.shows返回此:

[ 
    { 
    "_id": 73787, 
    "name": "That '70s Show", 
    "airsDayOfWeek": "Thursday", 
    "airsTime": "8:00 PM", 
    "firstAired": "1998-08-23T00:00:00.000Z", 
    "network": "FOX (US)", 
    "overview": "Set in the Wisconsin suburbs...", 
    "rating": null, // PROPERTY NULL 
    "ratingCount": 87, 
    "status": "Ended" 
    "poster": "/some/Img/Path" 
    }, 
    { 
    "_id": 35345, 
    "name": "Friends", 
    "airsDayOfWeek": "Thursday", 
    "airsTime": "8:00 PM", 
    "firstAired": "1998-08-23T00:00:00.000Z", 
    "network": "FOX (US)", 
    "overview": "New York TV Show...", 
    "rating": 9.1, 
    "ratingCount": 87, 
    "status": "Ended" 
    "poster": null // PROPERTY NULL 
    } 
] 

這就是對象的方式來,以及那些我需要在結果結束時放置的兩個屬性ey來吧nullratingposter

TL; DR:

排序依據功能,其在端部使在所述結果與收視率最高的元素的頂部,並與ratingposter === null

任何建議的元素?

+1

調整數據或使用自定義過濾器 – charlietfl

+0

@charlietfl如何調整數據是來自API? – TheUnnamed

+0

可以在接收到數據時循環並將'null'更改爲'0',以便正確排序。並不鮮見需要操縱數據以更好地適合您的應用程序 – charlietfl

回答

1

你可以編寫你自己定製的orderBy函數,它返回一個值。例如:

$scope.order = function(show) { 
    if (show.rating == null || show.poster == null) return 0; 
    return show.rating; 
} 

HTML

<div ng-repeat="show in shows | filter:query | orderBy:order:true"> 

Sample pen

+0

嗯這是沒有做任何事情 – TheUnnamed

+0

有趣的,讓我看看。 –

+0

嗯,它肯定是在工作,但我沒有選擇相反的選項。它應該是'orderBy:order:true',我會編輯我的答案。編輯:以及添加一個樣本筆。 –

相關問題