Angularjs: ng-options group by - angularjs

I have this one level tree situation:
<select ng-model="tipost"
ng-options="tip.DESC group by tip.TIPIS for tip in tipall"><br>
</select>
where the json is:
[
{"ID":"1", "IDPARENT":"0", "TIPIS":"", "DESC":"GroupName1"},
{"ID":"2", "IDPARENT":"1", "TIPIS":"GroupName1", "DESC":"CHILDNAME1"},
{"ID":"3", "IDPARENT":"0", "TIPIS":"", "DESC":"GroupName2"}
]
the problem is that this creates the optgroups with their children but repeats
the roots too:
- GroupName1
- GroupName2
[ GroupName1 ]
- CHILDNAME1
[ GroupName2 ]
i want to produce:
[ GroupName1 ]
- CHILDNAME1
[ GroupName2 ]

The grouping doesn't quite work like that, if you change your json to something like this:
[
{"ID":"1", "TIPIS":"GroupName1", "DESC":"name"},
{"ID":"2", "TIPIS":"GroupName1", "DESC":"name1"},
{"ID":"3", "TIPIS":"GroupName2", "DESC":"name2"},
{"ID":"4", "TIPIS":"GroupName1", "DESC":"name3"},
]
Then you'll get the grouping the way you want
jsFiddle: http://jsfiddle.net/rtCP3/182/

The simple drop down that can display items with and without groups, and also you can set some items as disabled: Here is plunk: https://plnkr.co/edit/uhsn4lmzssi6rrijPEAp?p=preview
<select ng-model="ddl.selected"
ng-options="item.id as item.text group by item.groupName disable when item.enabled===false for item in ddl.items"></select>

I was searching for same question and found better answer. One can use function to return data as he needs, like in query below getGarageName
ng-options="car.id as car.name + ' (' + car.color + ')' group by getGarageName(car.garageId) for car in cars"
// this is the data
cars = [
{"id": 1, "name": "Diablo", "color": "red", "garageId": 1},
{"id": 2, "name": "Countach", "color": "white", "garageId": 1},
{"id": 3, "name": "Clio", "color": "silver", "garageId": 2},
...
]
garages = [
{"id": 1, "name": "Super Garage Deluxe"},
{"id": 2, "name": "Toms Eastside"},
...
]
http://www.wenda.io/questions/2597799/angular-js-select-with-ngoptions-label-the-optgroup.html

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

AngularJS filtering using external JSON

I've been wondering for some long time, how can I filter something like this. Here is my situation:
I have one JSON table like this:
var cars = [{
"id": 1,
"brand": "Ford",
"model": "Focus",
"generation": "2.0TDCi",
"version": "hatchback, 3 drzwi",
"mileage": 100,},{
"id": 2,
"brand": "Volkswagen",
"model": "Golf",
"generation": "III",
"version": " 1.9 TDI, hatchback, 4 drzwi",
"mileage": 14,}];
And I'm using this kind of generating table using data from JSON:
<tr data-ng-repeat="car in cars" (...)
I've also have a dropdown with multiselection:
<div ng-dropdown-multiselect="" options="exampleBrand" selected-model="exampleBrandModel" extra-settings="exampleSettings"></div>
Things which are imported into this selector are from JSON file:
$scope.exampleBrand = [{id: 1, label: "Ford"}, {id: 2, label: "Ferrari"}, {id: 3, label:"Suzuki"},
{id: 4, label:"Fiat"}, {id: 5, label:"Aston Martin"}, {id: 6, label:"Opel"}];
The model of thing which are selected lands here:
$scope.exampleBrandModel = [];
For example if I select "Ford", the JSON file with model looks like this (checked with
{{ exampleBrandModel| json }}
):
[
{
"id": "Ford"
}
]
And my question is. How can I use angular's filter option to make it working?
I was using something like that which was not working:
<tr data-ng-repeat="car in cars |filter: {exampleBrandModel : id}">
I want to have situation like this:
At start I get whole table with cars listed in and then I select "Ford". Now every row in table without "Ford" word should be filtered out.
Do someone know how can I do that?

Ng-option directive cant get access to nested array value

Help
I'm trying to output different class options(economy/business) with ng-options.
Here is the syntax and json information .
Cant get it working
<select ng-model="mySelect" ng-options="item for item in items ">
</select>
$scope.items = [
{
"id": 2,
"codeName": "class",
"friendlyName": "Class",
"options":
[
{
"id": 15,
"displayOrderNo": 0,
"optionName": "Economy"
},
{
"id": 16,
"displayOrderNo": 1,
"optionName": "Business"
},
{
"id": 36,
"displayOrderNo": 2,
"optionName": "First Class "
}
]
}
];

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 ?

AngularJS: Preselecting Options on Multiple Select

I'm trying to preselect options on my multiselect. In order to make my case clear, I've made a JSFiddle.
<select ng-model="properties" id="props" multiple
ng-options="option.name group by option.category for option in options"></select>
Unfortunately, I am bound by the way the object is received, so I guess I need the ng-options attribute.
Does anybody have an idea how I can get both 'Captain America' and 'Dr. Doom' selected on load?
Try changing your controller code to:
function TestCtrl($scope) {
var myOptions = [{
"id": "4",
"name": "Captain America",
"categoryId": "1",
"category": "Heroes"
}, {
"id": "5",
"name": "Iron Man",
"categoryId": "1",
"category": "Heroes"
}, {
"id": "10",
"name": "Magneto",
"categoryId": "2",
"category": "Vilains"
}, {
"id": "9",
"name": "Dr. Doom",
"categoryId": "2",
"category": "Vilains"
}];
$scope.options = myOptions;
$scope.properties = [myOptions[0], myOptions[3]];
}
Explanation: you are setting your selected options (properties) to different instances of the objects than the ones that compose the full list. Use the same object references as shown above.
You are super close, two changes. Define the attributes in your controller like this
$scope.properties = ["Captain America", "Dr. Doom"];
And add a small piece to your ng-options like this
ng-options="option.name as option.name group by option.category for option in options"
Your option.name as will determine what the saved feature looks like in $scope.properties

Resources