defaultValue not woking on CheckboxGroupInput - admin-on-rest

I'm trying to write a simple check box group like this:
<CheckboxGroupInput source="test" defaultValue={{ _id: 123 }} choices={[
{ _id: 123, name: 'Leo Tolstoi', sex: 'M' },
{ _id: 456, name: 'Jane Austen', sex: 'F' },
]} />
I couldnt find an example of how to specify the defaultValue for CheckboxGroupInput.
i also tried passing:
defaultValue={{ _id: 123, name: 'Leo Tolstoi', sex: 'M', checked: true }}
defaultValue={[{ _id: 123, name: 'Leo Tolstoi', sex: 'M', checked: true }]}
defaultValue="123"
any leads are appreciated!

Don't forget to set the optionValue prop to _id. The default is id. See the documentation: https://marmelab.com/admin-on-rest/Inputs.html#checkboxgroupinput

Related

How to Append a combination field into nested list using Jinja2?

Below is sample dataset (Could extract them as a dict if that's easier as I'm using Ansible read_csv)
[
{
"id": "1",
"name": "apple",
"quantity": "10",
"type": "fruit"
},
{
"id": "2",
"name": "orange",
"quantity": "20",
"type": "fruit"
},
{
"id": "3",
"name": "carrot",
"quantity": "5",
"type": "veg"
},
{
"id": "4",
"name": "beetroot",
"quantity": "2",
"type": "veg"
}
]
I needed to append a combination field including type-name-id combo to the nested list
So the final outcome expected is
[
{
"uid": "fruit-apple-1",
"id": "1",
"name": "apple",
"quantity": "10",
"type": "fruit"
},
{
"uid": "fruit-orange-2",
"id": "2",
"name": "orange",
"quantity": "20",
"type": "fruit"
},
...
The list is pretty large and hence avoiding the ansible loop and using plain Jinja.
I've tried below, but below pivots into a list of combination field ONLY. But I wanted to retain the nesting rather than a flat list
- jinja_list: |-
{% set uid = [] %}
{% for item in fruits_csv_list.list %}
{% set li = item.type + '-' + item.name + '-' + item.id %}
{{uid.append(li)}}
{%- endfor %}
{{uid}}
Any idea how to achieve it efficiently?
You just have to combine the current item with the k/v you need for the current uid. In a nutshell:
jinja_list: |-
{% set result = [] %}
{% for item in fruits_csv_list.list %}
{% set li = {'uid': [item.type, item.name, item.id] | join('-')} %}
{{ result.append(item | combine(li)) }}
{%- endfor %}
{{ result }}
Given the list
f_list:
- {id: '1', name: apple, quantity: '10', type: fruit}
- {id: '2', name: orange, quantity: '20', type: fruit}
- {id: '3', name: carrot, quantity: '5', type: veg}
- {id: '4', name: beetroot, quantity: '2', type: veg}
Create the list of uid, zip the lists, and combine the dictionaries
f_uid: "{{ f_list|zip(f_list|json_query(q_uid))|map('combine') }}"
q_uid: '[].{uid: [type, name, id]|join(`-`, #)}'
gives
f_uid:
- {id: '1', name: apple, quantity: '10', type: fruit, uid: fruit-apple-1}
- {id: '2', name: orange, quantity: '20', type: fruit, uid: fruit-orange-2}
- {id: '3', name: carrot, quantity: '5', type: veg, uid: veg-carrot-3}
- {id: '4', name: beetroot, quantity: '2', type: veg, uid: veg-beetroot-4}
Example of a complete playbook for testing
- hosts: localhost
vars:
f_list:
- {id: '1', name: apple, quantity: '10', type: fruit}
- {id: '2', name: orange, quantity: '20', type: fruit}
- {id: '3', name: carrot, quantity: '5', type: veg}
- {id: '4', name: beetroot, quantity: '2', type: veg}
f_uid: "{{ f_list|zip(f_list|json_query(q_uid))|map('combine') }}"
q_uid: '[].{uid: [type, name, id]|join(`-`, #)}'
tasks:
- debug:
var: f_uid|to_yaml

elasticsearch 5: returning same document multiple times if multiple array items of one document fit condition

We are using elastic search 5 (yes I know it is EOL but it is what it is)
Elastic newbie here with a rather complicated task:
Let's imagine we have an event index filled with multiple event documents. Those event documents might have n speakers attached in a speakers object array. A speaker has a gender attribute.
How can I achieve the following query?
"return event for each speaker whose gender is x "
returning query result:
{
page: 1,
pageSize: 20,
total: 3
items: [
{
id: 1,
speakers: [
{
name: 'Sascha',
gender: 'x'
}
]
},
{
id: 1,
speakers: [
{
name: 'Leo',
gender: 'x'
}
]
},
{
id: 2,
speakers: [
{
name: 'Rue',
gender: 'x'
}
]
},
]
}
event index filled with three event documents
event 1
{
id: 1,
speakers: [
{
name: 'Sascha',
gender: 'x'
},
{
name: 'Leo',
gender: 'x'
},
]
}
event 2
{
id: 2,
speakers: [
{
name: 'Thomas',
gender: 'm'
},
{
name: 'Rue',
gender: 'x'
},
]
}
event 3
{
id: 2,
speakers: [
{
name: 'Nicole',
gender: 'f'
}
]
}
I didn't even start to code or write a query because I am not sure if elastic is built to give me a result like the one I want to achieve.
I don't even know how to articulate the type of query I am trying to achieve :/
Or do I have to create a new index which combines event and speaker index in a way I can work with?

How do I remove duplicate items from observable array based on multiple properties?

If I have an array of items like
[
{id: 1, name: 'Sam', gender: 'boy'},
{id: 2, name: 'Mary', gender: 'girl'},
{id: 3, name: 'Sam', gender: 'boy'}
]
Matching on just name and gender, how do I reduce it to the following result?
[
{id: 1, name: 'Sam', type: 'boy'},
{id: 2, name: 'Mary', type: 'girl'}
]
Let try
items$.pipe(map(this.uniqueArray))
uniqueArray(array: any[]): any[] {
return array.filter(
(item, index, self) =>
index === self.findIndex((x) => x.name === item.name)
);
}
https://stackblitz.com/edit/angular-isqjpa?file=src/app/hello.component.ts

How to merge array of hashes based on hash value but not merge values instead override

I have an array of hashes like this:
[
{ name: 'Pratha', email: 'c#f.com' },
{ name: 'John', email: 'j#g.com' },
{ name: 'Clark', email: 'x#z.com' },
]
And this is second group array of hashes:
[
{ name: 'AnotherNameSameEmail', email: 'c#f.com' },
{ name: 'JohnAnotherName', email: 'j#g.com' },
{ name: 'Mark', email: 'd#o.com' },
]
What I want is, merge these two arrays into one, merge based on :email and keep latest (or first) :name.
Expected Result (latest name overrided):
[
{ name: 'AnotherNameSameEmail', email: 'c#f.com' },
{ name: 'JohnAnotherName', email: 'j#g.com' },
{ name: 'Mark', email: 'd#o.com' },
{ name: 'Clark', email: 'x#z.com' },
]
or (first name preserved)
[
{ name: 'Pratha', email: 'c#f.com' },
{ name: 'John', email: 'j#g.com' },
{ name: 'Mark', email: 'd#o.com' },
{ name: 'Clark', email: 'x#z.com' },
]
So, basically, I want to group by :email, retain one :name, drop dupe emails.
The examples found on SO is creates an array of values for :name.
Ruby 2.6.3
Maybe you could just call Array#uniq with a block on email key of the concatenation (Array#+) of the two arrays:
(ary1 + ary2).uniq { |h| h[:email] }
a1 = [
{ name: 'Pratha', email: 'c#f.com' },
{ name: 'John', email: 'j#g.com' },
{ name: 'Clark', email: 'x#z.com' },
]
a2 = [
{ name: 'AnotherNameSameEmail', email: 'c#f.com' },
{ name: 'JohnAnotherName', email: 'j#g.com' },
{ name: 'Mark', email: 'd#o.com' },
]
Let's first keep the last:
(a1+a2).each_with_object({}) { |g,h| h.update(g[:email]=>g) }.values
#=> [{:name=>"AnotherNameSameEmail", :email=>"c#f.com"},
# {:name=>"JohnAnotherName", :email=>"j#g.com"},
# {:name=>"Clark", :email=>"x#z.com"},
# {:name=>"Mark", :email=>"d#o.com"}]
To keep the first, do the same with (a1+a2) replaced with (a2+a1), to obtain:
#=> [{:name=>"Pratha", :email=>"c#f.com"},
# {:name=>"John", :email=>"j#g.com"},
# {:name=>"Mark", :email=>"d#o.com"},
# {:name=>"Clark", :email=>"x#z.com"}]

Ruby map specific hash keys to new one

I've got an array full of hashes of which I want to combine specific keys to a new one, e.g.
[{ firstname: 'john', lastname: 'doe', something: 'else', key: ... }, { firstname: 'Joe', lastname: 'something', something: 'bla', key:... }]
should become
[{ name: 'john doe' },{ name: 'Joe something' }]
Please note: there are more keys in the hash as first and lastname. Is there a common ruby method to do this? Thanks!
Just do as
array = [{ firstname: 'john', lastname: 'doe' }, { firstname: 'Joe', lastname: 'something' }]
array.map { |h| { :name => h.values_at(:firstname, :lastname) * " " } }
# => [{:name=>"john doe"}, {:name=>"Joe something"}]
Read this Hash#values_at and Array#* .
This is:
a = [{ firstname: 'john', lastname: 'doe' }, { firstname: 'Joe', lastname: 'something' }]
a.map { |n| { name: n.values.join(' ') } }
# => [{:name=>"john doe"}, {:name=>"Joe something"}]

Resources