Using Angular Typeahead on Nested Objects - angularjs

I'm trying to use Angular Bootstrap Typeahead on an array of nested objects and I cannot figure out how to write out the typeahead.
My objects in my array are like so:
{
"category": "Locations",
"regions": [
{
"name": "Northeast",
"category": "region",
"states": [
{
"name": "New York",
"category": "state"
"cities": [
{
"name": "Syracuse",
"category": "city"
}
]
}
]
}
I only want to return the name values. So how would I go about writing this out?
I currently am writing <input ... typeahead=" filter.name for filter in filters| filter:$viewValue | limitTo:5">

Rather than using "in filters" just do something like "in transformFilters()"
$scope.transformFilters = function () {
// Loop over filters and create an array of
{name: name, category:cat}
return my new array
}

Related

How to use OData filter in dynamic array

I am trying to filter nested array using ?$filter in odata filter
but it is not working properly
parent array got filtered but not child one.
My Array
{
"value": [
{
"Id": 1,
"Country": "India",
"language": [
{
"Lid": 1,
"State": "telengana",
"Statuelanguage": "Telgu",
"Place to visit": [
"p3","p4"
]
},
{
"Lid": 2,
"State": "Delhi",
"Statuelanguage": "Hindi",
"Place to visit": [
"p5","p6"
]
},
{
"Lid": 3,
"State": "UP",
"Statuelanguage": "Hindi",
"Place to visit": [
"p7","p8"
]
}
]
}
]
}
Expected Responce
{
"value": [
{
"Id": 1,
"Country": "India",
"language": [
{
"Lid": 1,
"State": "telengana",
"Statuelanguage": "Telgu",
"Place to visit": [
"p3","p4"
]
}
]
}
]
}
Filter query
?$filter=language/any(c: c/Lid eq 1)
but when i am trying to use the filter, it is filtering the parent one not the child
it returns all 3 child to me
So it works as expected :)
$filter parameter is used to filter collection that you're querying.
To filter expanded/related collection (language in your case) you have to use expand filter feature:
...$expand=language($filter=Lid eq 1)
BUT: It is only possible in OData v4.
ref for webapi
nested filter description

How to projection element in array field of MongoDb collection?

MongoDb Collection Example (Person):
{
"id": "12345",
"schools": [
{
"name": "A",
"zipcode": "12345"
},
{
"name": "B",
"zipcode": "67890"
}
]
}
Desired output:
{
"id": "12345",
"schools": [
{
"zipcode": "12345"
},
{
"zipcode": "67890"
}
]
}
My current partial code for retrieving all:
collection.find({}, {id: true, schools: true})
I am querying the entire collection. But I only want to return zipcode part of school element, not other fields (because the actual school object might contain much more data which I do not need). I could retrieve all and remove those un-needed fields (like "name" of school) in code, but that's not what I am looking for. I want to do a MongoDb query.
You can use the dot notation to project specific fields inside documents embedded in an array.
db.collection.find({},{id:true, "schools.zipcode":1}).pretty()

empty array in angular-schema-form

I'm using angular-schema-form in my project and trying to add an empty array in my model.
I expected that array type schema form will not contain any items but it actually pushed one object in my array and showed it in from.
How can I init form with no items in array?
html
<div ng-app="app" ng-controller="Controller as ctr">
<form sf-schema="ctr.schema" sf-form="ctr.form" sf-model="ctr.model"></form>
{{ctr.model.comments}} - Where this object come from? This array was empty. Is it possible to keep it empty on load?
</div>
js
var myApp = angular.module('app', ['schemaForm'])
.controller("Controller", function() {
this.schema = {
"type": "object",
"title": "Comment",
"properties": {
"comments": {
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {
"title": "Name",
"type": "string"
},
"comment": {
"title": "Comment",
"type": "string",
"maxLength": 20,
"validationMessage": "Don't be greedy!"
}
}
}
}
}
};
this.form = [{
"key": "comments",
"items": [
"comments[]"
]
}];
this.model = {
comments: [] // Empty array defined
};
});
Jsfiddle
The value you are looking for is startEmpty: true this will avoid pre-populating the array with an object.
Angular Schema Form: Array Documentation
The pre-population is defaulted to ensure that the form fields within an array are available when the form loads. The startEmpty: true value can override this behaviour.

How do I filter to show only the first three properties from my JSON using Angular

I have the following JSON file to read and I want to only display some particular keys I want from the JSON.
[
{
"recommendation_id": "cuhwbchuwbcuhw-ccnahcbhb-12",
"title": "hellow world",
"content": "Hello angular",
"name": "John",
"home": "USA"
},
{
"recommendation_id": "cuhwcacabchuwbw-ccnahcbhb-32",
"title": "ng-show",
"content": "ng-show is amazing",
"name": "Google",
"home": "USA"
},
......
{
"recommendation_id": "guwqiwu212wbcuhw-ccnahcbhb-12",
"title": "Awesome",
"content": "Hello Awesome",
"name": "Mike",
"home": "Canada"
}
]
Here is what I use to access the header for my table using Jade.
table(id="allRecommendations")
thead
tr
th(ng-repeat='(key, value) in recommendationsAll[1]') {{key}}
And the output is
recommendation_id title content name home
But I only want the first three items, which should display like the following:
recommendation_id title content
So, what do I miss on my filter? I am interested in getting the object properties not my property details. How do I filter to show particular JSON properties?
This will solve the problem.
table(id="allRecommendations")
thead
tr
th(ng-repeat="(key, value) in recommendationsAll[1]", ng-show="key == 'recommendation_id' || key == 'title' || key == 'content' " ) {{key}}

object nested into a nested array, how to implement a custom filter for it?

I have an application that I am constructing with a friend and we have a very big problem: I have a very big json with some nested arrays an objects, I am using a filter with an ng-model="search" the filter (search) works great with the top level array which is named sports, but once I try to search through the leagues array, the filter returns nothing. I saw in another question that I can search(filter) based on nested properties which is exactly what I want. I tried to follow the example answer on that question but I am having a problem trying to solve this. Someone else says: that is not possible with this kind of matching that you are trying because you want to filter through a matching sport.name and all of his matching and not non matching leagues or a non matching sport.name and only his matching leagues.
json
[
{
"name": "Cricket",
"leagues": []
},
{
"name": "NBA Games",
"leagues": [
{
"name": "NBA",
"sport": {
"id": 8,
"name": "NBA Earliest"
},
"lineType": "G",
"priority": [
1,
3
],
"part": "0"
}
]
},
{
"name": "COLLEGE Basketball",
"leagues": [
{
"name": "College - NCAA BASKETBALL",
"sport": {
"id": 24,
"name": "College Basketball"
},
"lineType": "G",
"priority": [
0,
4
],
"part": "0"
},
{
"name": "NCAA BASKETBALL ADDED GAMES",
"sport": {
"id": 24,
"name": "College Basketball"
},
"lineType": "G",
"priority": [
1,
4
],
"part": "0"
},
...
my html
<input type="search" ng-model="search">
<div ng-repeat="sport in sports | filter: query">
<!-- searching (filtering) great -->
<div>{{sport.name}}</div>
</div>
<div ng-repeat="league in sport.leagues | filter: query">
<!-- here is not filtering at all -->
{{league.name}}
</div>
</div>
can I do it this way I am trying or how do I implement a custom filter for this ?

Resources