Angular 2 remove or add data to form.value object - forms

I need to remove "toys" from form.value object before submit, and add new data to 'price'. But controls must be declared.
form.value object
{
"red": 1,
"green": 3,
"black": "120",
"blue": 3,
"toys": [
{
"bear": 0,
"soldier": 0,
"car": 0
}
],
"price": [
{
"default": 123,
"pre": 3,
"after": 2
},
{
"default": 3,
"pre": 0,
"after": 0
}
]
}
ts
initForm() {
this.form = this._fb.group({
red: 0,
green: 0,
black: '',
blue: 0,
toys: this._fb.array([this.inittoys()]),
price: this._fb.array([this.initprice()]),
});
html
<div class="form-group">
<label for="black">Max travel time</label>
<select class="form-control" id="black" formControlName="black">
<option *ngFor="let t of colors; let i=index" [ngValue]="i">{{t}}</option>
</select>
</div>

You can modify the values in the function before you send the form like this:
<form [formGroup]="yourForm" (ngSubmit)="yourSendFunction(yourForm.value)">
....
in the component:
yourSendFunction(values) {
delete values.toys;
values.price.push({
// Here anything you wants to addd
})
// Next send the values
}

Related

Fusion Grid Column Formatter does not parse HTML, instead it prints actual HTML in the Grid

I am trying to get Fusion Grid (https://www.fusioncharts.com/fusiongrid )to work from the example in the online docs. However, it does not parse the HTML. It just prints the unparsed HTML in the column. The grid does not parse the HTML. You can see this problem in F-Series data (last column) in the example below, as well as using a column formatter, see myFunction2. None of the online docs have complete work. Is this a bug or am I missing something?
<!doctype html>
<html>
<head>
<!-- FusionGrid JS files -->
<script src="https://cdn.fusioncharts.com/fusiongrid/latest/fusiongrid.js"></script>
<link rel="stylesheet" href="https://cdn.fusioncharts.com/fusiongrid/latest/fusiongrid.css">
<script type="text/javascript">
var schema = [
{
name: 'Rank',
type: 'number',
},
{
name: 'Model'
},
{
name: 'Make'
},
{
name: 'Units Sold',
},
{
name: 'Assembly Location'
},
{
name: 'Link',
// type: 'html'
}
];
var data = [
[1, "F-Series", "Ford", 896526, "Claycomo, Mo.", "<a href='http://www.google.com>Edit</a>'"],
[2, "Pickup", "Ram", 633694, "Warren, Mich."],
[3, "Silverado", "Chevrolet", 575600, "Springfield, Ohio"],
[4, "RAV4", "Toyota", 448071, "Georgetown, Ky."],
[5, "CR-V", "Honda", 384168, "Greensburg, Ind."],
[6, "Rogue", "Nissan", 350447, "Smyrna, Tenn."],
[7, "Equinox", "Chevrolet", 346048, "Arlington, Tex."],
[8, "Camry", "Toyota", 336978, "Georgetown, Ky."],
[9, "Civic", "Honda", 325650, "Greensburg, Ind."],
[10, "Corolla", "Toyota", 304850, "Blue Springs, Miss."],
[11, "Accord", "Honda", 267567, "Marysville, Ohio"],
[12, "Tacoma", "Toyota", 248801, "San Antonio, Tex."],
[13, "Grand Cherokee", "Jeep", 242969, "Detroit, Mich."],
[14, "Escape", "Ford", 241338, "Louisville, Ky."],
[15, "Highlander", "Toyota", 239438, "Princeton, Ind."],
[16, "Sierra", "GMC", 232325, "Flint, Mich."],
[17, "Wrangler", "Jeep", 228032, "Toledo, Ohio"],
[18, "Altima", "Nissan", 209183, "Smyrna, Tenn."],
[19, "Cherokee", "Jeep", 191397, "Belvidere, Ill."],
[20, "Sentra", "Nissan", 184618, "Canton, Miss."],
];
function myFunction2(params) {
console.log(params.cellValue);
let value = params.cellValue;
let bgColor = '#ff0000';
if (params.cellValue > 346048 && params.cellValue <= 575600) {
bgColor = '#ffff00'
} else if (params.cellValue > 575601) {
bgColor = '#00ff00'
}
//return '{background-color: ' + bgColor + '}';
return ('<span style="background-color: "' + bgColor + '/>' + value + '</span>');
}
function render() {
// Getting the grid-container
var container = document.getElementById('grid-container');
// Passing data through DataStore
var dataStore = new FusionGrid.DataStore();
var dataTable = dataStore.createDataTable(data, schema, {enableIndex: false });
var grid = new FusionGrid(container, dataTable,
{
defaultColumnOptions: {
searchable: true,
filter: {
enable: true,
type: "conditional"
},
},
columns: [
{
field: 'Rank' ,
},
{
field: 'Model' ,
},
{
field: 'Make' ,
},
{
field: 'Units Sold',
type: 'number',
formatter: myFunction2,
},
{
field: 'Assembly Location',
},
{
field: 'Link',
// type: 'html'
}
],
});
// Render the grid
grid.render();
}
</script>
</head>
<body onload="render()">
<h1>Hello from FusionGrid!</h1>
<div id="grid-container" style="width: 100%; height: 450px;"></div>
</body>
</html>
I would expect that <span style="background-color: would be evaluated to actual HTML output, not unprocessed HTML
Thanks to Cesar for finding a solution: use a template with HTML type, not a formatter, as was incorrectly shown in the online documentation:
{
field: 'Units Sold',
type: 'html',
template: myFunction2,
},
use type html and template
{
field: 'Units Sold',
type: 'html',
template: myFunction2
}

echarts: bar chart show the value on the right align

as the picture, I hope show the value in the right align, but did not found option. any help? thanks
You can simply add another axis to achieve this, like demo below:
It kind of like a trick.
let echartsObj = echarts.init(document.querySelector('#canvas'));
let seriesData = [1, 1, 2, 3, 4, 6, 8];
option = {
color: ['#3398DB'],
grid: {
containLabel: true
},
yAxis : [
{
type : 'category',
data : ['192.168.167', '192.168.167', '192.168.167', '192.168.167', '192.168.167', '192.168.12.15', '192.168.167'],
},
{
type : 'category',
data : seriesData,
axisLine: {
show: false
},
axisTick: {
show: false
}
}
],
xAxis : [
{
type : 'value',
splitLine: {
show: false
}
}
],
series : [
{
type:'bar',
data:seriesData
}
]
};
echartsObj.setOption(option)
<html>
<header>
<script src="https://cdn.bootcss.com/echarts/4.1.0.rc2/echarts-en.min.js"></script>
</header>
<body>
<div id="canvas" style="width: 100%; height: 400px">
</div>
</body>
</html>

Angular2 patch input value inside formGroup

I need update inputs value inside each form group after server responce
TS
initAddressRows() {
return this._fb.group({
lat: 0,
lon: 0,
maxTravelTime: this.maxTime[0],
});
}
bindDATA(index) {
this.service.getData().subscribe(
(response) => {this.actualData = response.json()
this.lon = this.actualData.x;
this.lat = this.actualData.y;
// UPDATE THERE 'lat' and 'lon' inputs
},
(error) => console.log('ERROR: ' + error)
);
}
form.value
"addresses": [
{
"lat": 0,
"lon": 0,
"maxTravelTime": "5 min",
},
{
"lat": 0,
"lon": 0,
"maxTravelTime": "5 min",
},
{
"lat": 0,
"lon": 0,
"maxTravelTime": "5 min",
}
]
html
<div class="row" *ngFor="let itemrow of form.controls.addresses.controls; let ind=index" [formGroupName]="ind">
...
<select>
<option (onSelectionChange)="bindDATA(ind)" *ngFor="let option of testLocation; let in=index" [value]="option">
{{ option.text }}
<option>
</select>
...
</div>

Meteor Collection 2 Re-Use Array Items From Same Collection for AutoForm

I have a collection with a few arrays. I am using autoform to display various forms.
I need to pull in the existing data from the same _id into a different form field.
Here is my document with data:
{
"_id": "ERjvecg2GmSBktmn5",
"clientName": "General Motors",
"clientInfo": [
{
"fullname": "John Smith",
"email": "jsmith#gmail.com",
"phone": 2485551212,
"phone2": 7345551212
},
{
"fullname": "Billy Bob",
"email": "bbob#gmail.com",
"phone": 3135551212,
"phone2": 7346661212
}
],
"facilityList": [
{
"name": "GM # Ann Arbor",
"id": "1234",
"facilityType": "option1",
"address": "12345 Main St Ann Arbor, MI 48888",
"contacts": [
{
"contactName": "Jill Sutton",
"contactEmail": "jsutton#gmail.com",
"contactPhone1": 7348881212,
"contactPhone2": 3132224444
}
],
"special": "gsgfsdgdsgdsgdgsdgsdgdgdgggdsgfdgdfgdgsdgsdg",
"_id": "accde038-775d-4d11-b8fd-d0cc3b63c869"
}
],
"TasksWithData": [
{
"inspectionDetails": [
{
"inspector": "cHsSP6jadYa8wAeTi",
"inspectorName": "Joe Reporter",
"projectType": "inspection",
"startDate": "2017-01-13T05:30:00.000Z",
"desc": "fgfsdgsdgsdgdsgsdgdggsdgdg",
"activeTask": false
}
],
"TaskItem": [
{
"DataFacilitySection": "dsfgsdfgds",
"DataItemDescription": "item 1",
"DataItemSection": "dfgdfgdf",
"DataItemCode": "dfgdfgdf",
"DataItemPass": null
},
{
"DataFacilitySection": "ftydftuydrtuy",
"DataItemDescription": "item 2",
"DataItemSection": "dfgdgdf",
"DataItemCode": "dfgdfgdf",
"DataItemPass": null
}
],
"author": "Ss6TjGGzqWJRZYStx"
}
]
}
Really what I need to do is get the "facilityList" objects to appear as options in a select.
I have managed to do this with data from other collections like so:
'TasksWithData.$.inspectionDetails.$.inspector': {
type: String,
label: "Inspector",
optional: true,
autoform: {
firstOption: 'Choose an Inspector',
options: function() {
return Meteor.users.find({}, {
sort: {
profile: 1,
firstName: 1
}
}).map( function ( c ){
return{
label: c.profile.firstName + " " + c.profile.lastName,
value: c._id
};
} );
}
}
},
but it seems a bit different when doing it with items from the same _id.
Here is my autoForm:
{{#autoForm collection="ClientData" id=UpdateForm type="update-pushArray" scope="TasksWithData" doc=this}}
{{> afQuickField name="inspectionDetails.0.TaskFacility"}}
{{> afQuickField name="inspectionDetails.0.inspector"}}
{{> afQuickField name="inspectionDetails.0.inspectorName"}}
{{> afQuickField name="inspectionDetails.0.projectType"}}
{{> afQuickField name="inspectionDetails.0.startDate"}}
{{> afQuickField name="inspectionDetails.0.desc"}}
<div class="panel panel-default">
{{> afArrayField name="TaskItem"}}
</div>
<input class="btn btn-primary btn-block" type="submit" value="Add Task">
{{/autoForm}}
I am a little unsure if this would get done on the autoform side or with the schema. Here is an example of the schema I am using to attempt to pull in the data:
'TasksWithData.$.inspectionDetails.$.TaskFacility': {
type: String,
label: "Choose a Facility",
optional: true,
autoform: {
firstOption: 'Choose a Facility',
options: facilityList
},
Clearly its wrong, but I would REALLY appreciate suggestions on this.

Binding to nested array values in KendoUI TreeView

We are evaluating KendoUI web for use in all of our projects and I am running into an issue (or lack of understanding) with the TreeView Widget.
I am trying to use a MVVM TreeView and I am having issues when binding to nested array values from my template. I am dynamically building the tree view, by adding a node and its children with a button click. I want to bind each leaf(child) node to a text value on that node in the MVVM. This seems to work great on the initial load of the tree, but after adding more nodes to the viewModel, the bindings seem to quit working.
This seems to be a lot harder than I feel it should, which makes me think I am missing something.
Can anyone help shed some light on this?
Thanks,
Jason
JSFiddle
I created a JSFiddle example for our use in this discussion: http://jsfiddle.net/jsonsee/9B9pT/
HTML
<script id="treeTemplate" type="text/x-kendo-template">
<span>
#: item.name #
# if (!item.hasChilren) { #
<input type = 'text' data-bind = "value: treeData[#: item.parentIndex #].items[#: item.childIndex #].displayValue"/>
# } #
</span>
</script>
<div id="content">
<div id="form">
<label>Item Name:
<input type="text" data-bind="value: form.itemName" />
</label>
<button data-bind="click: form.onAddItem">Add Item</button>
<div class="console">
<h2>Output:</h2>
<div id="myConsole"/>
</div>
</div>
<h2>TreeView</h2>
<div data-template="treeTemplate" data-role="treeview" data-bind="source: treeData" />
</div>
Javascript
(function ($) {
var viewModel = kendo.observable({
itemIndex: 0,
form: {
itemName: '',
onAddItem: function (e) {
e.preventDefault();
var itemToAdd = {
name: viewModel.get('form.itemName'),
hasChilren: true,
items: (function () {
var children = [];
for (var i = 0; i < 3; i++) {
children.push({
name: viewModel.get('form.itemName') + '-child-' + i,
parentIndex: viewModel.get('itemIndex'),
childIndex: i,
displayValue: '',
hasChilren: false
});
}
return children;
})()
}
if (viewModel.get('treeData')) {
viewModel.get('treeData').push(itemToAdd);
} else {
viewModel.set('treeData', kendo.observableHierarchy([itemToAdd]));
}
viewModel.set('form.itemName', '');
viewModel.set('itemIndex', viewModel.get('itemIndex') + 1);
}
}
});
$(document).ready(function () {
kendo.bind(document.body, viewModel);
viewModel.bind('change', function (e) {
console.log('viewModel changed!');
var output = JSON.stringify(viewModel, undefined, 2);
console.log('----------------------------------------------------');
console.log(output);
console.log('----------------------------------------------------');
$("#myConsole").html("<pre>" + output + "</pre>");
});
});
})(jQuery);
Edit
Addressing a comment:
In my application, I am dragging elements from a tool palette on the left hand side of the page to a center pane. On dropping the item, I build a tree view. Each node has a set of children, those children do not have children. So the tree is one level deep (two if you count the parent).
Each node in the tree view is rendered as such (Starting with the parent)
* parentNode.name
--->* childNode.name [textbox bound to this node's displayValue property in viewModel]
--->* childNode.name [textbox bound to this node's displayValue property in viewModel]
--->* childNode.name [textbox bound to this node's displayValue property in viewModel]
--->* childNode.name [textbox bound to this node's displayValue property in viewModel]
Problem
The part I have a problem with is binding the child nodes text box to the displayValue property of the corresponding viewModel field in the hierarchical dataSource. I have tried many different methods, but the one that "sort-of" works is accessing the array elements directly from the template binding via: data-bind="value: treeData[#: item.parentIndex #].items[#: item.childIndex #].displayValue"
Note that the above binding doesn't work after I add the second node. That is the problem. I agree that the code is complex, that is part of the reason for the question... something seems wrong to me. So, either the kendo MVVM doesn't support direct binding to nested array elements, or I am doing something completely wrong.
Updated Output
I updated the JSFiddle and am posting the output to better illustrate the problem. Notice how the second set of child elements on the viewModel has a blank displayValue property, even after I changed them in the view. The bindings aren't working for some reason after I add the first node.
New JSFiddle: http://jsfiddle.net/jsonsee/9B9pT/
Printout of viewModel
{
"itemIndex": 2,
"form": {
"itemName": ""
},
"treeData": [
{
"name": "a",
"hasChilren": true,
"items": [
{
"name": "a-child-0",
"parentIndex": 0,
"childIndex": 0,
"displayValue": "1",
"hasChilren": false,
"items": [],
"index": 0
},
{
"name": "a-child-1",
"parentIndex": 0,
"childIndex": 1,
"displayValue": "2",
"hasChilren": false,
"items": [],
"index": 1
},
{
"name": "a-child-2",
"parentIndex": 0,
"childIndex": 2,
"displayValue": "3",
"hasChilren": false,
"items": [],
"index": 2
}
],
"index": 0,
"expanded": true
},
{
"name": "b",
"hasChilren": true,
"items": [
{
"name": "b-child-0",
"parentIndex": 1,
"childIndex": 0,
"displayValue": "",
"hasChilren": false,
"index": 0
},
{
"name": "b-child-1",
"parentIndex": 1,
"childIndex": 1,
"displayValue": "",
"hasChilren": false,
"index": 1,
"selected": true
},
{
"name": "b-child-2",
"parentIndex": 1,
"childIndex": 2,
"displayValue": "",
"hasChilren": false,
"index": 2
}
],
"index": 1,
"expanded": true
}
]
}