2015-08-27 113 views
0

我要求TheMovieDB提供的內容在我的網站上顯示它(標題,圖片,發佈日期),並將該內容存儲在我的數據庫中。我想知道是否可以按日期訂購。如何按日期訂購?

我知道那裏有| orderBy:'date'",但我不知道如何在我的網站上使用它。

會有兩種情況,我希望按日期排序。

第一種情況是當用戶執行搜索操作時,他們可以鍵入電影標題,我的搜索功能將從TheMovieDB中檢索所有標題(以及發佈日期)並將它們顯示在ul中。

%div{"ng-controller" => "searchCtrl", :id => "container_search"} 

    %input{"ng-change" => "search(searchquery)", "ng-model" => "searchquery", "ng-model-options" => "{ debounce: 500 }", :id => "search_input", :placeholder => "Search a movie!", "ng-keyup" => "clear_results()"} 
    %ul#showresults 
    %li.search_results{"ng-repeat" => "movie in movieList"} 
     .addmovie{"ng-click" => "addMovie()"} 
     %span 
      Add Movie 
     %img.poster{"ng-src" => "http://image.tmdb.org/t/p/w500/{{ movie.poster_path }}"} 
     %span.title 
     {{ movie.original_title }} 
     %span.release 
     {{ movie.release_date }} 

TheMovieDB HTTP請求的輸出是這樣的,

angular.callbacks._6({"page":1,"results":[{"adult":false,"backdrop_path":"/jSNIAKD5pCyhjgJL9HZ3hsu19yD.jpg","genre_ids":[28,12,80],"id":206647,"original_language":"en","original_title":"SPECTRE","overview":"A cryptic message from Bond’s past sends him on a trail to uncover a sinister organization. While M battles political forces to keep the secret service alive, Bond peels back the layers of deceit to reveal the terrible truth behind SPECTRE.","release_date":"2015-10-26","poster_path":"/zHx5KaKaQdZZnEUgtjcIdNcB3ka.jpg","popularity":1.279592,"title":"SPECTRE","video":false,"vote_average":7.0,"vote_count":61}],"total_pages":1,"total_results":1}) 

而第二個情況是當用戶檢查他的頭版就說明他加入到他的頁面中的所有電影片名。

%div{"ng-repeat" => "movie in movies", :class => "movie_container"} 
    %h2 
    {{ movie.release_date }} 
    %img.poster{"ng-src" => "{{ movie.image }}"} 
    .movie-info 
    %h2 
     {{ movie.title }} 
    %a{"ng-click" => "deleteMovie(movie)"}delete 

這些是在它們被創建時排序,而不是在它們被釋放時排序。我的JSON文件的輸出是這樣的,

{"id":51,"created_at":"2015-08-27T13:05:19.074Z","updated_at":"2015-08-27T13:05:19.074Z","user_id":1,"image":"http://image.tmdb.org/t/p/w500//uereni1nSrWb6OWiJhK7FrW4pcd.jpg","title":"\n  Batman v Superman: Dawn of Justice\n  ","release_date":"\n  2016-03-25\n  "}] 

回答

0

不要使用日期作爲字符串消氣劑領域的IM orderBy。我建議你創建一個自定義函數,將你的元素變成一個值。這個值可以通過在日期對象中使用JSON日期字符串(例如使用momentjs)來獲得,然後獲得絕對時間(秒)。

+0

'orderBy:'release_date'這實際上有效。沒想到答案會很明顯。 –