Moving JSon Response into Dropdown - angularjs

I have a Json Response as given below
JSON :
{
"json": {
"response": {
"servicetype": "1",
"functiontype": "10011",
"statuscode": "0",
"statusmessage": "Success",
"data": {
"assignedroles": [
{
"roleid": 162,
"rolename": "Admin",
"roleinformation": {
"A": [
{
"1st floor": 10
}
]
}
},
{
"roleid": 163,
"rolename": "Resident",
"roleinformation": {
"A": [
{
"1st floor": 10
}
]
}
}
],
"unassignedroles": [
{
"roleid": 36,
"rolename": "Product Managers",
"divisionlabel": "Department ",
"subdivisionlabel": "Category",
"roleinformation": {
"QA": [
{
"White Box Testing": 0
}
]
}
},
{
"roleid": 108,
"rolename": "Teacher",
"divisionlabel": "Class",
"subdivisionlabel": "Div",
"roleinformation": ""
},
{
"roleid": 115,
"rolename": "Staff",
"divisionlabel": "Class",
"subdivisionlabel": "Section",
"roleinformation": {
"1": [
{
"A": 0
}
]
}
},
{
"roleid": 116,
"rolename": "Student",
"divisionlabel": "Class",
"subdivisionlabel": "Section",
"roleinformation": {
"1": [
{
"A": 0
}
]
}
},
{
"roleid": 184,
"rolename": "CANt_MANAGE_Gbase",
"divisionlabel": "Division",
"subdivisionlabel": "SubDivision",
"roleinformation": {
"Div": [
{
"Sdiv": 0
},
{
"Sdiv1": 0
}
]
}
},
{
"roleid": 191,
"rolename": "Legend",
"divisionlabel": "a",
"subdivisionlabel": "b"
}
]
}
}
}
}
need to get rolename of both assigned and unassigned values from json response. I am able to get in two arrays, but couldn't work with moving both.
HTML:
<div class="form-group">
<label class="control-label col-sm-10" for="groupz">Select Role*</label>
<select class="form-control" name="role"
ng-model="model.rolename"
ng-change="GetAssignRole(model.rolename)">
<option selected>Select Roles</option>
<option ng-repeat="item in model.roles track by $index"
value="{{item}}">{{item}}</option>
</select>
<div>
JS:
UserService.getAssignRoles(json).then(function(response) {
if (response.json.response.statuscode == 0 && response.json.response.statusmessage == 'Success')
{
$scope.model.assignroles = [];
var resdata =response.json.response.data;
var unasresdata = response.json.response.data.unassignedroles;
var assresdata = response.json.response.data.assignedtoles;
$scope.model.assignroles.push(unasresdata);
$scope.model.assignroles.push(assresdata);
}
});
};
I couldn't figure out how to move both the datas into dropdown. If something is empty in assigned or unassigned , it gives undefined in response.But I am able to get two arrays inside assignRoles .

Concat unassignedroles[] and assignedroles[] to make single array.Just try below code.It might help u.
UserService.getAssignRoles(json).then(function(response) {
if (response.json.response.statuscode == 0 && response.json.response.statusmessage == 'Success')
{
$scope.model.assignroles = [],assignrolesArray = [];
var unasresdata = response.json.response.data.unassignedroles;
var assresdata = response.json.response.data.assignedtoles;
assignrolesArray = unasresdata.concat(assresdata);
$scope.model.assignroles = assignrolesArray;
}
});
HTML
<div class="form-group">
<label class="control-label col-sm-10" for="groupz">Select Role*</label>
<select class="form-control" name="role"
ng-model="model.rolename"
ng-change="GetAssignRole(model.rolename)">
<option selected>Select Roles</option>
<option ng-repeat="role in model.assignroles track by $index"
value="{{role.rolename}}">{{role.rolename}}</option>
</select>
<div>

Related

react axios and react-hook form problem put in json

I am a student who is practicing react js, for this I am developing a calendar in which you can put notes, as you will see there are some notes that I have put by hand. I have installed axios to work the json and the get of the data does it well, but when I try to set a new data it gives me this error.
the browser console shows me this error
PATCH http://localhost:5000/January/3/alarms/ 404 (Not Found) Uncaught
(in promise) Error: Request failed with status code 404 at
createError (createError.js:16) at settle (settle.js:17) at
XMLHttpRequest.handleLoad (xhr.js:62)
this is my JSOn
{
"January": [
{
"id": 1,
"alarms": [
{
"name": "Medico",
"initHour": "12:00",
"endHour": "12:35"
},
{
"name": "fisio",
"initHour": "16:00",
"endHour": "17:00"
},
{
"name": "desayuno con Rick",
"initHour": "8:00",
"endHour": "9:00"
}
]
},
{
"id": 2,
"alarms": [
{
"name": "Desayuno",
"initHour": "9:00",
"endHour": "9:35"
},
{
"name": "banco",
"initHour": "11:00",
"endHour": "12:00"
}
]
},
{
"id": 3,
"alarms": []
},{
"id": 4,
"alarms": []
},
{
"id": 5,
"alarms": []
},
{
"id": 6,
"alarms": []
},
{
"id": 7,
"alarms": []
},
{
"id": 8,
"alarms": []
},
{
"id": 9,
"alarms": [
{
"name": "fisio",
"initHour": "16:00",
"endHour": "17:00"
}
]
},
{
"id": 10,
"alarms": []
},
{
"id": 11,
"alarms": []
},
{
"id": 12,
"alarms": []
},
{
"id": 13,
"alarms": []
},
{
"id": 14,
"alarms": []
},
{
"id": 15,
"alarms": []
},
{
"id": 16,
"alarms": [
{
"name": "fisio",
"initHour": "16:00",
"endHour": "17:00"
}
]
},
{
"id": 17,
"alarms": []
},
{
"id": 18,
"alarms": []
},
{
"id": 19,
"alarms": []
},
{
"id": 20,
"alarms": []
},
{
"id": 21,
"alarms": []
},
{
"id": 22,
"alarms": []
},
{
"id": 23,
"alarms": [
{
"name": "fisio",
"initHour": "16:00",
"endHour": "17:00"
}
]
},
{
"id": 24,
"alarms": []
},
{
"id": 25,
"alarms": []
},
{
"id": 26,
"alarms": []
},
{
"id": 27,
"alarms": []
},
{
"id": 28,
"alarms": []
},
{
"id": 29,
"alarms": []
},
{
"id": 30,
"alarms": [
{
"name": "fisio",
"initHour": "16:00",
"endHour": "17:00"
}
]
},
{
"id": 31,
"alarms": []
}
]
}
and this is my code
import React, {useEffect, useState} from "react";
import './Calendar.scss';
import axios from "axios";
import Day from "./Day/Day";
import Modal from 'react-modal';
import { useForm } from 'react-hook-form';
const customStyles = {
content : {
top : '50%',
left : '50%',
right : 'auto',
bottom : 'auto',
marginRight : '-50%',
transform : 'translate(-50%, -50%)'
}
};
export default function Calendar() {
let subtitle;
const { register, handleSubmit} = useForm();
const [days, setDays] = useState([]);
const [modalIsOpen,setIsOpen] = useState(false);
const [id, setId] = useState([]);
Modal.setAppElement('#root')
useEffect(() => {
axios.get('http://localhost:5000/January').then(res => {
setDays(res.data);
});
}, [])
const onSubmit = form => {
console.log('http://localhost:5000/January/' + id + '/alarms', form);
axios.patch('http://localhost:5000/January/' + id + '/alarms/', form).then(res => {
console.log(res);
});
}
function openModal(id) {
setIsOpen(true);
setId(id);
}
function afterOpenModal() {
// references are now sync'd and can be accessed.
subtitle.style.color = 'rgb(72, 181, 163)';
}
function closeModal(){
setIsOpen(false);
}
return (
<div className="b-calendar">
<h3 className="b-calendar__title">January</h3>
<div className="b-calendar__headers">
<div>Monday</div>
<div>Tuesday</div>
<div>Wednesday</div>
<div>Thursday</div>
<div>Friday</div>
<div>Saturday</div>
<div>Sunday</div>
</div>
<div className="b-calendar__content">
<div className="b-december"><h1>28</h1></div>
<div className="b-december"><h1>29</h1></div>
<div className="b-december"><h1>30</h1></div>
<div className="b-december"><h1>31</h1></div>
{days.map((item, index) =>
<div key={index+1} id={index+1} onClick={() => openModal(item.id)}>
<Day items={item}/>
</div>
)}
</div>
<Modal
isOpen={modalIsOpen}
onAfterOpen={afterOpenModal}
onRequestClose={closeModal}
style={customStyles}
contentLabel="Example Modal"
>
<h2 ref={_subtitle => (subtitle = _subtitle)}>AƱadir Cita</h2>
<form className="b-loginForm" onSubmit={handleSubmit(onSubmit)}>
<label htmlFor="initHour">
<span className="b-text-label">Init hour</span>
<input id="initHour" className="b-input" type="time" name="initHour" ref={register({ required: true})} />
</label>
<label htmlFor="endHour">
<span className="b-text-label">End hour</span>
<input id="endHour" className="b-input" type="time" name="endHour" ref={register()} />
</label>
<label htmlFor="name">
<span className="b-text-label">Alarms name</span>
<input id="name" className="b-input" type="text" name="name" ref={register({ required: true})} />
</label>
<input className="b-btn" type="submit" value="Guardar" />
<button onClick={closeModal}>Salir</button>
</form>
</Modal>
</div>
)
}```
[1]: https://i.stack.imgur.com/PDVPD.png

Get a object from multidimensional array and display it on dropdown - AngularJS

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>

AngularJS Dropdown Multiselect adding custom button

I am currently stuck in a situation where I have to add a button to drop down options but can't find any help to add custom button to drop down options using this library
main.js
$scope.example14model = [];
$scope.example14settings = {
scrollableHeight: '200px',
scrollable: true,
enableSearch: true
};
$scope.example14data = [{
"label": "Alabama",
"id": "AL"
}, {
"label": "Alaska",
"id": "AK"
}
];
main.html
<div ng-dropdown-multiselect=""
options="example14data"
selected-model="example14model"
checkboxes="true"
extra-settings="example14settings"></div>
How can I add button to my drop down options? Any help will be highly appreciated.
I think this is what you are looking for :
https://jsfiddle.net/michaeldeongreen/22et6sao/9/
JS
'use strict';
var app = angular.module('myApp', ['angularjs-dropdown-multiselect']);
app.controller('AppCtrl', function ($scope) {
$scope.example14model = [];
$scope.example14settings = {
scrollableHeight: '200px',
scrollable: true,
enableSearch: true
};
$scope.example14data = [{
"label": "Alabama",
"id": "AL"
}, {
"label": "Alaska",
"id": "AK"
}, {
"label": "American Samoa",
"id": "AS"
}, {
"label": "Arizona",
"id": "AZ"
}, {
"label": "Arkansas",
"id": "AR"
}, {
"label": "California",
"id": "CA"
}, {
"label": "Colorado",
"id": "CO"
}, {
"label": "Connecticut",
"id": "CT"
}, {
"label": "Delaware",
"id": "DE"
}, {
"label": "District Of Columbia",
"id": "DC"
}, {
"label": "Federated States Of Micronesia",
"id": "FM"
}, {
"label": "Florida",
"id": "FL"
}, {
"label": "Georgia",
"id": "GA"
}, {
"label": "Guam",
"id": "GU"
}, {
"label": "Hawaii",
"id": "HI"
}, {
"label": "Idaho",
"id": "ID"
}, {
"label": "Illinois",
"id": "IL"
}, {
"label": "Indiana",
"id": "IN"
}, {
"label": "Iowa",
"id": "IA"
}, {
"label": "Kansas",
"id": "KS"
}, {
"label": "Kentucky",
"id": "KY"
}, {
"label": "Louisiana",
"id": "LA"
}, {
"label": "Maine",
"id": "ME"
}, {
"label": "Marshall Islands",
"id": "MH"
}, {
"label": "Maryland",
"id": "MD"
}, {
"label": "Massachusetts",
"id": "MA"
}, {
"label": "Michigan",
"id": "MI"
}, {
"label": "Minnesota",
"id": "MN"
}, {
"label": "Mississippi",
"id": "MS"
}, {
"label": "Missouri",
"id": "MO"
}, {
"label": "Montana",
"id": "MT"
}, {
"label": "Nebraska",
"id": "NE"
}, {
"label": "Nevada",
"id": "NV"
}, {
"label": "New Hampshire",
"id": "NH"
}, {
"label": "New Jersey",
"id": "NJ"
}, {
"label": "New Mexico",
"id": "NM"
}, {
"label": "New York",
"id": "NY"
}, {
"label": "North Carolina",
"id": "NC"
}, {
"label": "North Dakota",
"id": "ND"
}, {
"label": "Northern Mariana Islands",
"id": "MP"
}, {
"label": "Ohio",
"id": "OH"
}, {
"label": "Oklahoma",
"id": "OK"
}, {
"label": "Oregon",
"id": "OR"
}, {
"label": "Palau",
"id": "PW"
}, {
"label": "Pennsylvania",
"id": "PA"
}, {
"label": "Puerto Rico",
"id": "PR"
}, {
"label": "Rhode Island",
"id": "RI"
}, {
"label": "South Carolina",
"id": "SC"
}, {
"label": "South Dakota",
"id": "SD"
}, {
"label": "Tennessee",
"id": "TN"
}, {
"label": "Texas",
"id": "TX"
}, {
"label": "Utah",
"id": "UT"
}, {
"label": "Vermont",
"id": "VT"
}, {
"label": "Virgin Islands",
"id": "VI"
}, {
"label": "Virginia",
"id": "VA"
}, {
"label": "Washington",
"id": "WA"
}, {
"label": "West Virginia",
"id": "WV"
}, {
"label": "Wisconsin",
"id": "WI"
}, {
"label": "Wyoming",
"id": "WY"
}];
$scope.example2settings = {
displayProp: 'id'
};
});
var directiveModule = angular.module('angularjs-dropdown-multiselect', []);
directiveModule.directive('ngDropdownMultiselect', ['$filter', '$document', '$compile', '$parse',
function ($filter, $document, $compile, $parse) {
return {
restrict: 'AE',
scope: {
selectedModel: '=',
options: '=',
extraSettings: '=',
events: '=',
searchFilter: '=?',
translationTexts: '=',
groupBy: '#'
},
template: function (element, attrs) {
var checkboxes = attrs.checkboxes ? true : false;
var groups = attrs.groupBy ? true : false;
var template = '<div class="multiselect-parent btn-group dropdown-multiselect">';
template += '<button type="button" class="dropdown-toggle" ng-class="settings.buttonClasses" ng-click="toggleDropdown()">{{getButtonText()}} <span class="caret"></span></button>';
template += '<ul class="dropdown-menu dropdown-menu-form" ng-style="{display: open ? \'block\' : \'none\', height : settings.scrollable ? settings.scrollableHeight : \'auto\' }" style="overflow: scroll" >';
template += '<li ng-hide="!settings.showCheckAll || settings.selectionLimit > 0"><a data-ng-click="selectAll()"><span class="glyphicon glyphicon-ok"></span> {{texts.checkAll}}</a>';
template += '<li ng-show="settings.showUncheckAll"><a data-ng-click="deselectAll();"><span class="glyphicon glyphicon-remove"></span> {{texts.uncheckAll}}</a></li>';
template += '<li ng-hide="(!settings.showCheckAll || settings.selectionLimit > 0) && !settings.showUncheckAll" class="divider"></li>';
template += '<li ng-show="settings.enableSearch"><div class="dropdown-header"><input type="text" class="form-control" style="width: 100%;" ng-model="searchFilter" placeholder="{{texts.searchPlaceholder}}" /></li>';
template += '<li ng-show="settings.enableSearch" class="divider"></li>';
if (groups) {
template += '<li ng-repeat-start="option in orderedItems | filter: searchFilter" ng-show="getPropertyForObject(option, settings.groupBy) !== getPropertyForObject(orderedItems[$index - 1], settings.groupBy)" role="presentation" class="dropdown-header">{{ getGroupTitle(getPropertyForObject(option, settings.groupBy)) }}</li>';
template += '<li ng-repeat-end role="presentation">';
} else {
template += '<li role="presentation" ng-repeat="option in options | filter: searchFilter">';
}
template += '<a role="menuitem" tabindex="-1" ng-click="setSelectedItem(getPropertyForObject(option,settings.idProp))">';
if (checkboxes) {
template += '<div class="checkbox"><label><input class="checkboxInput" type="checkbox" ng-click="checkboxClick($event, getPropertyForObject(option,settings.idProp))" ng-checked="isChecked(getPropertyForObject(option,settings.idProp))" /> {{getPropertyForObject(option, settings.displayProp)}}</label></div></a>';
} else {
template += '<span data-ng-class="{\'glyphicon glyphicon-ok\': isChecked(getPropertyForObject(option,settings.idProp))}"></span> {{getPropertyForObject(option, settings.displayProp)}}</a>';
}
template += '</li>';
template += '<li class="divider" ng-show="settings.selectionLimit > 1"></li>';
template += '<li role="presentation" ng-show="settings.selectionLimit > 1"><a role="menuitem">{{selectedModel.length}} {{texts.selectionOf}} {{settings.selectionLimit}} {{texts.selectionCount}}</a></li>';
template += '</ul>';
template += '</div>';
element.html(template);
},
link: function ($scope, $element, $attrs) {
var $dropdownTrigger = $element.children()[0];
$scope.toggleDropdown = function () {
$scope.open = !$scope.open;
};
$scope.checkboxClick = function ($event, id) {
$scope.setSelectedItem(id);
$event.stopImmediatePropagation();
};
$scope.externalEvents = {
onItemSelect: angular.noop,
onItemDeselect: angular.noop,
onSelectAll: angular.noop,
onDeselectAll: angular.noop,
onInitDone: angular.noop,
onMaxSelectionReached: angular.noop
};
$scope.settings = {
dynamicTitle: true,
scrollable: false,
scrollableHeight: '300px',
closeOnBlur: true,
displayProp: 'label',
idProp: 'id',
externalIdProp: 'id',
enableSearch: false,
selectionLimit: 0,
showCheckAll: true,
showUncheckAll: true,
closeOnSelect: false,
buttonClasses: 'btn btn-default',
closeOnDeselect: false,
groupBy: $attrs.groupBy || undefined,
groupByTextProvider: null,
smartButtonMaxItems: 0,
smartButtonTextConverter: angular.noop
};
$scope.texts = {
checkAll: 'Check All',
uncheckAll: 'Uncheck All',
selectionCount: 'checked',
selectionOf: '/',
searchPlaceholder: 'Search...',
buttonDefaultText: 'Select',
dynamicButtonTextSuffix: 'checked'
};
$scope.searchFilter = $scope.searchFilter || '';
if (angular.isDefined($scope.settings.groupBy)) {
$scope.$watch('options', function (newValue) {
if (angular.isDefined(newValue)) {
$scope.orderedItems = $filter('orderBy')(newValue, $scope.settings.groupBy);
}
});
}
angular.extend($scope.settings, $scope.extraSettings || []);
angular.extend($scope.externalEvents, $scope.events || []);
angular.extend($scope.texts, $scope.translationTexts);
$scope.singleSelection = $scope.settings.selectionLimit === 1;
function getFindObj(id) {
var findObj = {};
if ($scope.settings.externalIdProp === '') {
findObj[$scope.settings.idProp] = id;
} else {
findObj[$scope.settings.externalIdProp] = id;
}
return findObj;
}
function clearObject(object) {
for (var prop in object) {
delete object[prop];
}
}
if ($scope.singleSelection) {
if (angular.isArray($scope.selectedModel) && $scope.selectedModel.length === 0) {
clearObject($scope.selectedModel);
}
}
if ($scope.settings.closeOnBlur) {
$document.on('click', function (e) {
var target = e.target.parentElement;
var parentFound = false;
while (angular.isDefined(target) && target !== null && !parentFound) {
if (_.contains(target.className.split(' '), 'multiselect-parent') && !parentFound) {
if (target === $dropdownTrigger) {
parentFound = true;
}
}
target = target.parentElement;
}
if (!parentFound) {
$scope.$apply(function () {
$scope.open = false;
});
}
});
}
$scope.getGroupTitle = function (groupValue) {
if ($scope.settings.groupByTextProvider !== null) {
return $scope.settings.groupByTextProvider(groupValue);
}
return groupValue;
};
$scope.getButtonText = function () {
if ($scope.settings.dynamicTitle && ($scope.selectedModel.length > 0 || (angular.isObject($scope.selectedModel) && _.keys($scope.selectedModel).length > 0))) {
if ($scope.settings.smartButtonMaxItems > 0) {
var itemsText = [];
angular.forEach($scope.options, function (optionItem) {
if ($scope.isChecked($scope.getPropertyForObject(optionItem, $scope.settings.idProp))) {
var displayText = $scope.getPropertyForObject(optionItem, $scope.settings.displayProp);
var converterResponse = $scope.settings.smartButtonTextConverter(displayText, optionItem);
itemsText.push(converterResponse ? converterResponse : displayText);
}
});
if ($scope.selectedModel.length > $scope.settings.smartButtonMaxItems) {
itemsText = itemsText.slice(0, $scope.settings.smartButtonMaxItems);
itemsText.push('...');
}
return itemsText.join(', ');
} else {
var totalSelected;
if ($scope.singleSelection) {
totalSelected = ($scope.selectedModel !== null && angular.isDefined($scope.selectedModel[$scope.settings.idProp])) ? 1 : 0;
} else {
totalSelected = angular.isDefined($scope.selectedModel) ? $scope.selectedModel.length : 0;
}
if (totalSelected === 0) {
return $scope.texts.buttonDefaultText;
} else {
return totalSelected + ' ' + $scope.texts.dynamicButtonTextSuffix;
}
}
} else {
return $scope.texts.buttonDefaultText;
}
};
$scope.getPropertyForObject = function (object, property) {
if (angular.isDefined(object) && object.hasOwnProperty(property)) {
return object[property];
}
return '';
};
$scope.selectAll = function () {
$scope.deselectAll(false);
$scope.externalEvents.onSelectAll();
angular.forEach($scope.options, function (value) {
$scope.setSelectedItem(value[$scope.settings.idProp], true);
});
};
$scope.deselectAll = function (sendEvent) {
sendEvent = sendEvent || true;
if (sendEvent) {
$scope.externalEvents.onDeselectAll();
}
if ($scope.singleSelection) {
clearObject($scope.selectedModel);
} else {
$scope.selectedModel.splice(0, $scope.selectedModel.length);
}
};
$scope.setSelectedItem = function (id, dontRemove) {
var findObj = getFindObj(id);
var finalObj = null;
if ($scope.settings.externalIdProp === '') {
finalObj = _.find($scope.options, findObj);
} else {
finalObj = findObj;
}
if ($scope.singleSelection) {
clearObject($scope.selectedModel);
angular.extend($scope.selectedModel, finalObj);
$scope.externalEvents.onItemSelect(finalObj);
if ($scope.settings.closeOnSelect) $scope.open = false;
return;
}
dontRemove = dontRemove || false;
var exists = _.findIndex($scope.selectedModel, findObj) !== -1;
if (!dontRemove && exists) {
$scope.selectedModel.splice(_.findIndex($scope.selectedModel, findObj), 1);
$scope.externalEvents.onItemDeselect(findObj);
} else if (!exists && ($scope.settings.selectionLimit === 0 || $scope.selectedModel.length < $scope.settings.selectionLimit)) {
$scope.selectedModel.push(finalObj);
$scope.externalEvents.onItemSelect(finalObj);
}
if ($scope.settings.closeOnSelect) $scope.open = false;
};
$scope.isChecked = function (id) {
if ($scope.singleSelection) {
return $scope.selectedModel !== null && angular.isDefined($scope.selectedModel[$scope.settings.idProp]) && $scope.selectedModel[$scope.settings.idProp] === getFindObj(id)[$scope.settings.idProp];
}
return _.findIndex($scope.selectedModel, getFindObj(id)) !== -1;
};
$scope.externalEvents.onInitDone();
}
};
}]);
HMTL
<div ng-app="myApp" ng-controller="AppCtrl">
<div ng-dropdown-multiselect="" options="example14data" selected-model="example14model" checkboxes="true" extra-settings="example14settings"></div> <pre>Selected Model: {{example14model}} | json</pre>

Matching values from different objects in the same array with angularJs

I have this json below, if there where more objects with more countries and and medals, what would would be the best approach in angular to match medals won to specific countries?
[
{
"athlete": "KOGO, Micah",
"country": "KEN",
"sex": "Men",
"event": "10000m",
"medal": "Bronze"
},
{
"athlete": "BEKELE, Kenenisa",
"country": "ETH",
"sex": "Men",
"event": "10000m",
"medal": "Gold"
},
{
"athlete": "SIHINE, Sileshi",
"country": "ETH",
"sex": "Men",
"event": "10000m",
"medal": "Silver"
},
{
"athlete": "FLANAGAN, Shalane",
"country": "USA",
"sex": "Women",
"event": "10000m",
"medal": "Bronze"
}
]
This creates an unordered list of countries with counts of the medals.
var app = angular.module("yourApp", []);
app.controller("controller", function($scope) {
$scope.input = [{
"athlete": "KOGO, Micah",
"country": "KEN",
"sex": "Men",
"event": "10000m",
"medal": "Bronze"
},
{
"athlete": "BEKELE, Kenenisa",
"country": "ETH",
"sex": "Men",
"event": "10000m",
"medal": "Gold"
},
{
"athlete": "SIHINE, Sileshi",
"country": "ETH",
"sex": "Men",
"event": "10000m",
"medal": "Silver"
},
{
"athlete": "FLANAGAN, Shalane",
"country": "USA",
"sex": "Women",
"event": "10000m",
"medal": "Bronze"
}
];
$scope.countries = $scope.input.map(function(obj) {
return obj.country;
});
$scope.uniqueCountries = $scope.countries.filter(function(item, pos) {
return $scope.countries.indexOf(item) == pos;
})
$scope.getMedalCount = function(country, colour) {
var count = 0;
angular.forEach($scope.input, function(obj) {
if ((obj.country === country) && (obj.medal === colour)) {
count++;
}
});
return count;
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="yourApp">
<div ng-controller="controller">
<ul>
<li ng-repeat="country in uniqueCountries">
{{ country }}
<ul>
<li>Gold: {{ getMedalCount(country, "Gold") }}</li>
<li>Silver: {{ getMedalCount(country, "Silver") }}</li>
<li>Bronze: {{ getMedalCount(country, "Bronze") }}</li>
</ul>
</li>
</ul>
</div>
</body>

group subdocuments - angularjs

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/

Resources