Angular 2 JS dropdown, select item from another component - select

I have two components one is employee and another is department. I want to make a dropdown so that employee can select a department.
I implemented a method for getting all departments inside my employee component:
getAllDepartment(){
this._employeeService.getAllDepartment()
.subscribe(data => this.departments = data.json());
}
in my employee html select looks like this:
<label for="editTitle">Department:</label><br>
<select [(ngModel)]="newItem.Department" (click)="getAll()" style="width: 180px">
<option *ngFor="#department of departments" [value]="department.departmentNo">{{department.departmentName}}</option>
</select>
but noting happens.
What is the best way for make this dropdown work and select items?
This is ahtml where I have select:
<form #f="ngForm" [hidden]="!showAddView" align="center">
<div>
<label for="editTitle">Employee No:</label><br>
<input type="text" [(ngModel)]="newItem.employeeNo" ngControl="employeeNo" required >
</div>
<div>
<label for="editAbrv">Employee name:</label><br>
<input type="text" [(ngModel)]="newItem.employeeName" ngControl="demployeeName" required >
</div>
<div>
<label for="editTitle">Department:</label><br>
<select [(ngModel)]="newItem.departmentNo" (click)="getAll()" style="width: 180px">
<option *ngFor="#department of departments" [value]="department.departmentNo">{{department.departmentName}}</option>
</select>
</div>
<div>
<label for="editAbrv">Salary:</label><br>
<input type="text" [(ngModel)]="newItem.salary" ngControl="salary" required>
</div>
<div>
<a href="javascript:void(0);" (click)="addEmployee(newItem)" title="Add">
Save
</a>
<a href="javascript:void(0);" (click)="showHide($event)" >
Cancel
</a>
</div>
</form>

You are binding the click event of the select box to a getAll() method, but the method that you have defined is called getAllDepartment().
You need to bind the event to an existing method name.
That being said, I'm unsure why you would want to reload the departments every time the dropdown is clicked. If the desired behaviour is to load the departments once when the component is initialized, you should add a constructor() method to the component, which will be called automatically.
constructor() {
this._employeeService.getAllDepartment()
.subscribe(data => this.departments = data.json());
}

Related

Get Value from Custom Dialog Form

I would like to get the value from my custom dialog (HTML) to use it as a string parameter to a function.
Dialog.html:
<form id="subject-form">
<div class="form-inline">
<input type="text" id="subject" placeholder="Bookmark name" name="subject" maxlength="16" required >
<span>.</span>
<div class="select-dropdown">
<select id="selector">
<option value="student">student</option>
<option value="cs">cs</option>
<option value="hr">hr</option>
</select>
</div>
<span>.xyz.com</span>
</div>
<div class="btn-container" id="dialog-button-bar">
<button type="submit" class="action" onclick="getLabel()">Add</button>
<button id="dialog-cancel-button" onclick="google.script.host.close()">Cancel</button>
</div>
<!-- <div id="dialog-status"></div> -->
</form>
DialogJS:
function getLabel () {
const subj = document.getElementById('subject').value;
const subd = document.getElementById('selector').value;
google.script.run
.withSuccessHandler()
.mailIt(subj, subd);
}
The problem is that you are reloading the page every time you press the button.
Change this:
<form id="subject-form">
To this:
<form onsubmit="return false" id="subject-form">
Then the function will work as expected.
Reference:
Stop form refreshing page on submit

How can I make this form dynamically with ES6 by extending a base class form?

I would like to make forms in JS that act like they're inheriting...For example, I can easily append form elements all day long using a for loop, but I'd rather leave myself the freedom to insert a different element in the middle. In other words, I want this to be 'modular', and have a base class that can generate something simple like a login screen, but then extend it to include dropdowns in between text fields. Any ideas as to how to make this happen? Preferably with ES6 classes and import/export and without the webpack nonsense.
Ideally i'd have a class called BasicForm and have RegistrationForm extends BasicForm. This way, I could simply store field names in an array and change that file once if i needed to make changes as opposed to changing everything. Here's the existing code....Note that "invoices" is only shown if the user role option selected is "admin"....which makes the idea of trying to make this all very difficult for me to comprehend. Is there any way to procedurally generate, with bootstrap and custom classes, this from Javascript using ES6 classes, such that the module may be reused to create forms either with or without dropdowns?
HTML:
<div class= "row"> <!--Inherits background from .body-->
<div class="col-hidden col-sm col-md col-lg col-xl"> <!--spacing divs inherit background from .body-->
</div>
<div class="form-box rounded col-12 col-xs col-sm-7 col-md-6 col-lg-4 col-xl-3"> <!--Actual box containing fields and prompts and buttons changes to new background-->
<h2 class="portal-heading">Registration</h2>
<form name="new_user_form">
Email Address<input type="text" class="form-control register-field highlight-hover" name="email" value="" placeholder="Email Address"><br>
Re-Enter Email Address<input type="text" class="form-control register-field highlight-hover" autocomplete="off" name="email" value="" placeholder="Re-enter Email Address"><br>
First Name<input type="text" class="form-control register-field highlight-hover" autocomplete="given-name" name="firstname" value="" placeholder="First Name"><br>
Last Name<input type="text" class="form-control register-field highlight-hover" autocomplete="family-name" name="lastname" value="" placeholder="Last Name"><br>
Company Name<img src="images/help-icon.png">
<select class="form-control register-field highlight-hover" name="company">
<option value="noSelect">Select</option>
<option value="company2">Company 2</option>
</select>
Mobile Phone <img src="images/help-icon.png">
<input type="text" class="form-control register-field highlight-hover" autocomplete="tel" name="mobile" value="" placeholder="0005559999"><br>
Portal User Role <img src="images/help-icon.png">
<select class="form-control register-field highlight-hover" name="role" id="user-role">
<option value="user">User</option>
<option value="admin">Admin</option>
</select>
<div id="invoices">
Enter two recent invoice totals in USD($)<br>
Invoice 1<input type="text" class="form-control register-field highlight-hover" name="invoice1" value="" placeholder="0.00">
Invoice 2<input type="text" class="form-control register-field highlight-hover" name="invoice2" value="" placeholder="0.00">
</div>
<button class="btn btn-block highlight-hover" id="submit">Submit</button>
</form>
</div>
<div class="col-hidden col-sm col-md col-lg col-xl"> <!--spacing divs-->
</div>
</div>
I would suggest using Web Components. They're native, have support in Chrome with other browsers soon to come, and you can use a polyfill for other browsers. With below code:
class WordCount extends HTMLParagraphElement {
constructor() {
// Always call super first in constructor
super();
//put code here
}
}
Insert code found at https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_custom_elements after super() with your element functionality
Define a new element: `customElements.define('popup-info', PopUpInfo);
And then you can use it like this:
<popup-info img="img/alt.png" text="Your card validation code (CVC)
is an extra security feature — it is the last 3 or 4 numbers on the
back of your card.">
Example taken from link above, in Mozilla's Dev Site.
Not sure if this can help (?): classList, it is meant to test if a class exist, at a precise position.
It isn't well documented, but found it very useful to bind stuffs.
Using a timeout for the demo (Added id="company" class="hidden"):
function toggle(id){
if (id.classList[0] === "hidden"){
id.classList = "unhidden"
}
else {
id.classList = "hidden"
}
}
setTimeout(function(){
toggle(company)
},3000)
.hidden {display: none}
.unhidden {display: block}
<div class= "row">
<div>
</div>
<div>
<h2>Registration</h2>
<form name="new_user_form">
Email Address<input type="text" name="email" value="" placeholder="Email Address"><br>
Re-Enter Email Address<input type="text" autocomplete="off" name="email" value="" placeholder="Re-enter Email Address"><br>
First Name<input type="text" autocomplete="given-name" name="firstname" value="" placeholder="First Name"><br>
Last Name<input type="text" autocomplete="family-name" name="lastname" value="" placeholder="Last Name"><br>
Company Name<a href="#"
data-toggle="tooltip" title="Choose your company name. If you do not see it here, please contact ACSI to become an official distributor."><img src="images/help-icon.png"></a>
<select id="company" class="hidden" name="company">
<option value="noSelect">Select</option>
<option value="company2">Company 2</option>
</select>
Mobile Phone <img src="images/help-icon.png">
<input type="text" autocomplete="tel" name="mobile" value="" placeholder="0005559999"><br>
Portal User Role <img src="images/help-icon.png">
<select name="role" id="user-role">
<option value="user">User</option>
<option value="admin">Admin</option>
</select>
<div id="invoices">
Enter two recent invoice totals in USD($)<br>
Invoice 1<input type="text" name="invoice1" value="" placeholder="0.00">
Invoice 2<input type="text" name="invoice2" value="" placeholder="0.00">
</div>
<button id="submit">Submit</button>
</form>
</div>
<div>
</div>
</div>
Steps:
Create a div, append it to document.
Create a form, append it to div.
Create fieldsets, append to form.
4.Create form fields, append to fieldset.
class BasicForm {
constructor(formId,formTitle) {
this.form = document.createElement('form');
this.form.id = formId;
this.form.method = 'post';
this.form.enctype = 'multipart/form-data';
this.topFieldset = document.createElement('fieldset');
this.topLegend = document.createElement('legend');
this.topLegend.appendChild(document.createTextNode(formTitle));
this.topFieldset.appendChild(this.topLegend);
this.form.appendChild(this.topFieldset);
this.div = document.createElement('div');
this.div.appendChild(this.form);
// add div to the document
// add fields to the topFieldset
}
}
class RegistrationForm extends BasicForm {
constructor() {
super();
// add fields of derived form
}
}
Note: the code is not tested.

jQuery UI Dialog Box using dialog() together with replaceWith()

I want to use jQuery UI dialog box to open a form dialog where one can edit information about an employee.
The form looks like this
<form id="formAddNewRow" action="/frontend_dev.php/test/create" method="post" >
<table>
<tbody>
<ul>
<li>
<label for="employee_firstname">Firstname</label>
<input type="text" name="employee[firstname]" id="employee_firstname" />
</li>
<li>
<label for="employee_lastname">Lastname</label>
<input type="text" name="employee[lastname]" id="employee_lastname" />
</li>
<ul>
</tbody>
</table>
</form>
I want to load the form elements prefilled with the employees data. eg
<label for="employee_lastname">Lastname</label> <input type="text" name="employee[lastname]" value="Miller" id="employee_lastname" />
So my idea was to ajax a complete form that fits the selected employee and replace it with the one above.
<form id="formAddNewRow" action="/frontend_dev.php/test/create" method="post" >
<table>
<tbody>
<ul>
<li>
<label for="employee_firstname">Firstname</label>
<input type="text" name="employee[firstname]" value="Miller" id="employee_firstname" />
</li>
<li>
<label for="employee_lastname">Lastname</label>
<input type="text" name="employee[lastname]" value="Tom" id="employee_lastname" />
</li>
<ul>
</tbody>
</table>
</form>
I try doing that by
$( ".editButton" )
.button()
.click(function() {
var replace = $.ajax({
url: 'employee/edit?id=1', success: function() {
$( "#formAddNewRow" ).replaceWith(replace.responseText);
}
});
});
This works, but it stops working when I do:
$( "#formAddNewRow" ).dialog({});
There is no error message or warning. The form just gets eliminated from the DOM together with its parent node that was inserted by dialog().
How do I prefill the form succesfully?
Put your <form> into a <div> and attach the .dialog() to the div instead of to the form.
In the AJAX call replace the form as you are now, leaving its parent div attached to the dialog box.
This is necessary because jQuery UI internally maintains references to the element contained in the dialog box, and if you replace that element those references don't get updated. Replacing a child of that element will eliminate that problem.
<div id="formAddNewRowDialog">
<form id="formAddNewRow" action="/frontend_dev.php/test/create" method="post" >
<table>
<tbody>
<ul>
<li>
<label for="employee_firstname">Firstname</label>
<input type="text" name="employee[firstname]" value="Miller" id="employee_firstname" />
</li>
<li>
<label for="employee_lastname">Lastname</label>
<input type="text" name="employee[lastname]" value="Tom" id="employee_lastname" />
</li>
<ul>
</tbody>
</table>
</form>
</div>
Wrap the form in a div like above then call
$( "#formAddNewRowDialog" ).dialog();

jquery mobile form submit. Simple form that takes user to page on form submit

I'm struggling with a form submit in jquery mobile and the api doesn't show any useful examples.
Here is my html:
<div data-role="page" id="test" data-id="test">
<div data-role="content">
<form id="form1">
<div id="titleDiv" data-role="fieldcontain">
<label id="titlelabel" for="title">Title *</label>
<select id="title" name="title">
<option value="null">Please select a title</option>
<option value="mr">Mr</option>
<option value="miss">Miss</option>
<option value="mrs">Mrs</option>
<option value="ms">Ms</option>
<option value="dr">Dr</option>
</select>
</div>
<div id="submitDiv" data-role="fieldcontain">
<input type="submit" value="Submit Form" data-inline="true"/>
</div>
</form>
</div>
</div>
<div data-role="page" id="test" data-id="test">
<p>form confirmation page</p>
</div>
and here is my javascript:
$('div').live('pageshow',function(event, ui) {
$('#form1').submit(function() {
alert('test');
// Submit the form
// $.post("/forms/requestProcessor.php", form1Var.serialize(), function(data){
// });
$.mobile.changePage($("#test"), "slideUp");
return false;
});
});
On submit, I want it to show an alert then take the user to a page. At the moment, that code shows the alert 3 times the does nothing. Any ideas what is wrong with it?
A problem is that you're re-using the test id. Give the second page a different id and changePage to that id will work. Also it would be better to omit the pageshow or restrict it to #test rather than all the divs in your page.

Some fields are binding on postback some don't

I have a view containing a number of fields
<div class="editor-label">
<label for="Supplier">Supplier</label>
</div>
<div class="editor-field">
<input id="Supplier" name="Supplier" type="text" value="Swifts Antiference Division Ltd" />
<input id="SupplierId" name="SupplierId" type="hidden" value="1" />
</div>
<div class="editor-label">
<label for="Scheme_Group">Group</label>
</div>
<div class="editor-field">
<select id="Scheme_Group_Id" name="Scheme.Group.Id"><option value="">Select group</option>
<option value="3">Gels</option>
<option value="2">Gloves</option>
<option selected="selected" value="1">Needles</option>
<option value="4">Soap</option>
</select>
The problem I am having is when the form these fields are contained within is submitted to teh controler action only some of the values are received by the action. The Supplier and SupplierId fields work as expected but the "Scheme_Group_Id" is never populated.
You should also post how your action is declared and what type are you passing to the view.
Please check the binding in the action parameters, something like this :
public void Edit([Bind(Prefix = "ClassName")]string fieldName){ ...action code...}
More information in here MSDN