0

I am creating create functionaltiy which is part of CRUD operation for one of my model in angular 7 application. I am using template driven forms. As you can see I have temporarily set the ManagerId to 0 to initiate the Create. As you can also see in my html , I am only displaying the contents if the ManagerDetails is not null. At the moment I am getting blank form. How I show blank fields on the screen.

import { Component, Injectable, NgZone, ViewEncapsulation, ViewChild } from '@angular/core';
import { OnInit, Input } from '@angular/core';
import { ManagerService } from '../services/manager.service';
import * as ClassicEditor from '@ckeditor/ckeditor5-build-classic';
import { CommonDataService } from '../services/common.data.service';
import { IManager } from './manager.interface';

@Component({
    selector: 'mgr-manager',
    templateUrl: './manager.component.html'
})


export class ManagerComponent implements OnInit {

    private Error: string;
    public ManagerDetails: any;
    public EditMode: boolean;
    public Editor = ClassicEditor;
    public EditorConfig: string;
    @Input() ManagerId: number;

    constructor(private managerService: ManagerService, private commonDataService: CommonDataService) {

    }

    ngOnInit() {
        this.ManagerId = 0;
        if (this.ManagerId !== 0 ) {
            this.getManagerDetails();
        } else {


        }
    }

    getManagerDetails() {
        if (this.ManagerId != null) {
            this.managerService.getManagerDetails(this.ManagerId).subscribe((data: IManager[]) => {
                this.ManagerDetails = data;
            });
        }
    }

    saveManager() {

        if (this.ManagerDetails.ManagerId === 0) {
            this.managerService.createManager(this.ManagerDetails).then((result ) => {
                if (result) {
                    this.getManagerDetails();
                    this.EditMode = !this.EditMode;
                }
            });

        } else {
            this.managerService.updateManager(this.ManagerDetails).then((result) => {
                if (result) {
                    this.getManagerDetails();
                    this.EditMode = !this.EditMode;
                }
            });
        }

    }

}

html

<div class="card">
    <div class="card-header panel-heading">
        <span style="font-size: 18px; font-weight: bold; ">Manager Details</span>
        <div class="pull-right" style="padding-right:10px;">
            <label class="btn btn-primary" [ngClass]="{'btn-primary': EditMode, 'btn-default': !EditMode }"><input
                    type="checkbox" [(ngModel)]="EditMode" class="hidden">Edit Mode</label>

        </div>
    </div>
    <div class="card-body">
        <div *ngIf="ManagerDetails" class="card-body scrollClass" style="width:100%">

            <div class="form-group row">
                <label for="inputName" class="col-sm-2 col-form-label modal-label header">First Name</label>
                <div class="col-sm-9">
                    <div *ngIf="!EditMode">{{ManagerDetails.FirstName}}</div>
                    <input *ngIf="EditMode" kendoTextBox [readonly]="false" class="form-control"
                        [(ngModel)]="ManagerDetails.FirstName" />
                </div>


            </div>

            <div class="form-group row">
                <label for="inputName" class="col-sm-2 col-form-label modal-label header">Last Name</label>
                <div class="col-sm-9">
                    <div *ngIf="!EditMode">{{ManagerDetails.LastName}}</div>
                    <input *ngIf="EditMode" kendoTextBox [readonly]="false" class="form-control"
                        [(ngModel)]="ManagerDetails.LastName" />
                </div>


            </div>



        </div>
        <div class="btn-toolbar" style="padding-top:40px;">

            <span *ngIf="EditMode"><button type="button" class="btn btn-primary btn-view-all btn mr-3"
                    (click)="saveManager()">Save</button>

            </span>
            <span><button type="button" class="btn btn-primary btn-view-all btn mr-3"
                    (click)="cancelManager">Cancel</button>
            </span>
            <span><button type="button" style="float: right;" class="btn btn-primary btn-view-all"
                    (click)="deleteManager()">Delete</button>
            </span>
        </div>
    </div>
</div>
2
  • Do I need to create an empty object model Commented Mar 13, 2019 at 13:34
  • stackoverflow.com/questions/13142635/…, see the code const modal = {} as IModal; Commented Mar 13, 2019 at 15:32

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.