filter list with multiple checkboxes in Angularjs - 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

Related

Laravel Eloquent - Working with an array of JSON objects

I have an array of JSON objects within my database (as depicted by the image below)
How do I search for a specific value within it? Say I want the record where the "ends_at" field is equal to "2018-11-24 08:00:00"
I've tried using
->where('column->starts_at', '2018-11-24 08:00:00')
or even tried
->whereJsonContains('column->starts_at', '2018-11-24 08:00:00').
Within the model I've cast the column to an array - am I missing something, or can this not be done?
Use this:
->whereJsonContains('column', ['starts_at' => '2018-11-24 08:00:00'])

Angular copy not working on copying array

It seems that angular.copy() is not properly working on one of the items that I am using it on. Here's the sample code and the screenshot that follows.
console.log("Copy");
$scope.traffic_data = traffic_data;
$scope.total_data = total_data;
console.log($scope.traffic_data);
console.log($scope.total_data);
console.log("Original");
$rootScope.original_traffic_data = angular.copy($scope.traffic_data);
$rootScope.original_total_data = angular.copy($scope.total_data);
console.log($rootScope.original_traffic_data);
console.log($rootScope.original_total_data);
console.log("Variable data");
console.log(total_data);
console.log("=============");
The problem I am facing is that the
$rootscope.original_total_data
is not copying the contents of the
$scope.total_data
as seen on the screenshot. I have highlighted the different console logs to differentiate them from one another.
The line
console.log($rootScope.original_total_data);
shows no contents even though I have used angular.copy on that variable.
What am I missing here? Please help. Thanks.
Also $rootScope is already declared in the controller and it is working for the
$rootScope.original_traffic_data
so why is it not working for
$rootScope.original_total_data?
Thanks.
total_data is an array, whereas traffic_data is an object.
angular.copy() distinguishes between arrays and objects. For objects it will copy all the keys (properties). For arrays, it will only copy the array elements and not any custom properties attached to it - see source code.
If you want to set properties on total_data, you should make it into an object instead. It does not appear to have any indexed values, so this should not be a problem, and it probably should have been an object in the first place.

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...

applying class in ng-repeat if contained in another array

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/

Unable to identify object using descriptive programming but able to do so with description object

On my application i have to search for an alphanumeric id which return one or more rows of data. On each of these rows a link is present i have to click on the first link.
Unfortunately it doesn't have any unique properties so i cannot add it to the OR. Instead i used descriptive programming something like below
'returns false
page.Link("class:=ng-binding","innertext:=AplhaID","html tag:=A").Exist
QTP fails to identify the object with the above code. So Instead of this i tried using Description object something like the below code
Set oDesc = Description.Object
oDesc("class").Value = "ng-binding"
oDesc("html tag").Value = "A"
oDesc("innertext").Value = "AplhaID"
Set lnk = page.ChildObjects(oDesc)
'gives me 2 which is correct. There are two links
msgbox lnk.Count
'highlights the correct link
lnk(0).Highlight
I do not know what could be causing this behavior. I thought is could be because multiple links match the description but I perform this search for multiple ids and eventhough multiple rows are returned the descriptive programming code is able to identify the correct row and proceed.
I looked at QTP descriptive programming issue but my link's property values do not have special characters.
In order to use Descriptive string method, ensure that you have only one object matching the given properties.
Below statement might fail if there are more than 1 object with the given properties.
page.Link("class:=ng-binding","innertext:=AplhaID","html tag:=A").Exist
So , you need to make the statement to find an object uniquely. Try this. It will work!
page.Link("class:=ng-binding","innertext:=AplhaID","html tag:=A", "index:=0").Exist

Resources