How to use React-widget Dropdownlist with groupby attribute - html

I need to use react-widgets dropdownlist with groupby attribute.
A typical example of this would be;
<Multiselect
data=[{name:'Dan' lastName:'Black'}, {name:'Man' lastName:'Black'}]
textField='name'
groupBy='lastName'
/>
But the data array i have of is of a completely different structure. like;
[
{
name:'test one',
objects: [{key:'my key', value:'my value'},
{key:'my key1', value:'my value1'}
{key:'my key2', value:'my value2'}]
},
{
name:'test two',
objects: [{key:'my key', value:'my value'},
{key:'my key1', value:'my value1'}
{key:'my key2', value:'my value2'}]
}
]
And i need the 'key' to be the value displayed in the list and groupby on 'name'.
Is this possible to achieve or just completely stupid and i have to restructure it totally?
Each objects array is honestly 100 or more objects long.. so i prefer not to restructure it again due to performance issue.
Thanks in advance, All ideas are welcome!
UPDATE:
<DropdownList
data = {
[{
repoName: 'google repository',
objects: [
{
key: 'mykey',
method: 'my meth',
value: 'my val'
},
{
key: 'mykey2',
method: 'my meth2',
value: 'my val2'
}]
}]}
textField='objects.key'
placeholder={placeholder}
groupBy='repoName'
/>
Gives something like;
google repository
[Object Object]
How could iterate that object array to avoid this?

According to my understanding, what you can do is create a different structure for data using your existing one.
const data = [{
name: 'test one',
objects: [{
key: 'my key',
value: 'my value'
}, {
key: 'my key1',
value: 'my value1'
} {
key: 'my key2',
value: 'my value2'
}]
}, {
name: 'test two',
objects: [{
key: 'my key',
value: 'my value'
}, {
key: 'my key1',
value: 'my value1'
} {
key: 'my key2',
value: 'my value2'
}]
}]
const newData = []
data.forEach((element) => {
const name = element.name
if (element.objects && element.object.length) {
element.objects.forEach((keyValueData) => {
newData.push({
name,
key: keyValueData.key,
value: keyValueData.value,
})
})
}
})
< Multiselect
data = {
newData
}
textField = 'name'
groupBy = 'key' / >
This way, it should solve your issue.Mind brackets not tested the code

Related

How to show antd multiple select component on this Simple Schema

I have this JSON schema, I tried to populate multiple select component with the uniform autoform.
(() => {
const ajv = new Ajv({ allErrors: true, useDefaults: true, keywords: ["uniforms"] });
const schema = {
title: 'Address',
type: 'object',
properties: {
city: {
type: 'string',
uniforms: {
options: [
{ label: 'New York', value: 'new york' },
{ label: 'Nashville', value: 'nashville' },
],
},
},
}
};
function createValidator(schema) {
const validator = ajv.compile(schema);
return (model) => {
validator(model);
if (validator.errors && validator.errors.length) {
return { details: validator.errors };
}
};
}
const schemaValidator = createValidator(schema);
return new JSONSchemaBridge(schema, schemaValidator);
})()
And the result look like this
Rendered component with this JSON schema
The multiselect component example from antd
could I render multiselect component instead select component (which default from uniform)?
Can I select new york and nashville at the same time?

Convert Form Data Array Object to JSON [duplicate]

This question already has answers here:
Convert JS object to JSON string
(23 answers)
Closed 1 year ago.
I'm trying to convert a form data object to JSON
var dataFormItin = $("#formitinerary").serializeArray();
result
itinerary: {
'itinerary[0][date]': undefined,
'itinerary[0][title]': 'Day 1 to Colombo',
'itinerary[0][destinationId]': '5ff3b8d7f0f3bf04b8141362',
'itinerary[0][program]': 'asd',
'itinerary[0][transfer_duration]': '2 hours'
}
and i want to make it like
itinerary : [
{ date : ..., title :...},
{ date : ..., title :...}
]
Maybe, it will be solved your problem
// 1. Example Data
const serializeArray = [
{ name: "itinerary[0][date]", value: "" },
{ name: "itinerary[0][title]", value: "Day 1 to Colombo" },
{ name: "itinerary[0][destinationId]", value: "5ff3b8d7f0f3bf04b8141362" },
{ name: "itinerary[0][program]", value: "asd" },
{ name: "itinerary[1][date]", value: "" },
{ name: "itinerary[1][title]", value: "Day 1 to Colombo" },
{ name: "itinerary[1][destinationId]", value: "5ff3b8d7f0f3bf04b8141362" },
{ name: "itinerary[1][program]", value: "asd" },
]
// 2. Define object key here
const arrayOfKey = ['date', 'title', 'destinationId', 'program']
// 3. Create empty array object
const arrayObject = []
// 4. Transform Serialize Array into Array Object
for(i = 0; i < serializeArray.length / arrayOfKey.length; i++ ){
const newObject = {}
for(const key of arrayOfKey){
newObject[key] = (serializeArray.find(data => data.name == `itinerary[${i}][${key}]`)).value
}
arrayObject.push(newObject)
}
// 5. Show the result
console.log(arrayObject)
/**
* [
{
date: '',
title: 'Day 1 to Colombo',
destinationId: '5ff3b8d7f0f3bf04b8141362',
program: 'asd'
},
{
date: '',
title: 'Day 1 to Colombo',
destinationId: '5ff3b8d7f0f3bf04b8141362',
program: 'asd'
}
]
*/

Json Schema validate reference property on same object

Using JSON Schema 7 to perform validations
Is the below validation possible using json schema.
{
properties : [{name: "a"}, {name: "b"}, {name: "c"}],
rules : [{ prop : ["a","b"] }, { prop : ["a"] }, {prop: ["c"]}]
}
The "prop" property in object is dependent values in properties.
ie only of "properties.name" exists then that value can be added to the "prop" array
Note:
The "properties" array can have any object of type {name : }
"name" can have any possible string, which i don't know beforehand
I have been going through documentation, but can find a answer.
Is this validation not supported in Json Schema yet?
You can't do it with a static JSON schema.
To archive it you would need a dynamic schema validation, but this could be dangerous to code injection from malicious users:
const Ajv = require('ajv')
const ajv = new Ajv({ allErrors: true, jsonPointers: true })
const data = {
properties: [{ name: 'a' }, { name: 'b' }, { name: 'c' }],
rules: [{ prop: ['a', 'b'] }, { prop: ['a', 'zz'] }, { prop: ['c'] }]
}
const validProp = data.properties.map(_ => _.name)
const schema = {
type: 'object',
required: ['properties', 'rules'],
properties: {
properties: {
type: 'array',
items: {
type: 'object',
required: ['name'],
properties: {
name: { type: 'string' }
}
}
},
rules: {
type: 'array',
items: {
type: 'object',
required: ['prop'],
properties: {
prop: {
type: 'array',
uniqueItems: true,
items: {
type: 'string',
enum: validProp // here happen the validation
}
}
}
}
}
}
}
const isValid = ajv.validate(schema, data)
if (!isValid) {
console.log(ajv.errors)
}

How to delete an object from a json in angular 2 and ionic

export default [
{
user:{
id: '1',
person: 'Theodore Roosevelt',
text: 'Believe you can and you\'re halfway there',
icon:'man'
}
},
{
user:{
id: '2',
person: 'Normale',
text: 'Change your thoughts and you change your world.',
icon:'woman'
}
}, {
user:{
id: '3',
person: 'Thlt',
text: 'Believe you can and you\'re halfway there',
icon:'man'
}
}]
The code above is a ts file under data folder in ionic tool.
I wish to delete an entry from this array on basis of id by click of delete button in front of each entry.
I'm new to ionic . I tried .slice it didn't work
const arr = [{
user:{
id: '1',
person: 'Theodore Roosevelt',
text: 'Believe you can and you\'re halfway there',
icon:'man'
}
},
{
user:{
id: '2',
person: 'Normale',
text: 'Change your thoughts and you change your world.',
icon:'woman'
}
}, {
user:{
id: '3',
person: 'Thlt',
text: 'Believe you can and you\'re halfway there',
icon:'man'
}
}]
const id = '2'; // 2 will be removed
const result = arr.filter(item => (item.user || {}).id !== id)
console.log(result)

How to access this specific JSON key

This is my JSON file from which I want to access "tr_name" which is inside var InspectorDefs but I can't find the way.Any help please?
JSON FILE:
var InspectorDefs = {
'link': {
inputs: {
attrs: {
'.attributes': {
'tr_name': { type: 'text', group: 'attributes', label: 'Name', index: 1 },
'tr_description': { type: 'text', group: 'attributes', label: 'Description', index: 2 },
'tr_rules': { type: 'select', options: [], group: 'attributes', label: 'Rule', index: 3 },
'tr_value': { type: 'select', options: ['true', 'false'], group: 'attributes', label: 'Value', index: 4 },
'tr_rule_source': { type: 'select', options: ['BPM', 'Drools', 'iLog'], group: 'attributes', label: 'Rule source', index: 5 }
},
},
},
},
};
I want to pass tr_name path here but I am desperate:
cell.on('change:tr_name', function(cell, change, opt) {})
if you are just looking for how to access your properties in javascript then you would reference it like this
InspectorDefs.link.inputs.attrs[".attributes"].tr_description
JSON objects can be referenced as properties or dictionaries, i am assuming you are having issues with the ".attributes"?
JsFiddle
you can use [] as in array or map.
var tr_name = InspectorDefs['link']['inputs']['attrs']['.attributes']['tr_name'];
But in your case I guess you want to call some function when the attribute of some html tag changed. so you can use the actual html tag generated by this json file which can be found from the web page generated or from my guess it is: <Name> tag as read from the json file