How to get json and display it by user id - json

I want to display the data of the selected user in angular 2. I make a service to get the data from API
getUserById(id: string): Promise<User> {
const url ='api/user/${id}';
return this.httpClient.get(url)
.toPromise()
.then(response => response.json() as User)
.catch(this.handleError); }
and this is my controller
user: User;
constructor(private userService: UserService) {
userService.getUserById('45835708-f55a-40fb-878f-33b9c920e196')
.then(hasil => this.user = hasil)
.catch(this.handleError);
}
and here is my template
<div class="form-group">
<label class="col-sm-2 control-label">FULL NAME</label>
<div class="col-sm-6">
<input class="form-control" type="text" value="" disabled>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">USER NAME</label>
<div class="col-sm-6">
<input class="form-control" type="text" value="" disabled>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">PASSWORD</label>
<div class="col-sm-6">
<input class="form-control" type="password" value="" disabled>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">DATE OF BIRTH</label>
<div class="col-sm-6">
<input class="form-control" type="text" value="" disabled>
</div>
</div>
How to display the json to my template? Do I make correct service and controller?

You can choose to use template driven forms or reactive driven forms.
this.user object can be a model to that form, and when you input something into html input elements, object will update automatically:
<div class="form-group">
<label class="col-sm-2 control-label">FULL NAME</label>
<div class="col-sm-6">
<input class="form-control" type="text" value="" [(ngModel)]="user.fullName" disabled>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">USER NAME</label>
<div class="col-sm-6">
<input class="form-control" type="text" value="" [(ngModel)]="user.username" disabled>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">PASSWORD</label>
<div class="col-sm-6">
<input class="form-control" type="password" value="" [(ngModel)]="user.password" disabled>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">DATE OF BIRTH</label>
<div class="col-sm-6">
<input class="form-control" type="date" value="" [(ngModel)]="user.dateOfBirth" disabled>
</div>
</div>
If you want to add some validator, you should go with Reactive forms:
https://angular.io/docs/ts/latest/guide/reactive-forms.html
The principle here is to create a form group and define individual properties with their initial values and validator.

First make sure you have FormsModule imported in your module. Then... remember to use ngModel to bind the user to the form.
But, forms do not care about ngModel unless there is also a name attribute on each field:
<input class="form-control" name="fullName" [ngModel]="user.fullName">
OR
ngModelOptions in each field:
<input class="form-control" [ngModelOptions]="{standalone: true}" [ngModel]="user.fullName">
Here's a demo for you with usage of the name attribute:
Plunker

Related

If I add phone number to my HTML form, the add new hire button gets disable. Is there a limit to put html elements?

Following is the html code. All I wanted to know if there are limits to putting html elements for form designing.
<div class="container">
<h1>Add New Employee</h1>
<br>
<form action="process.php" method="post">
<div class="form-group">
<label for="name">Full Name:</label>
<input class="form-control" type="text" name="name">
</div>
<div class="form-group">
<label for="permanent_address">Permanent Address:</label>
<input class="form-control" type="text" name="permanent_address">
</div>
<div class="form-group">
<label for="marital_status">Marital Status:</label>
<select class="form-control" name="marital_status">
<option value="Married">Married</option>
<option value="Unmarried">Unmarried</option>
</select>
</div>
<div class="form-group">
<label for="gender">Gender: </label>
<input type="radio" value="Male" name="train">Male
<input type="radio" value="Female" name="train">Female
</div>
This is where the problem starts.
This is the reason i've disable phone number by commenting it becasue it is disabling the submit button.
<!-- <div class="form-group">
<label for="phone">Phone number: </label>
<input class="form-control" type="number" name="phone">
</div> -->
<div class="form-group">
<label for="admission_date">Admission Date: </label>
<input type="date" name="admission_date">
</div>
<div class="form-group">
<label for="Aadhar">Aadhar Number: </label>
<input class = "form-control" name="Aadhar">
</div>
<button class="btn btn-success" name="submitted">Add New Hire</button>
</form>
</div>
your code seems to run fine. There are no limits to how many elements you can have in a form - possibly limited my memory. Why do you think your form isn't working?
also for phone input try the following:
<input type="tel" id="phone" name="phone"
pattern="[0-9]{3}-[0-9]{3}-[0-9]{4}"
required>
<div class="container">
<h1>Add New Employee</h1>
<br>
<form action="https://www.google.com" method="post">
<div class="form-group">
<label for="name">Full Name:</label>
<input class="form-control" type="text" name="name">
</div>
<div class="form-group">
<label for="permanent_address">Permanent Address:</label>
<input class="form-control" type="text" name="permanent_address">
</div>
<div class="form-group">
<label for="marital_status">Marital Status:</label>
<select class="form-control" name="marital_status">
<option value="Married">Married</option>
<option value="Unmarried">Unmarried</option>
</select>
</div>
<div class="form-group">
<label for="gender">Gender: </label>
<input type="radio" value="Male" name="train">Male
<input type="radio" value="Female" name="train">Female
</div>
This is where the problem starts.
This is the reason i've disable phone number by commenting it becasue it is disabling the submit button.
<div class="form-group">
<label for="phone">Phone number: </label>
<input class="form-control" type="number" name="phone">
</div>
<div class="form-group">
<label for="admission_date">Admission Date: </label>
<input type="date" name="admission_date">
</div>
<div class="form-group">
<label for="Aadhar">Aadhar Number: </label>
<input class = "form-control" name="Aadhar">
</div>
<button class="btn btn-success" name="submitted">Add New Hire</button>
</form>
</div>

Binding json - formgroup

I have a form and I want to display data that comes from an API (JSon file) in mode readonly. Do you have tracks?
I take an example of a form:
Thank you
<div class="col-sm-8 col-sm-offset-2">
<form [formGroup]="userForm" (ngSubmit)="onSubmitForm()">
<div class="form-group">
<label for="firstName">Prénom</label>
<input type="text" id="firstName" class="form-control" formControlName="firstName">
</div>
<div class="form-group">
<label for="lastName">Nom</label>
<input type="text" id="lastName" class="form-control" formControlName="lastName">
</div>
<div class="form-group">
<label for="email">Adresse e-mail</label>
<input type="text" id="email" class="form-control" formControlName="email">
</div>
<div class="form-group">
<label for="email">Adresse address</label>
<input type="text" id="address" class="form-control" formControlName="address">
</div>
<button type="submit" class="btn btn-primary">Soumettre</button>
</form>
</div>
File JSON
User:
- User1:
firstName: toto
lastName: titi
email: toto#toto.com
address: 2 rue titi
In your component (.ts file), as soon as the data comes back (typically inside of your ngOnInit), you should be able to do this.userForm.setValue(data).
As for making them readonly, an easy way I know is to set the FormGroup as disabled when you create it. So userForm = new FormGroup({_property_names_}, { disabled: true }).

How can i disabled selection based on ng-model?

I want to disabled Proxy input field for selection till Attestor is selected , Currently both fields are readOnly, if attestor is not selected user should not be able to select Proxy. I am new to angularJS please help how we can implement this logic using ng-model.
main.html
<div class="row">
<div class="form-group col-md-6">
<label for="attestorWorker" class="col-md-4">Attestor:</label>
<div class="col-md-8">
<input type="text" class="form-control" id="attestorWorker" required
ng-model="attestorDTO.attestorWorker" name="attestorWorker"
ng-click="openAttestorSearch()" readonly="readonly"/>
</div>
</div>
</div>
<div class="row">
<div class="form-group col-md-6">
<label for="proxyWorker" class="col-md-4">Proxy :</label>
<div class="col-md-8">
<input type="text" class="form-control" id="proxyWorker" required
ng-model="attestorDTO.proxyWorker" name="proxyWorker"
ng-click="openProxySearch()" readonly="readonly"/>
</div>
</div>
</div>
You can use the ng-disabled attribute.
<input type="text" class="form-control" id="proxyWorker" required
ng-model="attestorDTO.proxyWorker" name="proxyWorker"
ng-click="openProxySearch()" readonly="readonly"
ng-disabled="!attestorDTO.attestorWorker"/>
Changing:
ng-disabled="!attestorDTO.attestorWorker"
to the condition that you want it to be disabled

Two fields on the same line in a vertical form with bootstrap

I would like the fields "Name" and "Last Name" to be on the same row.
I've found some solutions such as this one here in stackoverflow, but the problem is that I want the labels positioned above the fields to be side by side too.
How is that possible?
Here's the form code:
<form>
<div class="form-group">
<label for="inputName">Name:</label>
<input type="text" class="form-control" id="inputName" placeholder="Enter your name here" value="Leandro">
</div>
<div class="form-group">
<label for="inputLastName">Last Name:</label>
<input type="text" class="form-control" id="inputLastName" placeholder="Enter your last name here" value="Faria">
</div>
<div class="form-group">
<label for="inputTitle">Title:</label>
<input type="text" class="form-control" id="inputTitle" placeholder="Enter your title here" value="CEO">
</div>
<div class="form-group">
<label for="inputEmail">Email:</label>
<input type="email" class="form-control" id="inputEmail" placeholder="Enter your email here" value="leo#saasmetrics.co">
</div>
<div class="form-group bottom-buffer">
<label for="selectEmail">Notifications:</label>
<select class="form-control" id="selectEmailNotifications">
<option>None</option>
<option>Daily</option>
<option>Weekly</option>
<option selected>Monthly</option>
</select>
</div>
<div class="form-group bottom-buffer">
<label for="selectEmail">Status:</label>
<select class="form-control" id="selectEmailNotifications">
<option selected>Active</option>
<option>Inactive</option>
</select>
</div>
<div class="form-group bottom-buffer">
<label for="selectEmail">Profile:</label>
<select class="form-control" id="selectEmailNotifications">
<option selected>Administrator</option>
<option>User</option>
</select>
</div>
</form>
Something like this should get you started.
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<label for="inputName">Name:</label>
<input type="text" class="form-control" id="inputName" placeholder="Enter your name here" value="Leandro">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label for="inputLastName">Last Name:</label>
<input type="text" class="form-control" id="inputLastName" placeholder="Enter your last name here" value="Faria">
</div>
</div>
</div>
Yes, the top answer is true, but this is another solution.You can use inline bootstrap forms like this:
<div class="form-inline">
<div class="form-group" style="width: 50%">
<label for="input1">Name</label>
<input type="text" class="form-control" id="input1" placeholder="David">
</div>
<div class="form-group" style="width: 49%">
<label for="input2">Last Name</label>
<input type="text" class="form-control" id="input2" placeholder="Tanner">
</div>
</div>
Read the bootstrap document

Create bootstrap form having horizontal and vertical form field

Is there anyway I can create a form using bootstrap which can have fields aligned in same line as well as horizontal fields?
Something like this?
in this the cvv MM and YY are in the same line..How can i make it possible using bootstrap form?
<form role="form">
<div class="form-group">
<label for="exampleInputEmail1">Email address</label><input type="email" class="form-control" id="exampleInputEmail1" />
</div>
<div class="form-group">
<label for="exampleInputEmail1">CVV</label><input type="email" class="form-control" id="cvv" />
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label><input type="password" class="form-control" id="exampleInputPassword1" />
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
</div>
I want the cvv to be aligned with the first form field
Wrap your input fields in a div and assign a col-sm-x class to it e.g:
<div class="form-group">
<label class="col-sm-3 control-label" for="cardNumber">Card # (required)</label>
<div class="col-sm-5">
<input type="text" class="form-control" id="cardNumber" size="12" autocomplete='off'>
</div>
</div>
Also add a col-sm-x to your labels
For columns:
<div class="form-group">
<label class="col-sm-3 control-label">Expiry date(MM/YYYY) (required)</label>
<div class="col-sm-2">
<input type="text" class="form-control" placeholder="MM" size="2" autocomplete='off'>
</div>
<div class="col-sm-2">
<input type="text" class="form-control" placeholder="YYYY" size="4" autocomplete='off'>
</div>
</div>
Finally, class of form should be form-horizontal Play with the col width as you like
Wrap your input fields as above in one div but add to this div the 'form-inline' class. No need of the col-sm-x class.
<div class="form-group form-inline">
<label class="control-label">Expiry date(MM/YYYY) (required)</label>
<input type="text" class="form-control" placeholder="MM" size="2" autocomplete='off'>
<input type="text" class="form-control" placeholder="YYYY" size="4" autocomplete='off'>
</div>
</div>