How to get ngForm variable reference in the component class? - dart

Given the following...
.html
<form (ngSubmit) = "onSubmit()"
#heroForm = "ngForm">
{{diagnostic}}
<div class = "form-group">
<label for = "name">Name</label>
<input type = "text"
class = "form-control"
required
[(ngModel)] = "model.name"
ngControl = "name"
#name = "ngForm"
#spy>
<p *ngIf = "name.dirty"
class = "alert alert-danger">
Name is required
</p>
<!--<p [hidden] = "name.dirty"-->
<!--class = "alert alert-danger">-->
<!--Name is required-->
<!--</p>-->
</div>
..
..is it possible to get the #name = "ngForm" (ngForm) reference in the .dart component to allow manipulation? Any suggestion and correction is welcome.

import this -
import {ViewChild} from 'angular2/core';
Just add this field with the annotation to the class
// Dart syntax
#ViewChild('heroForm') NgForm heroForm;
You can't use it in the constructor though because it is only set later. In ngAfterViewInit or event handlers for user input you can use it without limitations.

Related

how do i parse a div on website with jsoup

i want to parse something like this:
<div class = "1">
<div class = "2">
<div class = "3">
<div class = "4">
<div class = "5"> value </div>
</div>
</div>
</div>
but when i'm using
Document d = Jsoup.connect("https://example").get();
Elements elements = d.getElementsByClass("5");
System.out.println(elements);
i'm getting nothing ;( i've tried a ton of variants but nothing seems to work. How do i do that?

how to implement a multi line output

Will someone kindly give me a hand. Suppose I have a text box and a button. If I type a number say 5 in the text box, I want the numbers 1 to 5 appear in the result text box, one number per line. How should be modifying. Thank you.
<html><head>
<title>Temp proj</title>
<link rel = "stylesheet" type = "text/css"
href = "temp.css">
</head>
<body>
<div class = "container">
<form name = "myform">
<input type = "text" name = "first" class = "mytext" size = "4"><br>
<input class = "button" name = "" value = "Enter" onclick = "enter()"><br>
<input type = "text" name = "result" class = "mytext" size = "4">
</form>
<script type = "text/javascript">
function enter(){
first = parseInt(myform.first.value);
myform.result.value = result;
}
</script>
</head></html>

Displaying data when searching through external API

So, I am trying to search through an external API (Nutrionix) and am having trouble figuring out how to display the results.
I currently have a form for searching
<form action = "/foods" method = "get">
<div class = "field">
<input type = "text" name = "searched_value" alt = "Search Foods" />
</div>
<div class = "btn">
<input type = "submit" name ="btn" value = "Search Foods">
</div>
</form>
Then I have in my FoodsController:
class FoodsController < ApplicationController
require 'faraday'
require 'json'
def search
#searched_food = params[:searched_value].split(" ").join("%20")
#response_body = Faraday.get("https://api.nutritionix.com/v1_1/search/#{#searched_value}?format=json?fields=item_name%2Citem_id%2Cbrand_name%2Cnf_calories%2Cnf_total_fat&appId=[MYAPPID]&appKey=[MYAPPKEY]").body
#parsed_response = JSON.load(#response_body)
end
end
My relevant routes are:
Rails.application.routes.draw do
get 'food_form' => 'foods#food_form'
get 'foods' => 'foods#foods'
get 'search' => 'foods#search'
end
It then just goes back to the page that links to the "add foods" page where I'm currently just trying to get stuff to show up so I have:
<div class = "description">
<%= link_to "Add foods", food_form_path %>
<%= #parsed_response%>
<%= #response_body%>
<%= #searched_food %>
</div>
I currently get nothing for my results as just the link to the form appears. However, when I just put the controller info on an html page I can see results and if I go to the nutrionix link myself, I get results.
Help would be much appreciated. :)
The search form isn't hitting the action it supposed to because it has action = "/foods" which should be action = "/foods/search". Changing it should solve your problem.
<form action = "/foods/search" method = "get">
<div class = "field">
<input type = "text" name = "searched_value" alt = "Search Foods" />
</div>
<div class = "btn">
<input type = "submit" name ="btn" value = "Search Foods">
</div>
</form>

Dart CheckboxInputElement doesn't show text

Dart CheckboxInputElement adds specified text between opening and ending input tags and the browser ignores this text. For example, the following dart code:
FormElement form = new FormElement();
CheckboxInputElement option = new CheckboxInputElement();
option.name = "text1";
option.value = "text1";
option.text = "text1";
form.children.add(option);
window.children.add(form);
creates the following html code:
<form>
<input type="checkbox" name="text1" value"text1">text1</input>
</form>
I end up with checkboxes without descriptors.
You have to add a Label with the descriptor text and link it to the Checkbox:
FormElement form = new FormElement();
CheckboxInputElement option = new CheckboxInputElement();
option.name = "text1";
option.value = "text1";
option.id = "text1";
form.children.add(option);
LabelElement label = new LabelElement();
label.htmlFor = 'text1';
label.text = "This is a checkbox label";
form.children.add(label);
window.children.add(form);
The for property will look for the input with the id specified and connect them (so that clicking on the label text will toggle the checkbox) .
You will end up with the following HTML:
<form>
<input type="checkbox" name="text1" value="text1" id="text1">
<label for="text1">This is a checkbox label</label>
</form>

Grails databinding multiple domain classes

HI. I have this classes:
class Carro {
String name
String marca
String matricula
static constraints = {
name(nullable:false, blank:false)
}
static mapping = {
version false
}
}
class CarroMovel {
String move
String rodas
String espelhos
Carro carro
static hasMany = [carros: Carro]
static constraints = {
move(nullable:false, blank:false)
}
static mapping = {
version false
}
}
And the controllers:
class CarroController{
def save2 = {
def carroInstance = new Carro()
carroInstance.name = params.name
carroInstance.marca = params.marca
carroInstance.matricula = params.matricula
if (carroInstance.save(flush: true)) {
redirect(uri:"/home.gsp")
}
else {
render(view: "/testeAdd", model: [carroInstance: carroInstance])
}
}
And the view testeAdd.gsp
<g:form controller="carro" action="save2">
<h1>Add New Carro Record</h1>
<p>Basic Information</p>
<label>Name
<span class="small">as</span>
</label>
<input type="text" name="name" value="${carroInstance?.name}" /><br>
<label>Marca
<span class="small">as</span>
</label>
<input type="text" name="marca" value="${carroInstance?.marca}" /><br
<label>Matricula
<span class="small">as</span>
</label>
<input type="text" name="matricula" value="${carroInstance?.matricula}" /><br>
<g:submitButton name="save" value="Save" id="oneone"/>
<div class="spacer"></div>
</g:form>
<g:hasErrors bean="${carroInstance}">
<div class="errors">
<g:renderErrors bean="${carroInstance}" as="list" />
</div>
</g:hasErrors>
This is working good. Now i would like to be able to data binding multiple domain classes. So, along with the current code from my gsp file, i would also like to add carroMovel occurrences all in same save2. Im not sure how to do that, specially cause class Carro will need to have an id from class carroMovel. Any help please? Thank you.
I folowed some suggestions and now the results are as follows (im not concerned about error validation yet):
def save3 = {
def carroInstance = new Carro()
def carroMovelInstance = new CarroMovel()
carroInstance.name = params.carro.name
carroInstance.marca = params.carro.marca
carroInstance.matricula = params.carro.matricula
carroMovelInstance.move = params.carroMovel.move
carroMovelInstance.rodas = params.carroMovel.rodas
carroMovelInstance.espelhos = params.carroMovel.espelhos
carroInstance.save()
carroMovelInstance.carro = carroInstance
carroMovelInstance.save()
}
<g:form controller="carro" action="save3">
<h1>Add New Conference Record</h1>
<p>Basic Information</p>
<label>Name
<span class="small">Add your name</span>
</label>
<input type="text" name="carro.name" value="${carroInstance?.name}" /><br>
<label>Marca
<span class="small">Add your name</span>
</label>
<input type="text" name="carro.marca" value="${carroInstance?.marca}" /><br
<label>Matricula
<span class="small">Add your name</span>
</label>
<input type="text" name="carro.matricula" value="${carroInstance?.matricula}" /><br>
<label>Move
<span class="small">Add your name</span>
</label>
<input type="text" name="carroMovel.move" value="${carroMovelInstance?.move}" /><br>
<label>Rodas
<span class="small">Add your name</span>
</label>
<input type="text" name="carroMovel.rodas" value="${carroMovelInstance?.rodas}" /><br>
<label>Espelho
<span class="small">Add your name</span>
</label>
<input type="text" name="carroMovel.espelho" value="${carroMovelInstance?.espelho}" /><br>
<g:submitButton name="save" value="Save" id="addConference"/>
The Carro object is saved in the database, altought, nothing happens with CarroMovel and it is not saved and i can't figure it out.
First I would change the input names to carro.name, carro.marca, carroMovel.move, ... so that they are differentiated by name.
<input type="text" name="carro.name"/><br>
<input type="text" name="carro.marca"/><br>
<input type="text" name="carroMovel.move"/><br>
This has the advantage that the binding in the controller can be done the standard Grails way, and that the correct values are entered in the form without the value attribute set.
carro.properties = params.carro
carroMovel.properties = params.carroMovel
In the controller action you can also save and link the Carro and CarroMovel instances.
carro.save()
carroMovel.carro = carro
carroMovel.save()
if(carroMovel.hasErrors(){
render(view: 'save3', model: [carro: carro, carroMovel.carroMovel])
}
If I understand your question correctly, you can give this a try.
First, edit our form to include the necessary fields for the CarroMovel class,
e.g
<label>Move
<span class="small">as</span>
</label>
<input type="text" name="move" value="${carroMovelInstance?.move}" />
then
in your save2 action,
def carroInstance = new Carro()
carroInstance.name = params.name
carroInstance.marca = params.marca
carroInstance.matricula = params.matricula
def carroMovelInstance = new CarroMovel()
carroMovelInstance.name = params.move
carroMovelInstance.marca = params.rodasa
carroMovelInstance.matricula = params.espelhos
carroMovelInstance.carro = carroInstance
Since Carro does not belong to CarroMovel saving a carroMovelInstance will not cascade to the carroInstance, therefore you will need to save each instance individually before saving its owning instance.
carroMovelInstance.carro.save()
if (carroMovelInstance.save(flush: true)) {
redirect(uri:"/home.gsp")
}
else {
render(view: "/testeAdd", model: [carroInstance: carroInstance, carroMovelInstance:carroMovelInstance])
}
Let me know if that works out for you.
Some of the other answers may be simpler, but I've been using this technique:
http://omarello.com/2010/08/grails-one-to-many-dynamic-forms/
In order for Carro to cascade save the CarroMovel reference, CarroMovel needs belongsTo = Carro or you need to manually tell hibernate to cascade it upon save using something like this:
class CarroMovel{
static mapping = {
carro cascade: 'all'
}
}
Here's the hibernate documentation about cascading:
https://docs.jboss.org/hibernate/orm/4.0/javadocs/org/hibernate/metamodel/binding/CascadeType.html
I usually use a command object for this type of thing.
In your controller do something like this:
class CarroController {
def show = {
[cmd: new MyCommand()]
}
def save2 = { MyCommand cmd ->
def carro = cmd.carro
if (carro.save()) {
cmd.movel.carro = carro
if (cmd.movel.save()) {
redirect uri: 'home.gsp'
} else {
// show form again
render view: 'show', model:[cmd:cmd]
}
} else {
// show form again
render view: 'show', model:[cmd:cmd]
}
}
// same file or some other class file
class MyCommand {
Carro carro
CarroMovel movel
}
}
You will need to adjust your form a bit as well...
Where you have "Carro" fields reference them like this:
<input type="text" name="carro.matricula" value="${cmd.carro?.matricula}" />
Where you have the "CarroMovel" fields, like this:
<input type="text" name="movel.rodas" value="${cmd.movel?.rodas}" />
This code might not be exactly right (I didn't test it), but should get you down the correct path. Also instead of referencing the objects in the command object, you could just have the fields that you are binding, and then build the actual domain objects from them, either through a helper method (def buildCarro(){...}) or by hand in the controller method.

Resources