File
Implements
Index
Properties
|
|
Methods
|
|
Inputs
|
|
Outputs
|
|
Constructor
constructor(fb: FormBuilder, toast: ToastService, modal: ModalController)
|
|
Parameters :
Name |
Type |
Optional |
fb |
FormBuilder
|
No
|
toast |
ToastService
|
No
|
modal |
ModalController
|
No
|
|
selectedCategories
|
Type : {}
|
Default value : []
|
|
Outputs
onSubmit
|
Type : EventEmitter
|
|
Methods
categorySelect
|
categorySelect(event, option)
|
|
Parameters :
Name |
Optional |
event |
No
|
option |
No
|
|
Public
prepareForm
|
prepareForm()
|
|
|
validateOptions
|
validateOptions()
|
|
|
categoryData
|
Type : []
|
Default value : []
|
|
catgeoryForm
|
Type : FormGroup
|
|
Public
fb
|
Type : FormBuilder
|
|
otherCategory
|
Type : object
|
Default value : { input: 'text', field: 'otherCategories', value: '', show: false, validation: { required: true } }
|
|
import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
import { FormGroup, FormControl, Validators, FormBuilder } from '@angular/forms';
import { ModalController } from '@ionic/angular';
import { ToastService } from '../../core';
import * as _ from 'underscore';
@Component({
selector: 'app-category-select',
templateUrl: './category-select.component.html',
styleUrls: ['./category-select.component.scss'],
})
export class CategorySelectComponent implements OnInit {
@Input() categories;
@Input() selectedCategories = [];
@Output() onSubmit = new EventEmitter();
catgeoryForm: FormGroup;
categoryData = [];
otherCategoryValue;
otherCategory = { input: 'text', field: 'otherCategories', value: '', show: false, validation: { required: true } };
constructor(
public fb: FormBuilder,
public toast: ToastService,
private modal: ModalController
) { }
ngOnInit() {
let other;
if( this.selectedCategories){
other = this.selectedCategories.filter(obj1 => {
return !this.categories.options.find((s) => s.label == obj1.label);
})[0]
this.selectedCategories.map(c => {
if (c.label && other && other.label && c.label == other.label) {
c.value = other.label
c.label = "Others"
}
})
}
this.categoryData.push(this.categories);
this.categoryData.push(this.otherCategory);
if (this.selectedCategories.length) {
this.selectedCategories.forEach(selctedCatgory => {
if (selctedCatgory.label == "Others") {
this.otherCategory.value = selctedCatgory.value;
this.otherCategory.show = true;
}
})
this.validateOptions();
console.log(this.selectedCategories)
} else {
this.prepareForm();
console.log(this.selectedCategories)
}
}
public prepareForm() {
const controls = {};
const validationsArray = [];
this.categoryData.forEach(element => {
if (element.field == 'otherCategories') {
if (element.validation.required) {
element.validation.name = "required"
validationsArray.push(
Validators.required
);
}
controls[element.field] = new FormControl(element.value||'', validationsArray);
}
})
this.catgeoryForm = this.fb.group(
controls
);
}
validateOptions() {
for (const cd of this.categoryData) {
if (cd.input == 'select') {
for (const category of this.selectedCategories) {
let index = _.findIndex(cd.options, { value: category.value });
index = index <= -1 ? _.findIndex(cd.options, { _id: category.value }) : index
if (index > -1) {
cd.options[index].isChecked = true;
}
}
}
}
this.selectedCategories.forEach(option => {
if (option.label == "Others") {
this.otherCategory.show = true;
// added to show others if selected
this.categoryData[0].options.map((o) => {
if (o.label == "Others") {
o.isChecked = true
}
});
//
}
});
this.prepareForm();
}
categorySelect(event, option) {
if (option.label == "Others") {
this.categoryData.forEach(data => {
if (data.field == 'otherCategories') {
data.show = event.detail.checked;
data.value = '';
}
})
}
option.isChecked = event.detail.checked;
}
submit() {
let valid = true;
this.selectedCategories = [];
this.categoryData.forEach(element => {
if (element.input == 'select') {
element.options.forEach(option => {
if (option.isChecked) {
if (option.value == "others") {
if (this.catgeoryForm.value.otherCategories) {
option.value = this.catgeoryForm.value.otherCategories;
} else {
valid = false;
}
} else {
option.name = option.label;
}
this.selectedCategories.push(option);
}
});
}
});
valid ? this.modal.dismiss(this.selectedCategories) : this.toast.showMessage('FRMELEMNTS_LBL_PLEASE_ADD_OTHERCATEGORIES', 'danger')
}
close() {
this.modal.dismiss()
}
}
<ion-content>
<div class="custom-popup">
<div class="pop-container" *ngIf="catgeoryForm">
<h5 style="text-align: center;">{{'FRMELEMNTS_LBL_CATEGORIES' | translate}}</h5>
<form [formGroup]="catgeoryForm">
<div *ngFor="let data of categoryData">
<div *ngIf="data.input == 'select'">
<ion-list>
<ion-item *ngFor="let category of data.options" (ionChange)="categorySelect($event,category)">
<ion-checkbox slot="start" [checked]="category.isChecked" value="{{category.value}}"
(ionChange)="categorySelect($event,category)">
</ion-checkbox>
<ion-label> {{(category?.labelTranslations?category?.labelTranslations: category?.name || category?.label)| translateJson}}</ion-label>
</ion-item>
</ion-list>
</div>
<ion-item *ngIf="data.input == 'text' && data.show">
<ion-input type="text" name="{{data.field}}" placeholder="{{'FRMELEMNTS_LBL_SPECIFY_OTHER' | translate}}"
[formControlName]="data.field" value="{{data.value}}"></ion-input>
</ion-item>
</div>
</form>
</div>
</div>
</ion-content>
<ion-footer>
<ion-row>
<ion-col size="6">
<ion-button (click)="close()" expand="block" class="custom-btn-txt-transform-none">
{{'CANCEL' | translate}}
</ion-button>
</ion-col>
<ion-col size="6">
<ion-button (click)="submit()" expand="block" class="custom-btn-txt-transform-none">
{{'FRMELEMNTS_BTN_SUBMIT' | translate}}
</ion-button>
</ion-col>
</ion-row>
</ion-footer>
Legend
Html element with directive