feat: create, view, edit, delete data-table (#365)

- Add view-data-table
- Add edit-data-table
- Add create-data-table

Fixes #171

Co-authored-by: Chirag Gupta <gchirag@apache.org>
This commit is contained in:
Chirag Gupta
2020-03-23 02:06:57 +05:30
committed by GitHub
co-authored by Chirag Gupta
parent 17197056bb
commit b1a3646bc2
23 changed files with 1302 additions and 8 deletions
@@ -0,0 +1,109 @@
<div class="container">
<mat-card>
<form [formGroup]="dataTableForm" (ngSubmit)="submit()">
<mat-card-content>
<div fxLayout="row wrap" fxLayoutGap="2%" fxLayout.lt-md="column">
<mat-form-field fxFlex="40%">
<mat-label>Data Table Name</mat-label>
<input matInput required formControlName="datatableName">
<mat-error *ngIf="dataTableForm.controls.datatableName.hasError('required')">
Data Table Name is <strong>required</strong>
</mat-error>
</mat-form-field>
<mat-form-field fxFlex="40%">
<mat-label>Application Table Name</mat-label>
<mat-select required formControlName="apptableName">
<mat-option *ngFor="let appTable of appTableData" [value]="appTable.value">
{{ appTable.displayValue }}
</mat-option>
</mat-select>
<mat-error *ngIf="dataTableForm.controls.apptableName.hasError('required')">
Application Table Name is <strong>required</strong>
</mat-error>
</mat-form-field>
<div fxFlex="14%" class="multi-row-wrapper">
<mat-checkbox labelPosition="before" formControlName="multiRow" class="multi-row">Multi Row</mat-checkbox>
</div>
</div>
<br>
<div fxLayout="row wrap" fxLayoutGap="60%" fxLayout.lt-md="column">
<p fxFlex="20%" class="mat-title">Columns</p>
<button mat-raised-button fxFlex="20%" type="button" color="primary" (click)="addColumn()">
<fa-icon icon="plus"></fa-icon>&nbsp;&nbsp;
Add Column
</button>
</div>
<br>
<table mat-table [dataSource]="dataSource" matSort>
<ng-container matColumnDef="name">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Name </th>
<td mat-cell *matCellDef="let column"> {{ column.name }} </td>
</ng-container>
<ng-container matColumnDef="type">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Type </th>
<td mat-cell *matCellDef="let column"> {{ column.type }} </td>
</ng-container>
<ng-container matColumnDef="mandatory">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Mandatory </th>
<td mat-cell *matCellDef="let column"> {{ column.mandatory }} </td>
</ng-container>
<ng-container matColumnDef="length">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Length </th>
<td mat-cell *matCellDef="let column"> {{ column.length }} </td>
</ng-container>
<ng-container matColumnDef="code">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Code </th>
<td mat-cell *matCellDef="let column"> {{ column.code }} </td>
</ng-container>
<ng-container matColumnDef="actions">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Actions </th>
<td mat-cell *matCellDef="let column" fxLayoutGap="15%">
<button type="button" color="primary" mat-icon-button (click)="editColumn(column)">
<fa-icon icon="edit" size="lg"></fa-icon>
</button>
<button type="button" color="warn" mat-icon-button (click)="deleteColumn(column)">
<fa-icon icon="trash" size="lg"></fa-icon>
</button>
</td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
<mat-paginator [pageSizeOptions]="[10, 25, 50, 100]" showFirstLastButtons></mat-paginator>
</mat-card-content>
<mat-card-actions fxLayout="row" fxLayout.xs="column" fxLayoutAlign="center" fxLayoutGap="5px">
<button type="button" mat-raised-button [routerLink]="['../']">Cancel</button>
<button mat-raised-button color="primary"
[disabled]="!dataTableForm.valid || columnData.length === 0">Submit</button>
</mat-card-actions>
</form>
</mat-card>
</div>
@@ -0,0 +1,18 @@
@media (min-width: 992px) {
.multi-row-wrapper {
position: relative;
.multi-row {
padding: 0 0 17.5px 0;
position: absolute;
bottom: 0;
right: 0;
}
}
}
table {
width: 100%;
}
@@ -0,0 +1,25 @@
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { CreateDataTableComponent } from './create-data-table.component';
describe('CreateDataTableComponent', () => {
let component: CreateDataTableComponent;
let fixture: ComponentFixture<CreateDataTableComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ CreateDataTableComponent ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(CreateDataTableComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
@@ -0,0 +1,188 @@
/** Angular Imports */
import { Component, OnInit, ViewChild } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { ActivatedRoute, Router } from '@angular/router';
import { MatTableDataSource, MatPaginator, MatSort, MatDialog } from '@angular/material';
/** Custom Services */
import { SystemService } from '../../system.service';
/** Data Imports */
import { appTableData } from '../app-table-data';
/** Custom Components */
import { ColumnDialogComponent } from '../column-dialog/column-dialog.component';
import { DeleteDialogComponent } from 'app/shared/delete-dialog/delete-dialog.component';
/**
* Create Data Table Component.
*/
@Component({
selector: 'mifosx-create-data-table',
templateUrl: './create-data-table.component.html',
styleUrls: ['./create-data-table.component.scss']
})
export class CreateDataTableComponent implements OnInit {
/** Data Table Form */
dataTableForm: FormGroup;
/** Application Table Data */
appTableData = appTableData;
/** Column Data */
columnData: any[] = [];
/** Data passed to dialog. */
dataForDialog: {
columnName: string,
columnDisplayType: string,
isColumnPrimaryKey: boolean,
columnLength: string,
columnCode: string,
columnCodes: any,
type: string
} = {
columnName: undefined,
columnDisplayType: undefined,
isColumnPrimaryKey: undefined,
columnLength: undefined,
columnCode: undefined,
columnCodes: undefined,
type: undefined
};
/** Columns to be displayed in columns table. */
displayedColumns: string[] = ['name', 'type', 'mandatory', 'length', 'code', 'actions'];
/** Data source for columns table. */
dataSource: MatTableDataSource<any>;
/** Paginator for columns table. */
@ViewChild(MatPaginator) paginator: MatPaginator;
/** Sorter for columns table. */
@ViewChild(MatSort) sort: MatSort;
/**
* Retrieves the column codes data from `resolve`.
* @param {FormBuilder} formBuilder Form Builder.
* @param {SystemService} systemService System Service.
* @param {ActivatedRoute} route Activated Route.
* @param {Router} router Router for navigation.
* @param {MatDialog} dialog Dialog Reference.
*/
constructor(private formBuilder: FormBuilder,
private systemService: SystemService,
private route: ActivatedRoute,
private router: Router,
private dialog: MatDialog) {
this.route.data.subscribe((data: { columnCodes: any }) => {
this.dataForDialog.columnCodes = data.columnCodes;
});
}
/**
* Creates data table form and sets columns table.
*/
ngOnInit() {
this.createDataTableForm();
this.setColumns();
}
/**
* Initializes the data source, paginator and sorter for columns table.
*/
setColumns() {
this.dataSource = new MatTableDataSource(this.columnData);
this.dataSource.paginator = this.paginator;
this.dataSource.sort = this.sort;
}
/**
* Creates the data table form.
*/
createDataTableForm() {
this.dataTableForm = this.formBuilder.group({
'datatableName': ['', Validators.required],
'apptableName': ['', Validators.required],
'multiRow': ['']
});
}
/**
* Adds a new column.
*/
addColumn() {
this.dataForDialog.columnName = undefined;
this.dataForDialog.columnDisplayType = undefined;
this.dataForDialog.isColumnPrimaryKey = false;
this.dataForDialog.columnLength = undefined;
this.dataForDialog.columnCode = undefined;
this.dataForDialog.type = 'new';
const addColumnDialogRef = this.dialog.open(ColumnDialogComponent, {
data: this.dataForDialog
});
addColumnDialogRef.afterClosed().subscribe((response: any) => {
if (response !== '') {
this.columnData.push({
name: response.name,
type: response.type,
mandatory: response.mandatory,
length: response.length,
code: response.code
});
this.dataSource.connect().next(this.columnData);
}
});
}
/**
* Edits column.
* @param {any} column Column.
*/
editColumn(column: any) {
this.dataForDialog.columnName = column.name;
this.dataForDialog.columnDisplayType = column.type;
this.dataForDialog.isColumnPrimaryKey = column.mandatory;
this.dataForDialog.columnLength = column.length;
this.dataForDialog.columnCode = column.code;
this.dataForDialog.type = 'new';
const editColumnDialogRef = this.dialog.open(ColumnDialogComponent, {
data: this.dataForDialog
});
editColumnDialogRef.afterClosed().subscribe((response: any) => {
if (response !== '') {
this.columnData[this.columnData.findIndex(newColumn => newColumn.name === column.name)] = {
name: response.name,
type: response.type,
mandatory: response.mandatory,
length: response.length,
code: response.code
};
this.dataSource.connect().next(this.columnData);
}
});
}
/**
* Deletes the column.
* @param {any} column Column.
*/
deleteColumn(column: any) {
const deleteColumnDialogRef = this.dialog.open(DeleteDialogComponent, {
data: { deleteContext: `column ${column.name}` }
});
deleteColumnDialogRef.afterClosed().subscribe((response: any) => {
if (response.delete) {
this.columnData.splice(this.columnData.indexOf(column), 1);
this.dataSource.connect().next(this.columnData);
}
});
}
/**
* Submits the data table form and creates data table,
* if successful redirects to view created data table.
*/
submit() {
this.dataTableForm.value.columns = this.columnData;
this.systemService.createDataTable(this.dataTableForm.value)
.subscribe((response: any) => {
this.router.navigate(['../', response.resourceIdentifier], { relativeTo: this.route });
});
}
}