I am trying to get "children" object from nested array, well, the problem is how to pull "childrens" and display them in dropdown options? For example:
[
{
"id": 1,
"name": "someName",
"children": [
{
"id": 2,
"name": "children_lvl_1",
"children": [
{
"id": 3,
"name": "children_lvl_2",
}
]
"children": [
{
"id": 4,
"name": "children_lvl_3",
}
]
}
]
}
]
The original idea is to get something like here:
https://embed.plnkr.co/plunk/Vn1zH5
So far I tried something like below, that wasnt writen by me.
Data.post('documentCategories/read', {}).then(function(results) {
if (results.status === "success") {
$scope.model.categories = parseCategories(results.data, -1, $scope.tag);
$scope.model.categoryTree = results.data;
var obj = $scope.model.categoryTree = results.data;
// console.log($scope.model.categoryTree);
const node = getNode($scope.model.categoryTree, $scope.document.category_id);
if (node) {
parseExpirationRanges(node);
$scope.model.categoryTags.splice(0, $scope.model.categoryTags.length);
$scope.model.categoryTags = node.categoryTags.slice();
}
}
if (results.status === "error") {
$.notify(results.message, "error");
}
return 0;
});
<div class="form-group">
<label class="col-sm-4 control-label no-padding-right">Categorys</label>
<div class="col-sm-8">
<span class="block input-icon input-icon-right">
<select ng-model="document.category" class="form-control"
ng-options="category as category.name for category in model.categoryTree" required></select>
</span>
</div>
</div>
Any fresh ideas are welcome.
Tnx
Have ng-model for each select and in ng-options of second and third select iterate through .children of previous select's model variable
<select ng-model="select1" ng-options="node as node.name for node in data"></select>
<select ng-model="select2" ng-options="node as node.name for node in select1.children"></select>
<select ng-model="select3" ng-options="node as node.name for node in select2.children"></select>
angular.module('app', []).controller("ctrl", function ($scope) {
$scope.data = [{
"id": 1,
"name": "Lvl1 Example 1",
"children": [{
"id": 4,
"name": "Lvl2 Example 4",
"children": [{
"id": 8,
"name": "Lvl3 Example 8",
}]
},
{
"id": 5,
"name": "Lvl2 Example 5",
"children": [{
"id": 9,
"name": "Lvl3 Example 9",
}]
}
]
},
{
"id": 2,
"name": "Lvl1 Example 2",
"children": [{
"id": 6,
"name": "Lvl2 Example 6",
"children": [{
"id": 10,
"name": "Lvl3 Example 10",
}]
}]
},
{
"id": 3,
"name": "Lvl1 Example 3",
"children": [{
"id": 7,
"name": "Lvl2 Example 7",
"children": [{
"id": 11,
"name": "Lvl3 Example 11",
}]
}]
}
]
});
<div ng-app="app" ng-controller="ctrl">
<select ng-model="select1" ng-options="node as node.name for node in data"></select>
<select ng-model="select2" ng-options="node as node.name for node in select1.children"></select>
<select ng-model="select3" ng-options="node as node.name for node in select2.children"></select>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
Related
I am implementing typeahead using AngularUI-Bootstrap. I need to show the results grouped based on some values coming from the database. Here's a example of result
[{
"id": 1,
"label": "type_1",
"titles": [{
"id": 1,
"label": "title_1"
}, {
"id": 2,
"label": "title_2"
}, {
"id": 3,
"label": "title_3"
}]
}, {
"id": 2,
"label": "type_2",
"titles": [{
"id": 4,
"label": "title_4"
}, {
"id": 6,
"label": "title_6"
}]
}, {
"id": 3,
"label": "type_3",
"titles": [{
"id": 8,
"label": "title_8"
}, {
"id": 9,
"label": "title_9"
}]
}]
How can grouping titles by type in AngularUI-Bootstrap Typeahead
JB Nizet is right, you should use a custom template (default). Look at
// view
<script type="text/ng-template" id="typeahead-match.html">
<div ng-if="match.model.isGroup">{{match.label}}</div>
<a ng-if="!match.model.isGroup" ng-bind-html="match.label | uibTypeaheadHighlight:query">
{{match.label}}
</a>
</script>
<input
type="text"
ng-model="selected"
uib-typeahead="item as item.label for item in getItems($viewValue)"
class="form-control"
typeahead-template-url="typeahead-match.html">
// controller
myApp.controller('MainController', ['$scope', function($scope) {
var data = [{
"id": 1,
"label": "type_1",
"titles": [{
"id": 1,
"label": "title_1"
}, {
"id": 2,
"label": "title_2"
}, {
"id": 3,
"label": "title_3"
}]
}, {
"id": 2,
"label": "type_2",
"titles": [{
"id": 4,
"label": "title_4"
}, {
"id": 6,
"label": "title_6"
}]
}, {
"id": 3,
"label": "type_3",
"titles": [{
"id": 8,
"label": "title_8"
}, {
"id": 9,
"label": "title_9"
}]
}];
$scope.getItems = function(text) {
var result = [];
_.each(data, function(group) {
result.push({
label: group.label,
isGroup: true
});
_.each(group.titles, function(item) {
if(_.startsWith(item.label, text)) {
result.push(item);
}
});
if(result && result[result.length-1].isGroup) {
result.pop();
}
});
return result;
};
}]);
Example
i have object value in JSON, within which it contains array.i am trying to group the subdocument but does not work. my plunk
The below is my controller code.
Controller
app.controller('MainCtrl', function($scope) {
$scope.list = {
"_id": "56c4758af801160e00d176e0",
"orderfood": [
{
"_id": "569d84f04834c10e003dff36",
"qty": "1",
"confirm": "placed",
"price": 125,
"name": "Paneer Tikka"
},
{
"_id": "569d869fff1fe20e00f8ba9b",
"qty": "1",
"confirm": "placed",
"price": 85,
"name": "Falooda"
},
{
"_id": "569d869fff1fe20e00f8ba9b",
"qty": "1",
"confirm": "placed",
"price": 85,
"name": "Falooda"
}
],
"title": "Status",
"created": "2016-02-17T13:28:42.226Z"
}
});
The below is my HTML code
HTML
<p ng-repeat="(key,value) in list.orderfood | groupBy:'name'">
{{key}}
</p>
my plunk
Dont know what you want your ouput to be but you need to do this with a custom function:
<div ng-app ng-controller="Main">
<div ng-repeat="list in itemsToFilter() | filter:filterNames">
<b>{{list.name}}</b>
<li ng-repeat="item in itemsToFilter() | filter: {name: list.name}">NAME: {{item.name}}- PRICE: {{item.price}}, etc...</li>
</div>
</div>
Then in your controller:
function Main($scope) {
$scope.list = {
"_id": "56c4758af801160e00d176e0",
"orderfood": [
{
"_id": "569d84f04834c10e003dff36",
"qty": "1",
"confirm": "placed",
"price": 125,
"name": "Paneer Tikka"
},
{
"_id": "569d869fff1fe20e00f8ba9b",
"qty": "1",
"confirm": "placed",
"price": 85,
"name": "Falooda"
},
{
"_id": "569d869fff1fe20e00f8ba9b",
"qty": "1",
"confirm": "placed",
"price": 85,
"name": "Falooda"
}
],
"title": "Status",
"created": "2016-02-17T13:28:42.226Z"
}
var indexedTeams = [];
$scope.itemsToFilter = function() {
indexedTeams = [];
return $scope.list.orderfood;
}
$scope.filterNames = function(item) {
var nameIsNew = indexedTeams.indexOf(item.name) == -1;
if (nameIsNew) {
indexedTeams.push(item.name);
}
return nameIsNew;
}
}
here is a link...js-fiddle exmple: http://jsfiddle.net/L6cQN/305/
Select2 with MetroUI CSS works well with AngularJS.
But when I try using the same in dependent drop down lists I face the issue that view of child drop-down list doesn't get updated accordingly to change in parent drop-down list.
Here is a Stack Snippet demonstrating the issue I am seeing.
(function () {
"use strict";
var app = angular.module("app", []);
app.controller("AppCtrl", AppCtrl);
function AppCtrl() {
var vm = this;
vm.ContextData = {
selectParents: [{ "id": 1, "name": "item 1" },
{ "id": 2, "name": "item 2" },
{ "id": 3, "name": "item 3" },
{ "id": 4, "name": "item 4" }],
selectChildren: [{ "id": 1, "parentId": 1, "name": "1 based on item1" },
{ "id": 2, "parentId": 1, "name": "2 based on item1" },
{ "id": 3, "parentId": 1, "name": "3 based on item1" },
{ "id": 4, "parentId": 1, "name": "4 based on item1" },
{ "id": 5, "parentId": 1, "name": "5 based on item1" },
{ "id": 6, "parentId": 1, "name": "6 based on item1" },
{ "id": 7, "parentId": 1, "name": "7 based on item1" },
{ "id": 8, "parentId": 2, "name": "8 based on item2" },
{ "id": 9, "parentId": 2, "name": "9 based on item2" },
{ "id": 10, "parentId": 2, "name": "10 based on item2" },
{ "id": 11, "parentId": 2, "name": "11 based on item2" },
{ "id": 12, "parentId": 2, "name": "12 based on item2" },
{ "id": 13, "parentId": 3, "name": "13 based on item3" },
{ "id": 14, "parentId": 3, "name": "14 based on item3" },
{ "id": 15, "parentId": 3, "name": "15 based on item3" },
{ "id": 16, "parentId": 3, "name": "16 based on item3" },
{ "id": 17, "parentId": 4, "name": "17 based on item4" },
{ "id": 18, "parentId": 4, "name": "18 based on item4" }]
};
}
})();
<body ng-app="app" ng-controller="AppCtrl as vm">
<div class="flex-grid">
<div class="row">
<div class="cell colspan2 margin10">
<div class="input-control full-size" data-role="select" data-placeholder="Select a parent" data-allow-clear="true">
<select class="full-size" style="display:none" ng-model="vm.selectedParent" ng-options="item.name for item in vm.ContextData.selectParents">
<option value=""></option>
</select>
</div>
</div>
<div class="cell colspan2 margin10">
<div class="input-control full-size" data-role="select" data-placeholder="Select a child" data-allow-clear="true">
<select class="full-size" style="display:none" ng-model="vm.selectedChild" ng-options="item.name for item in vm.ContextData.selectChildren | filter:{parentId:vm.selectedParent.id}">
<option value=""></option>
</select>
</div>
</div>
</div>
</div>
<div class="flex-grid margin10">
<div class="row">
<div class="cell colspan2">
Selected Parent :
</div>
<div class="cell colspan3">
{{vm.selectedParent}}
</div>
</div>
<div class="row">
<div class="cell colspan2">
selected Child :
</div>
<div class="cell colspan3">
{{vm.selectedChild}}
</div>
</div>
</div>
</body>
<link href="https://cdn.rawgit.com/olton/Metro-UI-CSS/master/build/css/metro-responsive.min.css" rel="stylesheet"/>
<link href="https://cdn.rawgit.com/olton/Metro-UI-CSS/master/build/css/metro.min.css" rel="stylesheet"/>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/select2/4.0.1/js/select2.min.js"></script>
<script src="https://cdn.rawgit.com/olton/Metro-UI-CSS/master/build/js/metro.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.8/angular.min.js"></script>
Steps to reproduce:
Select 'Item 1' in parent select (parent select's place-holder is 'select a parent')
In "Selected parent" section correct value of selected parent is shown.
Child select is bound with correct child option.
Select any option from child select.
In "Selected child" section correct value of selected child is shown.
In Parent select, select 'Item 4'.
In "Selected parent" section correct value of selected parent is shown.
Child select is bound but the options are not updated and it still shows the options for parent Item1.
Select any item in child select, In "Selected Child" section correct value of child is shown, but child drop down list shows wrong Item!
Hence my issue is that even though child select gets the correct options bound to it, it's option list view doesn't refreshes.
[I tried select2.val(") but still the option view of child dropdown did not refresh.]
Another issue is this: if I clear the selection in Parent select, it doesn't clear out the selection in Child select.
The issue here involves how Select2 caches options once the dropdown is open, because it is saving what it thinks is the correct data object to the <option> even though AngularJS is actually changing the option later on. As a result, Select2 will still display the cached data while AngularJS will recognize that the option has changed, and you end up seeing strange edge cases like this.
There is no known fix to this issue right now, except for forcing AngularJS to somehow not reuse options in the dependent dropdown.
See this codepen.io to get the dependent drop-down list working.
Key points of solution are:
Give an ID to child drop-down
Then provide an ng-change event to parent drop-down
In ng-change event of parent drop-down, search for the child drop-down via it's ID
Then call select2 functions on child drop-down to empty the selected value and html
Do not forget to trigger change event on child drop-down.
so it would be something like this in the ng-change event (childSelect2 is ID of child drop-down):
var ele = angular.element('#childSelect2');
ele.html("").val("").trigger("change");
Also note that on clicking the cross mark in parent drop-down, we are clearing the child drop-down as for any change we are calling ele.html("").val("").trigger("change");
and this is expected because if there is no selection in parent then it has no meaning to have selection in child.
but with this we also need to clear out the ng-model of child drop-down so for that in ng-change function set the model of child drop-down to null (vm.selectedChild is ng-model for child drop-down)
vm.selectedChild = null;
though to have this any effect I found out that this setting of child model to null should be before calling ele.html("").val("").trigger("change"); hence I have made it the first statement in ng-change event. [I do not know why this placement-order is important, but it worked only after this change :)]
Hope the answer helps other and save their time.
quick reference:
HTML Code:
<body ng-app="app" ng-controller="AppCtrl as vm">
<div class="flex-grid">
<div class="row">
<div class="cell colspan2 margin10">
<div class="input-control full-size" data-role="select" data-placeholder="Select a parent" data-allow-clear="true">
<select class="full-size" style="display:none" ng-model="vm.selectedParent" ng-options="item.name for item in vm.ContextData.selectParents" ng-change="vm.SetSelect2()">
<option value=""></option>
</select>
</div>
</div>
<div class="cell colspan2 margin10">
<div class="input-control full-size" data-role="select" data-placeholder="Select a child" data-allow-clear="true">
<select class="full-size" style="display:none" ng-model="vm.selectedChild" ng-options="item.name for item in vm.ContextData.selectChildren | filter:{parentId:vm.selectedParent.id}" id="childSelect2">
<option value=""></option>
</select>
</div>
</div>
</div>
</div>
<div class="flex-grid margin10">
<div class="row">
<div class="cell colspan2">
Selected Parent :
</div>
<div class="cell colspan3">
{{vm.selectedParent}}
</div>
</div>
<div class="row">
<div class="cell colspan2">
selected Child :
</div>
<div class="cell colspan3">
{{vm.selectedChild}}
</div>
</div>
</div>
</body>
JS Code (angular Module and controller):
(function () {
"use strict";
var app = angular.module("app", []);
app.controller("AppCtrl", AppCtrl);
function AppCtrl() {
var vm = this;
vm.ContextData = {
selectParents: [{ "id": 1, "name": "item 1" },
{ "id": 2, "name": "item 2" },
{ "id": 3, "name": "item 3" },
{ "id": 4, "name": "item 4" }],
selectChildren: [{ "id": 1, "parentId": 1, "name": "1 based on item1" },
{ "id": 2, "parentId": 1, "name": "2 based on item1" },
{ "id": 3, "parentId": 1, "name": "3 based on item1" },
{ "id": 4, "parentId": 1, "name": "4 based on item1" },
{ "id": 5, "parentId": 1, "name": "5 based on item1" },
{ "id": 6, "parentId": 1, "name": "6 based on item1" },
{ "id": 7, "parentId": 1, "name": "7 based on item1" },
{ "id": 8, "parentId": 2, "name": "8 based on item2" },
{ "id": 9, "parentId": 2, "name": "9 based on item2" },
{ "id": 10, "parentId": 2, "name": "10 based on item2" },
{ "id": 11, "parentId": 2, "name": "11 based on item2" },
{ "id": 12, "parentId": 2, "name": "12 based on item2" },
{ "id": 13, "parentId": 3, "name": "13 based on item3" },
{ "id": 14, "parentId": 3, "name": "14 based on item3" },
{ "id": 15, "parentId": 3, "name": "15 based on item3" },
{ "id": 16, "parentId": 3, "name": "16 based on item3" },
{ "id": 17, "parentId": 4, "name": "17 based on item4" },
{ "id": 18, "parentId": 4, "name": "18 based on item4" }]
};
vm.SetSelect2 = function () {
////debugger;
vm.selectedChild = null;
var ele = angular.element('#childSelect2');
//ele.html("");
//ele.val("").trigger("change");
ele.html("").val("").trigger("change");
}
}
})();
I am using syncfusion dropdownlist. I want to create a dropdownlist for blood group in my form. How can I do that in angularjs?
<input type="text" ej-dropdownlist e-datasource="BloodGroup_List" e-value="Name" ng-model="employee.bloodgroup">
In my controller I have written,
$scope.bloodgroup = [{ "Id": "1", "Name": "O+" },
{ "Id": "2", "Name": "O-" },
{ "Id": "3", "Name": "A+" },
{ "Id": "4", "Name": "A-" },
{ "Id": "5", "Name": "B+" },
{ "Id": "6", "Name": "B-" },
{ "Id": "7", "Name": "AB+" },
{ "Id": "8", "Name": "AB-" }];
$scope.BloodGroup_List = $scope.bloodgroup;
The above code is not working, where am I wrong?
You need to map the data source to the corresponding attributes in "fields" property.
<input type="text" id="bloodgroup" ej-dropdownlist e-datasource="BloodGroup_List" e-fields-text="Name" e-fields-value="Name" e-fields-id="Id" e-value="value">
In the script, inject 'ejAngular' and specify your values as shown below
angular.module('DropCtrl', ['ejangular'])
.controller('DropDownCtrl', function ($scope) {
$scope.bloodgroup = [{ "Id": "1", "Name": "O+" },
{ "Id": "2", "Name": "O-" },
{ "Id": "3", "Name": "A+" },
{ "Id": "4", "Name": "A-" },
{ "Id": "5", "Name": "B+" },
{ "Id": "6", "Name": "B-" },
{ "Id": "7", "Name": "AB+" },
{ "Id": "8", "Name": "AB-" }];
$scope.BloodGroup_List = $scope.bloodgroup;
$scope.value = "AB-";
});
This will work for you.
Try this..
<select ng-model="selectedItem">
<option ng-repeat="bloodgroupvalue in bloodgroup" value="{{bloodgroupvalue.Id}}">{{bloodgroupvalue.Name}}</option>
</select>
or
<select ng-model="selectedItem" ng-options="bloodgroupvalue.Name for bloodgroupvalue in bloodgroup">
</select>
You need to use e-fields-text="Name" to show text on in the dropdown then it will bind the value to that ng-model
HTML
<input id="test" type="text" ej-dropdownlist e-datasource="BloodGroup_List" e-fields-text="Name"
e-fields-value="Name" ng-model="employee.bloodgroup" e-change="selectedBloodGroup"/>
CODE
$scope.selectedBloodGroup = function(args) {
$scope.employee.bloodgroup = args.value;
$scope.$apply();
};
Working Plunkr
I am working on an angular app . here i need to show data in date order for user timeline
JS:
$scope.comments = [{"_id":"535e912cc6b93c7b30479454",
"created_at":"2014-04-28 23:04:36",
"post_id":"535e912cc6b93c7b30479453",
"type":"story",
"story":"[{"_id":"535e912cc6b93c7b30479454",
"title":"test story",
"content":"this is my story...",
"created_at":2014-04-30 22:04:36"}]",
"article":"[]"
},
{"_id":"535e912cc6b93c7b30479454",
"created_at":"2014-04-25 23:04:36",
"post_id":"535e912cc6b93c7b30479453",
"type":"article"
"article":"[{"_id":"535e912cc6b93c7b30479454",
"title":"test article",
"content":"this is my article...",
"created_at":2014-04-30 22:04:36"}]",
"story":"[]"
},----
//so on
]
View:
<div ng-repeat = "data in comments">
<div class="time">
<span>{{data.created_at | DateFiltertoIso | date:'MMM-dd-yy'}}
<div>
<div ng-switch-on="data.type">
<div ng-switch-when="story">
//switch layout adn get further story data by post_id
</div>
</div>
<div ng-switch-on="data.type">
<div ng-switch-when="article">
//switch layout adn get further article data by post_id
</div>
</div>
</div>
Expected Output is like
Apr-04-14
post1
post2
/all post for that date
Apr-01-14
post5
post6
//so on
I am getting
Apr-04-2014
post1
Apr-04-2014
post2
Apr-04-2014
post3
I am newbie to angular. Any Help would be helpfull
Try this
Working Demo
Html
<div ng-controller="MyCtrl">
<div ng-repeat="data in flattenedResults">
<div class="time"> <span>
<b>{{data.created_at | badDateToISO | date:'MMM-d-yy'}} </b></span>
<div>
<u ng-show="data.story.length>0">Stories</u>
<div ng-repeat="story in data.story">
{{story.title}}{{story.created_at}}
</div>
<u ng-show="data.article.length>0">Articles</u>
<div ng-repeat="article in data.article">
{{article.title}}
</div>
Script
var myApp = angular.module('myApp', []);
myApp.filter('badDateToISO', function () {
return function (badTime) {
var goodTime = badTime.replace(/(.+) (.+)/, "$1T$2Z");
return goodTime;
};
});
myApp.controller('MyCtrl', function ($scope) {
$scope.flattenedResults = [];
$scope.comments = [{
"_id": "535e912cc6b93c7b30479454",
"created_at": "2014-04-28 23:04:36",
"post_id": "535e912cc6b93c7b30479453",
"type": "story",
"story": [{
"_id": "535e912cc6b93c7b30479454",
"title": "test story 1",
"content": "this is my story... 2",
"created_at": "2014-04-30 22:04:36"
}],
"article": "[]"
}, {
"_id": "535e912cc6b93c7b30479454",
"created_at": "2014-04-28 23:04:36",
"post_id": "535e912cc6b93c7b30479453",
"type": "article",
"article": [{
"_id": "535e912cc6b93c7b30479454",
"title": "test article 33",
"content": "this is my article... 33",
"created_at": "2014-04-30 22:04:36"
}],
"story": "[]"
}, {
"_id": "535e912cc6b93c7b30479454",
"created_at": "2014-04-25 23:04:36",
"post_id": "535e912cc6b93c7b30479453",
"type": "article",
"article": [{
"_id": "535e912cc6b93c7b30479454",
"title": "test article 1",
"content": "this is my article... 1",
"created_at": "2014-04-30 22:04:36"
}],
"story": "[]"
}, {
"_id": "535e912cc6b93c7b30479454",
"created_at": "2014-04-28 23:04:36",
"post_id": "535e912cc6b93c7b30479453",
"type": "story",
"story": [{
"_id": "535e912cc6b93c7b30479454",
"title": "test story 2",
"content": "this is my story... 2",
"created_at": "2014-04-30 22:04:36"
}],
"article": "[]"
}, {
"_id": "535e912cc6b93c7b30479454",
"created_at": "2014-04-25 23:04:36",
"post_id": "535e912cc6b93c7b30479453",
"type": "article",
"article": [{
"_id": "535e912cc6b93c7b30479454",
"title": "test article 2",
"content": "this is my article... 2",
"created_at": "2014-04-30 22:04:36"
}],
"story": "[]"
}];
$scope.jsonFlatten = function () {
$scope.dates = [];
$scope.comments.reduce(function (result, item) {
$scope.dates.push(item.created_at);
}, 0);
$scope.dates = _.uniq($scope.dates);
angular.forEach($scope.dates, function (dateValue, dateKey) {
var obj = {};
obj.created_at = dateValue;
var articleValues = [];
var storyValues = [];
angular.forEach($scope.comments, function (value, key) {
if (dateValue === value.created_at) {
obj._id = value._id;
obj.post_id = value.post_id;
if (value.type === 'story') {
angular.forEach(value.story, function (storyValue, storyKey) {
storyValues.push(storyValue)
});
} else if (value.type === 'article') {
angular.forEach(value.article, function (articleValue, articleKey) {
articleValues.push(articleValue)
});
}
}
});
obj.story = storyValues;
obj.article = articleValues;
$scope.flattenedResults.push(obj);
});
};
$scope.jsonFlatten(); //calling the method jsonFlatten() and making the flatten json
});