AngularJS&需要Rails建议

时间:2014-10-21 19:06:47

标签: javascript ruby-on-rails angularjs

我目前正在使用rails构建一个站点,但希望将AngularJS用于以下内容:

在2种不同类型的数据中快速排序(例如,点击列表中的不同餐馆,然后点击列表中的不同食物类别并查看所有结果)

搜索餐馆,食品类别和食品。

确定“孩子”是否合并到食品中并用布尔值显示。

经过大量的研究,我没有成功找到任何符合我需求的好文章/物品。主要是因为我想使用javascript而不是coffeescript。如果任何人有任何可以帮助我的资源或想法,请发送。

现在我看了以下内容:

https://www.honeybadger.io/blog/2013/12/11/beginners-guide-to-angular-js-rails https://egghead.io/lessons/angularjs-rails-todo-api-part-1

1 个答案:

答案 0 :(得分:2)

我根据这个小提琴创建了一个类似于你所描述的角度过滤器,并且从它来自的线程获得了一些帮助。希望这些对你有用......

http://jsfiddle.net/rzgWr/19/

Filter Array Using Checkboxes with AngularJS

var myApp = angular.module('myApp',[]);

var uniqueItems = function (data, key) {
var result = [];

for (var i = 0; i < data.length; i++) {
    var value = data[i][key];

    if (result.indexOf(value) == -1) {
        result.push(value);
    }

}
return result;
};

function MyCtrl($scope, filterFilter) {
$scope.usePants = {};
$scope.useShirts = {};
$scope.useShoes = {};

$scope.players = [
    {name: 'Bruce Wayne', shirt: 'XXL', pants: '42', shoes: '12'},
    {name: 'Wayne Gretzky', shirt: 'XL', pants: '38', shoes: '10'},
    {name: 'Michael Jordan', shirt: 'M', pants: '32', shoes: '9'},
    {name: 'Rodman', shirt: 'XSXL', pants: '42', shoes: '11'},
    {name: 'Jake Smitz', shirt: 'XXL', pants: '42', shoes: '12'},
    {name: 'Will Will', shirt: 'XXLL', pants: '42', shoes: '12'},
    {name: 'Youasdf Oukls', shirt: 'XL', pants: '38', shoes: '10'},
    {name: 'Sam Sneed', shirt: 'XL', pants: '38', shoes: '10'},
    {name: 'Bill Waxy', shirt: 'M', pants: '32', shoes: '9'},
    {name: 'Javier Xavior', shirt: 'M', pants: '32', shoes: '9'},
    {name: 'Bill Knight', shirt: 'M', pants: '32', shoes: '9'},        
    {name: 'One More', shirt: 'M', pants: '32', shoes: '9'},        
    {name: 'Player One', shirt: 'XXL', pants: '42', shoes: '11'},
    {name: 'Space Cadet', shirt: 'XXL', pants: '42', shoes: '12'},
    {name: 'Player Two', shirt: 'XXXXL', pants: '42', shoes: '12'}
]; 

// Watch the pants that are selected
$scope.$watch(function () {
    return {
        players: $scope.players,
        usePants: $scope.usePants,
        useShirts: $scope.useShirts,
        useShoes: $scope.useShoes
    }
}, function (value) {
    var selected;

    $scope.count = function (prop, value) {
        return function (el) {
            return el[prop] == value;
        };
    };

    $scope.pantsGroup = uniqueItems($scope.players, 'pants');
    var filterAfterPants = [];        
    selected = false;
    for (var j in $scope.players) {
        var p = $scope.players[j];
        for (var i in $scope.usePants) {
            if ($scope.usePants[i]) {
                selected = true;
                if (i == p.pants) {
                    filterAfterPants.push(p);
                    break;
                }
            }
        }        
    }
    if (!selected) {
        filterAfterPants = $scope.players;
    }

    $scope.shirtsGroup = uniqueItems($scope.players, 'shirt');
    var filterAfterShirts = [];        
    selected = false;
    for (var j in filterAfterPants) {
        var p = filterAfterPants[j];
        for (var i in $scope.useShirts) {
            if ($scope.useShirts[i]) {
                selected = true;
                if (i == p.shirt) {
                    filterAfterShirts.push(p);
                    break;
                }
            }
        }       
    }
    if (!selected) {
        filterAfterShirts = filterAfterPants;
    }

    $scope.shoesGroup = uniqueItems($scope.players, 'shoes');
    var filterAfterShoes = [];        
    selected = false;
    for (var j in filterAfterShirts) {
        var p = filterAfterShirts[j];
        for (var i in $scope.useShoes) {
            if ($scope.useShoes[i]) {
                selected = true;
                if (i == p.shoes) {
                    filterAfterShoes.push(p);
                    break;
                }
            }
        }    
    }
    if (!selected) {
        filterAfterShoes = filterAfterShirts;
    }        

    $scope.filteredPlayers = filterAfterShoes;        
}, true);


$scope.$watch('filtered', function (newValue) {
    if (angular.isArray(newValue)) {
        console.log(newValue.length);
    }
}, true);    
}

myApp.filter('count', function() {
return function(collection, key) {
  var out = "test";
  for (var i = 0; i < collection.length; i++) {
      //console.log(collection[i].pants);
      //var out = myApp.filter('filter')(collection[i].pants, "42", true);
  }
  return out;
}
});


myApp.filter('groupBy',
        function () {
            return function (collection, key) {
                if (collection === null) return;
                return uniqueItems(collection, key);
    };
});