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 @ViewChild(MatTable) table: MatTable<T>;
64 @ViewChild('input') input: ElementRef;
68 applicationService: any;
69 public displayedColumns = [];
70 public columnsInfoList = [];
72 isPaginationRequired: boolean = false;
75 isSearchEnabled: boolean;
76 isServerSidePaginationEnabled: boolean = false;
77 showAddButton: boolean = true;
82 constructor(public dataTableService: RdpDataTableService, private rdpModal: RdpModalService) { }
86 this.setData(this.data);
91 if(this.isServerSidePaginationEnabled){
93 this.sort.sortChange.subscribe(() => this.paginator.pageIndex = 0);
95 fromEvent(this.input.nativeElement,'keyup')
98 distinctUntilChanged(),
100 this.paginator.pageIndex = 0;
102 this.loadData(this.paginator.pageIndex, this.paginator.pageSize);
106 merge(this.sort.sortChange, this.paginator.page)
108 tap(() => this.loadData(this.paginator.pageIndex, this.paginator.pageSize))
112 this.dataSource.paginator = this.paginator;
113 this.dataSource.sort = this.sort;
118 console.log("Table setting Objects >>>>", this.settings);
120 this.applicationService = this.settings.applicationService;
122 this.settings.columns.forEach(element => {
123 this.displayedColumns.push(element.title);
124 this.columnsInfoList.push(element);
127 if (!this.settings.isReadOnly) {
128 this.displayedColumns.push('edit');
131 if (!this.settings.isReadOnly) {
132 this.displayedColumns.push('delete');
135 if (this.settings.isReadOnly) {
136 this.showAddButton = false;
139 if (this.settings.isTableSearchEnabled) {
140 this.isSearchEnabled = true;
143 if(this.settings.isServerSidePaginationEnabled){
144 this.isServerSidePaginationEnabled = true;
147 if (this.settings.isToggleEnabled) {
148 this.displayedColumns.push('toggle');
151 if (this.settings.isPaginationEnabled) {
152 this.isPaginationRequired = true;
153 if (this.settings.paginationsSize) {
154 this.pageSize = this.settings.paginationsSize;
157 console.log("this.displayedColumns>>>>>", this.displayedColumns);
162 if(this.settings.isServerSidePaginationEnabled){
163 console.log("Server side pagination is enabled");
164 this.dataSource = new RDPDataSource();
165 this.dataSource.loadData(this.settings.applicationService,'', this.sort.active, this.sort.direction, 0, this.settings.paginationsSize);
166 this.totalRowsCount = this.getTotalRowCount();
169 this.dataSource = new MatTableDataSource([]);
170 console.log("Server side pagination is not enabled");
171 if (Array.isArray(data)) {
172 this.dataSource.data = data;
173 this.totalRowsCount = data.length;
178 getTotalRowCount(): any {
181 totalRows = this.settings.applicationService.getTotalRowCount();
183 console.log("Error while getting total row count :: ",error);
188 loadData(pageIndex:any, pageSize:any) {
189 this.dataSource = new RDPDataSource();
190 this.dataSource.loadData(this.settings.applicationService, this.input.nativeElement.value, this.sort.active, this.sort.direction, pageIndex , pageSize);
193 onPaginationChange(event:any){
194 console.log("onPaginationChange event :: ",event);
195 //this.loadData(event.pageIndex, event.pageSize);
199 this.dataTableService.add(this.applicationService, data);
202 updateRow(data: any) {
203 return this.dataTableService.update(this.applicationService, data);
207 this.dataTableService.get(this.applicationService, data);
210 deleteRow(data: any) {
211 this.dataTableService.delete(this.applicationService, data);
214 applyFilter(filterValue: string) {
215 this.dataSource.filter = filterValue.trim().toLowerCase();
220 * openAddNewRoleModal
223 openEditModalPopup(rowData: any) {
224 this.cloneObject = Object.assign({}, rowData)
225 console.log("Copied Object : ", this.cloneObject);
226 if (this.cloneObject) {
227 const modalRef = this.rdpModal.open(RdpDataTableEditComponent, { size: 'lg' });
228 modalRef.componentInstance.title = 'Edit';
229 modalRef.componentInstance.settings = this.settings;
230 if (this.cloneObject != 'undefined' && this.cloneObject) {
231 modalRef.componentInstance.rowdata = this.cloneObject;
232 modalRef.componentInstance.isEditMode = true;
233 this.isEditMode = true;
235 modalRef.componentInstance.rowdata = {};
236 modalRef.componentInstance.isEditMode = false;
237 this.isEditMode = false;
239 modalRef.componentInstance.passEntry.subscribe((receivedEntry: any) => {
241 console.log("Original Object : ", rowData);
242 let response = this.updateRow(receivedEntry);
243 console.log("Response form application ",response);
244 console.log("Entry : ",receivedEntry);
245 rowData = receivedEntry;
246 if(this.dataTableService.response == "Success"){
247 console.log("Result is success, update the tabel");
248 this.columnsInfoList.push(receivedEntry);
249 console.log("Updtae column info list : ", this.columnsInfoList);
256 handleScroll = (scrolled: boolean) => {
257 scrolled ? this.dataSource : _noop();
260 toggleUserActive(rowData: any) {
261 console.log("Row data : ", rowData);
264 //hasMore = () => !this.dataSource || this.dataSource.data.length < this.limit;