2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 2019 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
8 * Unless otherwise specified, all software contained herein is licensed
9 * under the Apache License, Version 2.0 (the "License");
10 * you may not use this software except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * Unless otherwise specified, all documentation contained herein is licensed
22 * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23 * you may not use this documentation except in compliance with the License.
24 * You may obtain a copy of the License at
26 * https://creativecommons.org/licenses/by/4.0/
28 * Unless required by applicable law or agreed to in writing, documentation
29 * distributed under the License is distributed on an "AS IS" BASIS,
30 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31 * See the License for the specific language governing permissions and
32 * limitations under the License.
34 * ============LICENSE_END============================================
39 import { Component, Input, OnInit, OnChanges, ViewChild, AfterViewInit, ElementRef} from '@angular/core';
40 import { MatPaginator, MatSort, MatTable, MatTableDataSource } from '@angular/material';
41 import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
42 import { RdpDataTableService } from '../shared/rdp-data-table.service';
43 import { RdpDataTableEditComponent } from './rdp-data-table-edit/rdp-data-table-edit.component';
44 import { noop as _noop, update } from 'lodash-es';
45 import { RDPDataSource } from '../datasouce/RDPDataSource';
46 import { debounceTime, distinctUntilChanged, tap } from 'rxjs/operators';
47 import { merge, fromEvent } from "rxjs";
48 import { RdpModalService } from '../services/rdp-modal.service';
52 selector: 'rdp-data-table',
53 templateUrl: './rdp-data-table.component.html',
54 styleUrls: ['./rdp-data-table.component.scss']
56 export class RdpDataTableComponent<T> implements OnChanges, AfterViewInit, OnInit {
59 @Input() settings: any;
61 @ViewChild(MatSort) sort: MatSort;
62 //@ViewChild(MatPaginator) paginator: MatPaginator;
63 private paginator: MatPaginator;
65 @ViewChild(MatPaginator) set matPaginator(mp: MatPaginator) {
67 this.setData(this.data);
70 @ViewChild(MatTable) table: MatTable<T>;
71 @ViewChild('input') input: ElementRef;
75 applicationService: any;
76 public displayedColumns = [];
77 public columnsInfoList = [];
79 isPaginationRequired: boolean = false;
82 isSearchEnabled: boolean;
83 isServerSidePaginationEnabled: boolean = false;
84 showAddButton: boolean = false;
89 constructor(public dataTableService: RdpDataTableService, private rdpModal: RdpModalService) { }
93 this.setData(this.data);
98 if(this.settings && this.settings.isServerSidePaginationEnabled && this.data){
100 this.sort.sortChange.subscribe(() => this.paginator.pageIndex = 0);
102 fromEvent(this.input.nativeElement,'keyup')
105 distinctUntilChanged(),
107 this.paginator.pageIndex = 0;
109 this.loadData(this.paginator.pageIndex, this.paginator.pageSize);
113 merge(this.sort.sortChange, this.paginator.page)
115 tap(() => this.loadData(this.paginator.pageIndex, this.paginator.pageSize))
119 this.dataSource.paginator = this.paginator;
120 this.dataSource.sort = this.sort;
126 this.setData(this.data);
129 console.log("Table setting Objects >>>>", this.settings);
131 this.applicationService = this.settings.applicationService;
133 this.settings.columns.forEach(element => {
134 this.displayedColumns.push(element.title);
135 this.columnsInfoList.push(element);
138 if (!this.settings.isReadOnly) {
139 this.displayedColumns.push('edit');
142 if (!this.settings.isReadOnly) {
143 this.displayedColumns.push('delete');
146 if (this.settings.isReadOnly) {
147 this.showAddButton = false;
149 this.showAddButton = true;
152 if (this.settings.isTableSearchEnabled) {
153 this.isSearchEnabled = true;
156 if(this.settings.isServerSidePaginationEnabled){
157 this.isServerSidePaginationEnabled = true;
160 if (this.settings.isToggleEnabled) {
161 this.displayedColumns.push('toggle');
164 if (this.settings.isPaginationEnabled) {
165 this.isPaginationRequired = true;
166 if (this.settings.paginationsSize) {
167 this.pageSize = this.settings.paginationsSize;
170 console.log("this.displayedColumns>>>>>", this.displayedColumns);
175 if(this.settings && this.settings.isServerSidePaginationEnabled){
176 console.log("Server side pagination is enabled");
177 this.dataSource = new RDPDataSource();
178 this.dataSource.loadData(this.settings.applicationService,'', this.sort.active, this.sort.direction, 0, this.settings.paginationsSize);
179 this.totalRowsCount = this.getTotalRowCount();
182 this.dataSource = new MatTableDataSource([]);
183 console.log("Server side pagination is not enabled");
184 if (Array.isArray(data)) {
185 this.dataSource.data = data;
186 this.dataSource.paginator = this.paginator;
187 this.dataSource.sort = this.sort;
188 this.totalRowsCount = data.length;
193 getTotalRowCount(): any {
196 totalRows = this.settings.applicationService.getTotalRowCount();
198 console.log("Error while getting total row count :: ",error);
203 loadData(pageIndex:any, pageSize:any) {
204 this.dataSource = new RDPDataSource();
205 this.dataSource.loadData(this.settings.applicationService, this.input.nativeElement.value, this.sort.active, this.sort.direction, pageIndex , pageSize);
208 onPaginationChange(event:any){
209 console.log("onPaginationChange event :: ",event);
210 //this.loadData(event.pageIndex, event.pageSize);
214 this.dataTableService.add(this.applicationService, data);
217 updateRow(data: any) {
218 return this.dataTableService.update(this.applicationService, data);
222 this.dataTableService.get(this.applicationService, data);
225 deleteRow(data: any) {
226 this.dataTableService.delete(this.applicationService, data);
229 applyFilter(filterValue: string) {
230 this.dataSource.filter = filterValue.trim().toLowerCase();
235 * openAddNewRoleModal
238 openEditModalPopup(rowData: any) {
239 this.showSpinner = true;
240 console.log("Row data : ", rowData);
241 this.cloneObject = Object.assign({}, rowData)
242 console.log("Update or Add functionality intialized");
243 const modalRef = this.rdpModal.open(RdpDataTableEditComponent, { size: 'lg' });
244 modalRef.componentInstance.title = 'Edit';
245 modalRef.componentInstance.settings = this.settings;
246 if (this.cloneObject.data != 'undefined' && this.cloneObject) {
247 modalRef.componentInstance.rowdata = this.cloneObject;
248 modalRef.componentInstance.applicationService = this.applicationService;
249 modalRef.componentInstance.isEditMode = true;
250 this.isEditMode = true;
252 modalRef.componentInstance.rowdata = {};
253 modalRef.componentInstance.isEditMode = false;
254 this.isEditMode = false;
256 modalRef.componentInstance.passEntry.subscribe((receivedEntry: any) => {
258 this.data = receivedEntry;
259 this.setData(this.data);
261 this.showSpinner = false;
266 handleScroll = (scrolled: boolean) => {
267 scrolled ? this.dataSource : _noop();
270 toggleUserActive(rowData: any) {
271 console.log("Row data : ", rowData);
274 //hasMore = () => !this.dataSource || this.dataSource.data.length < this.limit;