applying class in ng-repeat if contained in another array - arrays

I am querying for a collection of IDs from Parse.com and showing them in my $scope as an array.
I would like to apply a class to the items in my $scope that match any one of these IDs, placing a border around the object illustrating that it is already contained in the 'saved' array. I have tried the following however not having any luck.
ng-class="{'approved': ParseSavedExercises.indexOf(exercise.id) == -1}"
in this case my ParseSavedExercisesis my array to check against and exercise.id is what I am checking for.
here is a quick fiddle

Please see here http://jsfiddle.net/e9pr4yqj/
Yours ParseSavedExercises contains string and id is number so no id existed in ParseSavedExercises
$scope.ParseSavedExercises = ['2','3'];
change to
$scope.ParseSavedExercises = [2,3];
or use
ng-class="{'approved': ParseSavedExercises.indexOf(exercise.id.toString()) == -1}"
like here http://jsfiddle.net/1ujgvL80/

Related

Get stored index from object literal in angular 2

I have a stored object literal which returns 2 or more objects depending on the users actions. This is then filtered further depending on if they are complete or not, so there are generally 2 object arrays that are sent to the view.
in the view i do a simple ngFor
<ion-card *ngFor="let item of inProgressList">
...
Which is a segment of the complete list held in a variable programsList
In order to change an item from in progress to complete i need to update the object, which works fine, however i cannot correctly pass the index.
filledOutFormsData[this.index].complete = true;
As i cannot get the correct index based on the array that has been filtered.
I cannot use
<ion-card *ngFor="let item of inProgressList; let i = index">
Because that will return a index of the looped items which will be incorrect once one other object is equal to complete
Basically i need to get the object key from the original object literal into the filtered array to use in the view so that i can pass the index.
So i got this working by adding a custom pipe which filters the programsList instead of passing in two separate lists, I can then use the trackBy option to pass the correct index.

Change a parameter type of a Lodash function

I'm newbie to Lodash and I use it within TypeScript coding. So I have an array of objects called items (of foods), every item has a category object parameter (with id & name attributes), so it looks like this :
I want to group those items by categroy names, I've created a new Object of type any called foodLists (public foodLists: any;),
and after loading those items, I've tried this:
this.foodLists = lodash.keyBy(this.items, 'category.name');
Actually, it did a part of the work, so it appears like this:
So the items are sorted by category , the issue is that it afftected just an only item to each category , however I want that many items should be affected to each category
So the second parameter of lodash.keyBy() (which is here 'category.name') should accept an array of objects. Hope you understand me. Is that possible ? or is there any alternative in lodash that gives the wanted result ? and Thank you.
it seems the solution is _.groupBy
lodash.groupBy(this.items, 'category.name')

AngularJS two way binding variables to array items

To view my code follow this link
I've created a directive which handles an array of items (it will always be two because it's a "from" and "to" date pair).
I want to make the array items accessible as separate values for later use so I refer to the array items like this
vm.data = ['data a', 'data b'];
vm.separateData = vm.data[0];
vm.otherData = vm.data[1];
When I implement a two way bind into the directive, the vm.data[0] and vm.data[1] references are updated but the vm.separateData and vm.otherData aren't.
Is there a way of making this work or should I just restructure the rest of my app (where needed) to accomodate for array items?
In my fiddle link (same as above) try changing the text input values and you'll see what I mean.
vm.data[0] is a string and thus it is a primitive datatype in javascript which is immutable. So you bind the immutable String 'Data a' to vm.separateData, which is not a reference to data[0].
If you want to copy the reference to the array into vm.separateData try to wrap your strings in other javascript objects, e.g.
vm.data = [{"value":"Data a"}, {"value":"Data b"}]
and then you can reference
vm.separateData = vm.data[0];
and access the value via
vm.separateData.value

Angularjs - How to filter objects by multiple values in their properties

I am having different objects like in the code sample below. I have some checkboxes that filters based on brewer, style, aroma and country. It is filtering only if it has one property in the array.
What is the best solution to make the filter work based on all the elements in the array? I checked some other questions and I couldn't find anything related.
It will be great if any of you could help me.
Thanks.
sample of the code here: http://pastie.org/10787362
You could use filter method of Javascript Array() object.
For example:
$scope.filteredNames = $scope.names.filter(function(element) {
return
'brewer1' in element.brewers &&
'aroma1' in element.aromas
;
});
$scope.filteredNames should contain all names with 'brewer1' value in brewers property AND 'aroma1' value in aromas property...

filter list with multiple checkboxes in Angularjs

I would like to filter a ng-repeat list of items with multiple catgories checkboxes.
i read this Filtering by multiple checkboxes in AngularJS and watched the videos by Egghead, but i have an error on a simple for loop and i don't understand:
ReferenceError: i is not defined
here is a plunker with the code : http://plnkr.co/edit/p538ALfs00JTFQ6mKT9j
thank you for your help
If you're going to use strict mode ('use strict';), you need to declare your variables. Your checkboxFilter uses many variables that are never defined (i included). You can get past the your initial issues by changing script.js:22-23 to:
var i,j,k,filter_cat,filter_value,filter_name,matchingItems = [];
You have three other problems after that...
Line 37, This code is looking for a property named filter_cat: items[i].filter_cat, when what you wanted was to look for a property name with the value of filter_cat, so this is what you want: items[i].[filter_cat].
Your json data has lowercase field names and your code is searching on uppercase (Type vs type)
Your json data has lowercase values and your code is searching on uppercase (Fruit vs fruit)
Here is a partial edit that gets you on the right track. You'll still need to modify your json or compare lowercase (I partially modified your json): http://plnkr.co/edit/d7p4QthXJg4ao34ATWla?p=preview

Resources