I want to access inputed Value in JSON array using javascript - javascript

i created array 'PROPERTIES' which accept value like username, password,sid etc.And i want to access this entered value using javascript.
'PROPERTIES': {'gatewayurl':
{'Name':
{
'en': 'Input gatewayurl'
},
'Description': {
'en': 'Input gatewayurl for sms gateway'
},
'Type': 'string',
'Required': 'Y',
'Multiple': 'N',
'Default': '',
},
'username':
{'Name':
{
'en': 'Input username'
},
'Description': {
'en': 'Input username'
},
'Type': 'string',
'Required': 'Y',
'Multiple': 'N',
'Default': '{=Document:NAME}',
},
'password':
{'Name':
{
'en': 'Input string'
},
'Description': {
'en': 'Input password'
},
'Type': 'string',
'Required': 'Y',
'Multiple': 'N',
'Default': '{=Document:NAME}',
},
'sid':
{'Name':
{
'en': 'Input string'
},
'Description': {
'en': 'Input sid'
},
'Type': 'string',
'Required': 'Y',
'Multiple': 'N',
'Default': '{=Document:NAME}',
},
'mobileno':
{'Name':
{
'en': 'Input mobileno'
},
'Description': {
'en': 'Input mobileno'
},
'Type': 'number',
'Required': 'Y',
'Multiple': 'N',
'Default': null,
},
'message':
{'Name':
{
'en': 'Input message'
},
'Description': {
'en': 'Input message'
},
'Type': 'string',
'Required': 'Y',
'Multiple': 'N',
'Default': null,
}
},
i Used php code But i want Access value using only Javascript Please Some one Help me
$url =$_REQUEST["properties"]["gatewayurl"];
$user = $_REQUEST["properties"]["username"];
$password = $_REQUEST["properties"]["password"];
$sid = $_REQUEST["properties"]["sid"];
$number= $_REQUEST["properties"]["mobileno"];
$message= $_REQUEST["properties"]["message"];

If "PROPERTIES" is an array as you were saying you should initialize it as an array.
var PROPERTIES = [ {'gatewayurl' ........... }]
Then you can get values by
var url = PROPERTIES[0].gatewayurl;
var user = PROPERTIES[0].username;
var password = PROPERTIES[0].password;
var sid = PROPERTIES[0].sid;
etc..
If its a JSON Array as shown in your example,
var REQUEST = {
'PROPERTIES': {'gatewayurl' ... }
}
Then you can get values by
var url = REQUEST["properties"]["gatewayurl"];
var user = REQUEST["properties"]["username"];
var password = REQUEST["properties"]["password"];
var sid = REQUEST["properties"]["sid"];

Related

JS: Do I override or add in fields when use _.extend() in express?

When I have an object and I put it as an extension of another object in which I put value for some of the extended fields - is it going to be rewritten or the value will be added to the old one?
For example if I have:
const PATCH_REQUEST_SCHEMA = {
'type': 'object',
'title': 'Some object',
'description': 'Some object Representation',
'properties': {
'name': {
'type': 'string',
'minLength': 1,
'maxLength': 256,
'title': 'Name'
}
};
const POST_REQUEST_SCHEMA = _.extend({}, PATCH_REQUEST_SCHEMA, {
'properties': {
'surname': {
'type': 'string',
'minLength': 1,
'maxLength': 256,
'title': 'Surname'
}
}
});
What would be the result for POST_REQUEST_SCHEMA.properties ?
Will it be:
{
'name': {
'type': 'string',
'minLength': 1,
'maxLength': 256,
'title': 'Name'
},
'surname': {
'type': 'string',
'minLength': 1,
'maxLength': 256,
'title': 'Surname'
}
}
or:
{
'surname': {
'type': 'string',
'minLength': 1,
'maxLength': 256,
'title': 'Surname'
}
}
I tested it manually - it rewrites/overrides the field "properties". So the answer is:
{
'surname': {
'type': 'string',
'minLength': 1,
'maxLength': 256,
'title': 'Surname'
}
}

Conversational Form with React. How to make multiple choice?

I am working with React(Next.js). I have a good example [https://codepen.io/space10/pen/JMXzGX][1] how to use multiple choise in simple HTML, but how to use it in React?
Here is my code
Here what I have in browser
Just keep adding objects into the array - as long as the name is the same, it seems to combine it.
Working Demo: https://stackblitz.com/edit/react-conversational-form-with-radio
this.formFields = [
{
'tag': 'input',
'type': 'radio',
'name': 'country',
'cf-questions': 'What is your country?',
'cf-label': 'ukraine',
'value': 'ukraine',
},
{
'tag': 'input',
'type': 'radio',
'name': 'country',
'cf-label': 'india',
'value': 'india',
},
{
'tag': 'input',
'type': 'radio',
'name': 'country',
'cf-label': 'sri lanka',
'value': 'sri lanka',
},
{
'tag': 'input',
'type': 'text',
'name': 'lastname',
'cf-questions': 'What is your lastname?'
}
];
Returns the form result something like this:
{ country: ["ukraine"], lastname: 'tasdasd' }

Angular ngModel, 3 arrays are affected

I am creating a form builder in Angular, I used Lam Phuoc ThinhI work for reference, check his work here
<https://codepen.io/thinhlam/pen/BowEPp>
Now the problem is I have this 3 arrays, Current Field, Form Fields and Draggable Fields. Everytime I dragged a field, like for example the Header Field and then I changed the text or value of that field, the Draggable Fields is also affected. So if i dragged another Header field the value is the same with the first header field that i drag. Please check the image below.
image
app.component.html
<div *ngFor="let set of CurrentField.Settings; let i = index; trackBy:trackByIdx" [ngSwitch]="set.Type">
<div *ngSwitchCase="'textarea'" class="form-group">
<label for="textarea-{{i}}">{{set.Name}}</label>
<input [name]="'textarea' + i" type="text" class="form-control" [(ngModel)]="set.Value">
</div>
</div>
app.component.ts
FormFields: any;
CurrentField: any;
guid: number;
DragElements = [{
'Name': "Header",
"Type": "textLabel",
'Icon': "fa fa-header",
'Settings': [{
'Name': 'Field Label',
'Value': 'Header',
'Type': 'textarea'
}]
}, {
'Name': "Columns",
"Type": "columns",
'Icon': "fa fa-columns",
'Settings': [{
'Name': 'Number of Columns',
'Value': 0,
'PossibleValue': [2,3,4]
}, {
'Name': 'Column',
'Columns': []
}],
}, {
'Name': "Image",
"Type": "image",
'Icon': "fa fa-image",
'Settings': [{
'Name': 'Field Label',
'Value': 'Image',
'Type': 'textarea'
}]
}, {
'Name': "Single Text",
'Type': "text",
'Icon': "fa fa-font",
'Settings': [{
'Name': 'Field Label',
'Value': 'Single Text',
'Type': 'text'
}]
}, {
'Name': "Number",
'Type': "number",
'Icon': "fa fa-th-large",
'Settings': [{
'Name': 'Field Label',
'Value': 'Number',
'Type': 'text'
}]
}, {
'Name': "Date",
'Type': "date",
'Icon': "fa fa-calendar",
'Settings': [{
'Name': 'Field Label',
'Value': 'Date',
'Type': 'text'
}]
}, {
'Name': "Single Selection",
"Type": "dropdown",
'Icon': "fa fa-dot-circle-o",
'Settings': [{
'Name': 'Field Label',
'Value': 'Single Selection',
'Type': 'text'
}]
}, {
'Name': "Pagaraph Text",
"Type": "textarea",
'Icon': "fa fa-align-left",
'Settings': [{
'Name': 'Field Label',
'Value': 'Paragraph Text',
'Type': 'text'
}]
}];
constructor(
private cd: ChangeDetectorRef,
private zone: NgZone,
) {}
ngOnInit() {
this.CurrentField = {};
this.FormFields = [];
this.guid = 1;
}
createNewField = function() {
return {
'id': ++this.guid,
'Name': '',
'Settings': [],
'Active': true,
'ChangeFieldSetting': function(Value, SettingName) {
switch (SettingName) {
// case 'Field Label':
// this.Settings[0].Value = Value;
// // console.log(Value);
// break;
case 'Image Alignment':
this.Settings[2].DefaultAlignment = Value;
break;
case 'Columns':
let columns : any = {};
this.Settings[0].Value = Value;
for(let i = 0; i < Value; i++) {
columns = {
'ID': i + "-" + this.id,
'Title': 'Column '+i,
'Items': []
}
this.Settings[1].Columns.push(columns);
}
console.log(this);
break;
default:
break;
}
},
'GetFieldSetting': function(settingName) {
let result = {};
let settings = this.Settings;
settings.forEach((index, set) => {
if (index.Name == settingName) {
result = index;
return;
}
});
if (!Object.keys(result).length) {
settings[settings.length - 1].Options.forEach((index, set) => {
if (index.Name == settingName) {
result = index;
return;
}
});
}
return result;
}
};
}
changeFieldName = function(Value) {
this.CurrentField.Name = Value;
this.CurrentField.Settings[0].Value = this.CurrentField.Name;
}
removeElement = function(idx){
if(this.FormFields[idx].Active) {
this.CurrentField = {};
}
this.FormFields.splice(idx, 1);
};
addElement(ele, idx) {
// this.zone.run(() => {
this.CurrentField.Active = false;
this.CurrentField = this.createNewField();
Object.assign(this.CurrentField, ele);
if (idx == null) {
this.FormFields.push(this.CurrentField);
} else {
this.FormFields.splice(idx, 0, this.CurrentField);
}
// });
};
activeField = function(f) {
this.CurrentField.Active = false;
this.CurrentField = f;
f.Active = true;
};
Wherever you are using
Object.assign(this.CurrentField,ele);
like operations
deep clone before pushing.
Object.assign(this.CurrentField, JSON.parse(JSON.stringify(ele)));
This is one way of deep cloning which has its pros and cons. But your solution will be to deep clone using any suitable method.

Update method with post

I have to create an Update method with a post request.
But when I did it , create an another object.
this is the python code from the server for edit class
def edit_schema(cls, admin: bool = False, cm: bool = False):
if cm:
return {
'id': {'type': 'number', 'min': 1},
'client': {'type': 'number', 'required': False, 'min': -1, 'forbidden': [0]},
'led_spec': {'type': 'string', 'required': False}
}
elif admin:
return {
'id': {'type': 'number', 'min': 1},
'client': {'type': 'number', 'required': False, 'dependencies': 'group', 'min': -1, 'forbidden': [0]},
'name': {'type': 'string', 'required': False, 'empty': False, 'maxlength': 255},
'led_spec': {'type': 'string', 'required': False}
# }
else:
return {
'id': {'type': 'number', 'min': 1},
'name': {'type': 'string', 'required': False, 'empty': False, 'maxlength': 255}
}
My service.ts
public update(fixture: Fixture){
let body = {};
if(fixture.name !== '' && fixture.name !== null)
body['name'] = fixture.name;
if(fixture.client !== -1 && fixture.client !== null)
body['client'] = fixture.client;
if(fixture.group !== -1 && fixture.group !== null)
body['group'] = fixture.group;
return this
.post('fixture/edit', body).toPromise().then(data => {
this._refreshNeeded$.next();
})
}
and my component
update() {
if (this.form.valid) {
this.formSubmitAttempt = true;
this.fixtureService.update(this.form.value);
this.dialogRef.close(this.form.value);
console.log(this.form.value)// send updated value to main component
}
}
```

Angular.Copy does not allow me to create unique objects in a separate array

I'm building a configurator in AngularJS that uses the object $scope.data to allow users to edit the object via the front-end. The user can then save this object to a separate array in $scope.builds, allowing them to have multiple configurations. Here is the function that adds another configuration to $scope.builds.
$scope.addition = function(){
var newData = angular.copy($scope.data);
$scope.builds.push(newData);
}
Unfortunately, despite using the angular.copy function, all of the objects in the array of $scope.builds seem to be the same $scope.data object duplicated over and over.
EDIT:
Here is an abridged version of what $scope.data looks like:
$scope.data = [
{
'title': 'Select your configuration',
'required': true,
'options': [
{
'name': 'Option 1',
'select': true,
'filter': true
}, {
'name': 'Option 2',
'select': false,
'filter': true
}, {
'name': 'Option 3',
'select': false,
'filter': true
}
]
}, {
'title': 'Select your configuration',
'required': true,
'options': [
{
'name': 'Option 1',
'select': true,
'filter': true
}, {
'name': 'Option 2',
'select': false,
'filter': true
}, {
'name': 'Option 3',
'select': false,
'filter': true
}
]
}
];

Categories

Resources