Use variable in another variable in Angular? - angularjs

i'm trying to use this component
https://github.com/alexklibisz/angular-dual-multiselect-directive
but I don't know how could I pass data from the back-end to the component.
I get the data from the back-end:
var vm = this;
vm.Categoria1 = [];
$http.get('http://localhost:5541/api/date/ListCategorii').success(function (data) {
vm.Categoria1 = data;
}
);
I get the data:
[{"Id":1,"NumeCategorie":"Fructe"},{"Id":2,"NumeCategorie":"Legume"},{"Id":3,"NumeCategorie":"Ciocolata"}]
but below vm.Categoria1 is empty:
$scope.demoOptions = {
title: 'Demo: Recent World Cup Winners',
filterPlaceHolder: 'Start typing to filter the lists below.',
labelAll: 'All Items',
labelSelected: 'Selected Items',
helpMessage: ' Click items to transfer them between fields.',
/* angular will use this to filter your lists */
orderProperty: 'name',
/* this contains the initial list of all items (i.e. the left side) */
items: vm.Categoria1, //[{ 'id': '50', 'name': 'Germany' }, { 'id': '45', 'name': 'Spain' }, { 'id': '66', 'name': 'Italy' }, { 'id': '30', 'name': 'Brazil' }, { 'id': '41', 'name': 'France' }, { 'id': '34', 'name': 'Argentina' }],
/* this list should be initialized as empty or with any pre-selected items */
selectedItems: []
};
Thank you.

The best practice is to use like this :
var vm = this;
vm.Categoria1 = [];
$http.get('http://localhost:5541/api/date/ListCategorii')
.success(function(data) {
vm.Categoria1 = data;
$scope.demoOptions = {
title: 'Demo: Recent World Cup Winners',
filterPlaceHolder: 'Start typing to filter the lists below.',
labelAll: 'All Items',
labelSelected: 'Selected Items',
helpMessage: ' Click items to transfer them between fields.',
/* angular will use this to filter your lists */
orderProperty: 'name',
/* this contains the initial list of all items (i.e. the left side) */
items: vm.Categoria1, //[{ 'id': '50', 'name': 'Germany' }, { 'id': '45', 'name': 'Spain' }, { 'id': '66', 'name': 'Italy' }, { 'id': '30', 'name': 'Brazil' }, { 'id': '41', 'name': 'France' }, { 'id': '34', 'name': 'Argentina' }],
/* this list should be initialized as empty or with any pre-selected items */
selectedItems: []
})
.error(function(data, status) {
console.error('Repos error', status, data);
})
.finally(function() {
console.log("finally finished repos");
});
Hope that helps ! Sa-mi spui daca ai probleme.

You need to set demoOptions in the success callback:
var vm = this;
vm.Categoria1 = [];
$http.get('http://localhost:5541/api/date/ListCategorii').success(function (data) {
vm.Categoria1 = data;
$scope.demoOptions = {
title: 'Demo: Recent World Cup Winners',
filterPlaceHolder: 'Start typing to filter the lists below.',
labelAll: 'All Items',
labelSelected: 'Selected Items',
helpMessage: ' Click items to transfer them between fields.',
/* angular will use this to filter your lists */
orderProperty: 'name',
/* this contains the initial list of all items (i.e. the left side) */
items: vm.Categoria1, //[{ 'id': '50', 'name': 'Germany' }, { 'id': '45', 'name': 'Spain' }, { 'id': '66', 'name': 'Italy' }, { 'id': '30', 'name': 'Brazil' }, { 'id': '41', 'name': 'France' }, { 'id': '34', 'name': 'Argentina' }],
/* this list should be initialized as empty or with any pre-selected items */
selectedItems: []
};
});

Other option is, you can create one function contains that http.get
call and call that function on page load :
var vm = this;
vm.Categoria1 = [];
var onload = function(){
$http.get('http://localhost:5541/api/date/ListCategorii').success(function (data) {
vm.Categoria1 = data;
}
};
$scope.demoOptions = {
title: 'Demo: Recent World Cup Winners',
filterPlaceHolder: 'Start typing to filter the lists below.',
labelAll: 'All Items',
labelSelected: 'Selected Items',
helpMessage: ' Click items to transfer them between fields.',
/* angular will use this to filter your lists */
orderProperty: 'name',
/* this contains the initial list of all items (i.e. the left side) */
items: vm.Categoria1, //[{ 'id': '50', 'name': 'Germany' }, { 'id': '45', 'name': 'Spain' }, { 'id': '66', 'name': 'Italy' }, { 'id': '30', 'name': 'Brazil' }, { 'id': '41', 'name': 'France' }, { 'id': '34', 'name': 'Argentina' }],
/* this list should be initialized as empty or with any pre-selected items */
selectedItems: []
};
onload();

You should set the items in the request success :
$http.get('http://localhost:5541/api/date/ListCategorii')
.success(function (data) {
$scope.demoOptions.items = vm.Categoria1 = data;
});

Related

Typescript Angular how to map an object of classes to a single entity

I have an array called panels.
It is setup like this
panels = [
{
active: true,
name: 'Company Information',
disabled: false,
tempControls:[]
},
{
active: true,
disabled: false,
name: 'Locations',
tempControls: []
},
{
active: true,
disabled: false,
name: 'Job Information',
tempControls:[]
}
];
I also have an object of objects that is setup like this
export class AssessmentModel{
companyInformationModel? : CompanyInformationModel;
locationInformationModels? :LocationInformationModel[];
jobInformationModel? : JobInformationModel
}
export class CompanyInformationModel{
companyName?:string = '';
contactName?:string = '';
primaryPhone?:string = '';
secondaryPhone?:string = '';
email?:string = '';
hourlyRate? : string = ''
}
I need to map each object to that in the tempControls with the key of 'Name' and 'Value'
so the panels will look like this with tempControls initialized
panels = [
{
active: true,
name: 'Company Information',
disabled: false,
tempControls:[
'Title': '',
'Value': ''
]
}
]
panels = [
{
active: true,
name: 'Company Information',
disabled: false,
tempControls:[
{
'Title': assessmentModel.CompanyInformationModel."Nameofthefield" (example companyname is the first field inthe class"
'Value': assessmentModel.CompanyInformationModel.companyName
},
{
'Title': assessmentModel.CompanyInformationModel."Nameofthefield" (example contactname is the second field in the class"
'Value': assessmentModel.CompanyInformationModel.contactName
}
]},
{
active: true,
name: 'Job Information',
disabled: false,
tempControls:[
{
'Title': assessmentModel.JobInformationModel."Nameofthefield"
'Value': assessmentModel.JobInformationModel.JobTitle
}
]},
{
active: true,
name: 'Location Information',
disabled: false,
tempControls:[
{ 'Title': assessmentModel.LocationInformationModel[0]."Nameofthefield"
'Value': assessmentModel.LocationInformationModel[0].address1
},
{
'Title': assessmentModel.LocationInformationModel[0]."Nameofthefield"
'Value': assessmentModel.LocationInformationModel[0].address2
}]
,
[
{
'Title': assessmentModel.LocationInformationModel[1]."Nameofthefield"
'Value': assessmentModel.LocationInformationModel[1].address1
},
{
'Title': assessmentModel.LocationInformationModel[1]."Nameofthefield"
'Value': assessmentModel.LocationInformationModel[1].address2
}
]
}
So far I have (but of course this doesn't work)
ngOnInit(){
let model = _assesmentModel;
this.setPanels(model);
}
setPanels(model: AssessmentModel){
this.panels.map(x => x.tempControls.map( y => y.Name = model.companyInformationModel.keys())
}

How to construct multiple dictionaries to array in react native

I am trying to show Expand/Collapse Flatlist data in react-native.
It is like single parent with multiple childs. So, If user tap on the cell, I have to show multiple rows for that expand/collapse.
But, I am getting data from server like following.
[{
code: '1212',
name: 'Bajaj Model 1',
url: 'Some url',
category: 'Bike'
},
{
code: '1213',
name: 'Bajaj Model 2',
url: 'other url',
category: 'Bike'
},
{
code: '1454',
name: 'BMW Model 1',
url: 'Some url',
category: 'Car'
},
{
code: '1454',
name: 'BMW Model 2',
url: 'Some url',
category: 'Car'
}
]
So, All the Bike data, I have to show one place like Parent and child.
For that I have done filter.
const fundsFilterData = mapValues(groupBy(response, 'category'),
fundslist => fundslist.map(item => omit(item, 'category')));
And I am getting like following.
{
'Bike': [{
code: '1212',
name: 'Bajaj Model 1',
url: 'Some url'
},
{
code: '1213',
name: 'Bajaj Model 2',
url: 'other url'
},
],
'Car': [{
code: '1454',
name: 'BMW Model 1',
url: 'Some url'
},
{
code: '1454',
name: 'BMW Model 2',
url: 'other url'
},
]
}
But, I want to make it as array along with some pre added keys like following.
[{
'Title': 'Bike',
'Values': [{
'code': '1212',
'name': 'Bajaj Model 1',
'url': 'Some url'
},
{
'code': '1454',
'name': 'Bajaj Model 2',
'url': 'other url'
}
]
},
{
'Title': 'Car',
'Values': [{
'code": '
1454 ',
'name": '
BMW Model 1 ',
'url": '
Some url '
},
{
'code': '1454',
'name': 'BMW Model 2',
'url': 'Some url'
}
]
}
}
So that I can show Titles in headers and once user tap on row, I can show child's data as expanded.
Any suggestions?
Use _.map() which returns an array, and not _.mapValues(). In the _.map() get the Title (the key) from the 2nd param. Use it and the values (after _.omit()) to construct an object for each group.
const { map, groupBy, omit } = _;
const response = [{"code":"1212","name":"Bajaj Model 1","url":"Some url","category":"Bike"},{"code":"1213","name":"Bajaj Model 2","url":"other url","category":"Bike"},{"code":"1454","name":"BMW Model 1","url":"Some url","category":"Car"},{"code":"1454","name":"BMW Model 2","url":"Some url","category":"Car"}];
const fundsFilterData = map(
groupBy(response, 'category'),
(list, Title) => ({
Title,
Values: list.map(item => omit(item, 'category'))
})
);
console.log(fundsFilterData);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>

AngularJS get data from database through controller set as array to show in view

angular.module('app', []).controller('Tree', function ($scope,med){
$scope.med= med.Categories.query();
//query() have service in which data get from db
$scope.data = [{ /* i want this id:med.id */
'id': 1,
'title': 'one',
'parent':0,
'nodes': [
{
'id': 2,
'title': 'wadood',
'parent':1
},
{
'id': 3,
'title': 'nouman',
'parent':1
}
]
}];
}

UI-GRID Dynamic column headers and array of object data

I have following data
vm.myData =
{
'Type' :
[
'A',
'B',
'C'
],
'Data': [
{
'Key': 'XXX',
'AValues':
{
'ID': '1',
'Val': '10'
},
'BValues':
{
'ID': '2',
'Val': '20'
},
'CValues':
{
'ID': '2',
'Val': '20'
}
},
{
'Key': 'TTT',
'AValues':
{
'ID': '2',
'Val': '30'
},
'BValues':
{
'ID': '4',
'Val': '70'
},
'CValues':
{
'ID': '2',
'Val': '20'
}
}
]
};
I am trying to show data as below
Key A B C
XXX 10 20 20
TTT 30 70 20
I tried it many ways but not able to get desired result.
I want the name of the Columns to come from 'Type' on 'myData'.
I am able to display first row by hardcoding.
vm.gridOptions = {
columnDefs: [
{ name: 'Key', field: 'Data[0].Key'},
{ name: 'A', field: 'Data[0].AValues.Val'},
{ name: 'B', field: 'Data[0].BValues.Val'},
{ name: 'C', field: 'Data[0].CValues.Val'}
],
data: vm.myData
};
I would really appreciate any help.
Update
After going through Naren Mulrali's suggestion, I did following which gives me desired result.
But I have hardcoded column names(I need different display names).
Is there a way to dynamically get column header from the 'Type' array from 'myData'
vm.gridOptions = {
columnDefs: [
{ displayName: 'Key', name: 'Key' },
{ displayName: 'A', name: 'AValues.Val' },
{ displayName: 'B', name: 'BValues.Val' },
{ displayName: 'C', name: 'CValues.Val' }
],
data: vm.myData.Data
};
You need to convert your Object into a format that angular ui grid understands, please refer to the below JSFiddle
angular.forEach($scope.myData.Data, function(value, index){
value.AValues = value.AValues.Val;
value.BValues = value.BValues.Val;
value.CValues = value.CValues.Val;
$scope.myData.Data[index] = value;
});
JSFiddle: link

angularjs : I have one form it has many fields some of them carrying array to display dropdown. is it wise that we should provider?

I have one form which has 10 to 12 fields which contains dropdown and multi select drop down. I want before my controller function gets called all the fields should filled up. I tried something like this :
var app = angular.module('app', []);
app.config(function ($provide) {
var transactionType = [
{ id: 0, type: 'card', value: 'card' },
{ id: 1, type: 'cash', value: 'cash' },
{ id: 2, type: 'other', value: 'other' }
];
var friends = [
{ id: 0, name: 'A', value: 'A' },
{ id: 1, name: 'B', value: 'B' },
{ id: 2, name: 'C', value: 'C' },
{ id: 3, name: 'D', value: 'D' }
];
var currency = [
{ id: 0, currency: 'USD', value: 'USD' },
{ id: 0, currency: 'INR', value: 'INR' },
{ id: 0, currency: 'EUR', value: 'EUR' },
];
$provide.value('TransactionType', transactionType);
$provide.value('Friends', friends);
$provide.value('Currency', currency);
});
app.controller('MainCtrl', function ($scope, TransactionType, Friends, Currency) {
});
Is this the right way considering that may be in future I have to use http service to get array from DB and to populate on dropdown list?
I'd use a specific type of provider called a Factory instead. If you use promises in your factory when switch to http services you'd only need to change your factory code - the rest of your application will work without code changes!
angular.module('myApp')
.factory('Friend', function ($q) {
return {
list: function() {
var deferred = $q.defer();
deferred.resolve([
{ id: 0, name: 'A', value: 'A' },
{ id: 1, name: 'B', value: 'B' },
{ id: 2, name: 'C', value: 'C' },
{ id: 3, name: 'D', value: 'D' }
]);
return deferred.promise;
}
};
});

Resources