Skip to content

Commit 86b8999

Browse files
author
Stijn Goethals
committed
Add forms example #19
1 parent 3ec8b27 commit 86b8999

File tree

7 files changed

+73
-2
lines changed

7 files changed

+73
-2
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,8 @@ Every single demo is separate component. Bellow you can find links to components
122122
#### [Demo](https://github.com/tealpartners/ng-select2/tree/master/src/app/demos/allow-clear) with allow clear option
123123
- Demo with allow clear option
124124

125+
126+
#### [Demo](https://github.com/tealpartners/ng-select2/tree/master/src/app/demos/form) with a form
127+
- Demo with a form
128+
125129
Demo forked from: https://github.com/NejcZdovc/ng2-select2-demo

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"test": "ng test",
2929
"lint": "ng lint",
3030
"e2e": "ng e2e",
31+
"github": "ng build --prod --base-href https://tealpartners.github.io/ng-select2/ && npx angular-cli-ghpages --dir dist-site/ng-select2",
3132
"packagr": "ng-packagr -p projects/ng-select2/ng-package.json",
3233
"packagr-prod": "ng-packagr -p projects/ng-select2/ng-package.prod.json"
3334
},

src/app/app.component.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,6 @@ <h1>ng-select2 demo's</h1>
1818
<app-value-changed></app-value-changed>
1919
<hr>
2020
<app-allow-clear></app-allow-clear>
21-
</div>
21+
<hr>
22+
<app-form></app-form>
23+
</div>

src/app/app.module.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { BasicComponent } from './demos/basic/basic.component';
99
import { ChangeComponent } from './demos/change/change.component';
1010
import { CustomArrayComponent } from './demos/custom-array/custom-array.component';
1111
import { DynamicComponent } from './demos/dynamic/dynamic.component';
12+
import { FormComponent } from './demos/form/form.component';
1213
import { MultipleComponent } from './demos/multiple/multiple.component';
1314
import { OptionsComponent } from './demos/options/options.component';
1415
import { TemplateComponent } from './demos/template/template.component';
@@ -27,7 +28,8 @@ import { DataService } from './services/data.service';
2728
MultipleComponent,
2829
CustomArrayComponent,
2930
ValueChangedComponent,
30-
AllowClearComponent
31+
AllowClearComponent,
32+
FormComponent
3133
],
3234
imports: [
3335
BrowserModule,

src/app/demos/form/form.component.css

Whitespace-only changes.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<h2>10. Form demo</h2>
2+
<form (ngSubmit)="onSubmit()"
3+
#testForm="ngForm">
4+
5+
<div class="form-group"
6+
*ngIf="!submitted else showData">
7+
<label for="name">Name: </label>
8+
<ng-select2 [data]="exampleData"
9+
[(ngModel)]="value"
10+
required
11+
name="name"
12+
#name="ngModel"
13+
[width]="300">
14+
</ng-select2>
15+
<div [hidden]="name.valid || name.pristine"
16+
class="alert alert-danger">
17+
Name is required
18+
</div>
19+
</div>
20+
21+
<button *ngIf="!submitted"
22+
type="submit"
23+
class="btn btn-success"
24+
[disabled]="!testForm.form.valid">Submit</button>
25+
<button type="button"
26+
class="btn btn-default"
27+
(click)="testForm.reset(); submitted = false">Reset</button>
28+
</form>
29+
30+
<ng-template #showData>
31+
<p>Form is submitted with value: {{value}}</p>
32+
</ng-template>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { Component, OnInit } from '@angular/core';
2+
import { FormControl } from '@angular/forms';
3+
import { Select2OptionData } from 'ngSelect2';
4+
5+
@Component({
6+
selector: 'app-form',
7+
templateUrl: './form.component.html',
8+
styleUrls: ['./form.component.css']
9+
})
10+
export class FormComponent implements OnInit {
11+
public exampleData: Array<Select2OptionData>;
12+
13+
public formControl = new FormControl();
14+
public value: string;
15+
16+
ngOnInit() {
17+
this.exampleData = [
18+
{
19+
id: 'cool-guy',
20+
text: 'Cool guy'
21+
}
22+
];
23+
}
24+
25+
public submitted = false;
26+
27+
onSubmit() {
28+
this.submitted = true;
29+
}
30+
}

0 commit comments

Comments
 (0)