0
私はAngularJSフィルターを使ってムービーを選んでいますが、複数の検索語を使用しようとしています。ムービーがフィルタによってまだキャッチされていない場合、ムービーは配列の先頭に移動します。最終的には、ヒットで配列を並べ替えるのですが、その間は設定したいのですが、ムービーがすでに検索結果配列にある場合は、配列の先頭に移動して表示されます最初。どんな助けでも大歓迎です!配列内の要素をJavascript/AngularJSで先頭に移動
// New module:
var app = angular.module('movieFinder', []);
// Search function:
app.filter('searchFor', function() {
return function(arr, searchString) {
// Show all movies until search begins
if(!searchString) {
return arr;
}
var result = [];
// Search for each word. Change to lowercase. Remove any punctuation. Split into array.
searchArray = searchString.toLowerCase().replace(/[^\w\s]|_/g, "").split(" ");
// Work through each word.
for (i = 0; i < searchArray.length; i++) {
// Work through each movie.
angular.forEach(arr, function(movie) {
if((movie.title.toLowerCase().indexOf(searchArray[i]) !== -1) ||
(movie.description.toLowerCase().indexOf(searchArray[i]) !== -1) ||
(movie.director.toLowerCase().indexOf(searchArray[i]) !== -1) ||
(movie.country.toLowerCase().indexOf(searchArray[i]) !== -1) ||
(movie.year.toString().indexOf(searchArray[i]) !== -1) ||
(movie.genre.toLowerCase().indexOf(searchArray[i]) !== -1) ||
(movie.keywords.toLowerCase().indexOf(searchArray[i]) !== -1)) {
// If the movie is not listed...
if (result.indexOf(movie) === -1) {
// Insert at top of array
result.unshift(movie);
};
};
});
}
return result;
}
});
// The controller
function MovieFinderController($scope){
// The data model
$scope.movies = [
{
title: 'The Beat That My Heart Skipped',
description: 'Will Thomas still lead a life of crime and cruelty, just like his thuggish father, or will he pursue his dream of becoming a pianist?',
director: 'Jacques Audiard',
country: 'France',
year: 2005,
genre: 'Noir Thriller',
cover: 'images/thebeatthatmyheartskipped.jpg',
keywords: 'piano, gangster, broody, thug'
},{
title: 'Chungking Express',
description: 'Two melancholy Hong Kong policemen fall in love: one with a mysterious female underworld figure, the other with a beautiful and ethereal server at a late-night restaurant he frequents.',
director: 'Wong Kar Wai',
country: 'Hong Kong',
year: 1994,
genre: 'Romantic Comedy',
cover: 'images/chungkingexpress.jpg',
keywords: 'fast food, cops, music, wigs, dancing, Chinese food, heartache, eating'
}
(...)
ありがとうございました!