Prime NG multi select init value cannot see check box selected - primeng

Prime NG multi select init value cannot see check box selected
init method
this.cities = [
{label: 'New York', value: 'NY'},
{label: 'Rome', value: 'RM'},
{label: 'London', value: 'LDN'},
{label: 'Istanbul', value: 'IST'},
{label: 'Paris', value: 'PRS'}];
let selectedT: Country[];
selectedT = [{label: "Rome", value: "RM"}];
this.dForm.controls["selectedCities"].setValue([{label: "Rome", value: "RM"}]);

No need to pass the whole object to "selectedCities" Array. Pass the only value to an array.
selectedT = ["RM"];
this.dForm.controls["selectedCities"].setValue(["RM"]);

Related

Finding a property value in an array of object based on another property value

I have the following array
const arrayOfObject = [{
name: 'Mike',
age: '38',
}, {
name: 'Russell',
age: '42',
}, {
name: 'John',
age: '26',
}, ];
and I want to find the age of 'Russell' for instance.
If I use .find() I manage to get the whole object
const employeeAge = arrayOfObject.find((item) => item.name === 'Russell')
Is it possible to do it in one line?

Remove duplicates while creating an array with multiple properties in TypeScript

I receive an array from an API which looks as follows:
results = [
{name: 'Ana', country: 'US', language: 'EN'},
{name: 'Paul', country: 'UK', language: 'EN'},
{name: 'Luis', country: 'PH', language: 'SP'},
{name: 'Tom', country: 'US', language: 'EN'}
];
From this I'd like to create an array that looks like this:
countries = [
{filter: 'country', value: 'PH'},
{filter: 'country', value: 'UK'},
{filter: 'country', value: 'US'},
];
To this end, what I tried is:
countries = Array.from([...new Set(this.results.map(item => ({categoryOfFilter: 'country', value: item.country})))]);
Because I was told to use set. This does create an array as specified above, but it contains duplicates. Like so:
countries = [
{filter: 'country', value: 'US'},
{filter: 'country', value: 'UK'},
{filter: 'country', value: 'PH'},
{filter: 'country', value: 'US'},
];
Do you guys have any idea? The truth is that I was never any good with js in the first place so I'm waaay beyond stretching here.
The Set object lets you store unique values of any type, whether
primitive values or object references.
Sets don't work like that with objects. The items are all different, as they have different object references, even if their attributes all have equal values.
It will work like that (split it into two line to keep it a bit readable)
results = [
{name: 'Ana', country: 'US', language: 'EN'},
{name: 'Paul', country: 'UK', language: 'EN'},
{name: 'Luis', country: 'PH', language: 'SP'},
{name: 'Tom', country: 'US', language: 'EN'}
];
// create a set with all country codes. Set works fine with strings
const countryCodes = new Set(results.map(item => item.country));
// spread the set values into a new array and map that to the target objects
const countries = [...countryCodes].map(value => {return {filter: 'country', value}});
console.log(countries);
See this codepen: https://codepen.io/kyletanders/pen/NWqpWVX?editors=0012
something like this:
const data = [
{name: 'Ana', country: 'US', language: 'EN'},
{name: 'Paul', country: 'UK', language: 'EN'},
{name: 'Luis', country: 'PH', language: 'SP'},
{name: 'Tom', country: 'US', language: 'EN'}
];
let unique = [...new Set(data.map(item => item.country))].map(x => {return {filter: 'Country', value: x}});
console.log(unique);

How to filter all the fields in react?

How to filter all the fields in react?
I have written a code which only filters specified fields.
let filteredContacts=this.props.contacts.filter(
(contact)=>{
return contact.name.indexOf(this.state.search)!==-1;
}
);
the above code only returns filtered names...but i want to filter name,age,city work.
this is the data file:
export default function(){
return [{
key: 1,
name: 'Steve',
city: 'Paris',
}, {
key: 2,
name: 'Tim',
city: 'London',
}, {
key: 3,
name: 'Stella',
city: 'Bankok',
}, {
key: 4,
name: 'John',
city: 'Paris',
}];
}
i want to create a search bar which which filters all the fields suppose if i search john then the list of john should be displayed and if i search paris then all the entries which have paris should be displayed..So the code which i have written only searches for name as i have specified name..i want to search the inputed data to search all the fields.
The filter criteria can be the indexOf on Object.values() like below but it will only give a complete match
var data = [{
key: 1,
name: 'Steve',
city: 'Paris',
}, {
key: 2,
name: 'Tim',
city: 'London',
}, {
key: 3,
name: 'Stella',
city: 'Bankok',
}, {
key: 4,
name: 'John',
city: 'Paris',
}];
var value = 'Paris'
var newData = data.filter((obj) => {
return Object.values(obj).indexOf(value) > -1
})
console.log(newData)
Well, I fastly tried, do a loop on your keys. Maybe there's a more elegant way.
let filteredContacts=this.props.contacts.filter(
(contact)=>{
var found = false;
Object.keys(contact).map(function(key, index) {
if (contact[key].indexOf(this.state.search) >= 0)
found = true;
}
return found;
}
);

OrderBy with enumerable property in AngularJS

I have an object array where every object can have an array of properties, like this
[
{
name: 'Big house',
properties: [{name: 'Area',value: '400'}, {name: 'Year',value: '1950'}]
},
{
name: 'Small house',
properties: [{name: 'Area',value: '400'}, {name: 'Year',value: '1950'}]
},
{
name: 'Green house',
properties: [{name: 'Area',value: '40'}, {name: 'Year',value: '2008'}]
},
{
name: 'Red house',
properties: [{name: 'Area',value: '250'}, {name: 'Year',value: '1999'}]
},
];
Now I'd like to order this list by one of the properties, say Area, using a filter in ng-repeat. Is that possible?
I've been play around with this in plunker (http://plnkr.co/edit/GEgLxv5zJyW0ZBTtJR5S?p=preview) but cannot figure out how to do.
Thanks in advance
This will work if the area is always the first property:
<tr ng-repeat="house in houses | orderBy:'properties[0].value'">
Also make sure that the area is defined as an integer, right now they are strings. So 250 will be sorted before 40.
So remove the quotes from the numbers:
{
name: 'Area',
value: 400
}
, {
name: 'Year',
value: '1950'
}

Empty element in Angular ng-options (with and without grouping)

I have (a seemingly common) problem with empty option values an angular model viewed using a select using ng-options.
$scope.groupTypeOptions = [
{ group: 'g1', name: '---', value: null },
{ group: 'g2', name: 'Feature', value: 'feature' },
{ group: 'g2', name: 'Bug', value: 'bug' },
{ group: 'g2', name: 'Enhancement', value: 'enhancement' }
];
<select ng-model='form.groupType' required ng-options='option.value as option.name group by option.group for option in groupTypeOptions'></select>
A fiddle can be seen here:
http://jsfiddle.net/v6z3zh49/
My goal is to, from a predefined model, show a grouped select with the selected item representing null (or empty string). However, when selecting a value and I always end up with an extra, empty, option element being added. See fiddle above.
I have looked at similar questions, for example here and here, but cannot find a solution.
Any tips?
Try:
$scope.groupTypeOptions = [
{ group: 'g1', name: '---', value: undefined },
{ group: 'g2', name: 'Feature', value: 'feature' },
{ group: 'g2', name: 'Bug', value: 'bug' },
{ group: 'g2', name: 'Enhancement', value: 'enhancement' }
];
It happens because in javascript null !== undefined.
Hope, it will help.
Demo: http://jsfiddle.net/ababashka/skk4uj1y/

Resources