import { Component, OnInit, Input, Output, EventEmitter, ElementRef, ViewChild, ContentChild, TemplateRef, IterableDiffer, IterableDiffers, Directive, Inject } from '@angular/core';
import { CfCoreComponent } from '../core/core.component';
import { InputModel } from '../../models/input/input.model';
import { DatatableModel } from '../../models/datatable';
import { DatatableColumnModel } from '../../models/datatable-column';
/**
*
Datatable is creating using DatatableModel and DatatableColumnModel Classes and Input, Icon and Button Components.
* Example of using Menu:
*
* <cf-datatable [settings]="myDatatable" (cfDatatableRowEvent)="log($event)"></cf-datatable>
*
*/
@Component({
selector: 'cf-datatable',
template: " {{value}} ",
styles: [".cf-datatable-top-options { display: flex; flex-direction: row; justify-content: space-between; } .cf-datatable-top-options .cf-datatable-allrow-togglers { display: flex; flex-grow: 1; align-items: flex-end; } .cf-datatable-top-options .cf-datatable-allrow-togglers a, .cf-datatable-top-options .cf-datatable-allrow-togglers span { margin: 0 4px 14px 0; } .cf-datatable-top-options .cf-datatable-allrow-togglers span { margin-bottom: 16px; } .cf-datatable-top-options .cf-datatable-allrow-togglers a { text-decoration: none !important; outline: none !important; } .cf-datatable-top-options cf-input { display: flex; flex-grow: 1; justify-content: flex-end; } .cf-datatable-toggle-row { display: flex; width: 16px; height: 16px; text-decoration: none !important; outline: none !important; } :host /deep/ .datatable-body-cell { display: flex !important; align-items: center; } :host /deep/ .datatable-checkbox { padding: 0px !important; } :host /deep/ .ngx-datatable .datatable-body .datatable-scroll { width: 100% !important; } "]
})
export class CfDatatableComponent extends CfCoreComponent implements OnInit {
/**
* Component property for registering Datatable and for connection to ngx-datatable functionality.
*/
@ViewChild('cfDatatableElement') cfTable: any;
/**
* Component property for rendering template for each Datatable item details row.
*/
@ContentChild('rowDetailsTpl') cfRowTemplate;
/**
* It is Object which will recive data from DatatableModel Class. It is used in cf-datatable template.
* Example of DatatableModel Class:
* {
* rows: [
* { name: 'Austin', city: 'London' },
* { name: 'Molly', city: 'Tokyo' },
* { name: 'Austin', city: 'New York' },
* { name: 'Monica', city: 'Paris' },
* { name: 'Austin', city: 'Dubai' }
* ],
* columns: [
* { name: 'Name', type: 'standard' },
* { name: 'City', type: 'standard' }
* ],
* rowHeight: 50,
* limit: 3,
* filtering: true,
* expandable: {
* value: true,
* detailsHeight: 140
* },
* selected: this.mySelected
* }
* rows: array
* It is an array with data for rows of any type.
* columns: array
* It is an array with columns definition. Each column item must have two required properties: name and type. Name will be used as column header. Type will set layout of each column and can be: standard(default), cf-icon, cf-button.
* rowHeight: number
* It is height of each row. Can be numeric (number will be transformed to pixels) or 'auto'. Optional. Not set by default. Required when rows expanding is set.
* limit: number
* Means rows quontity per page. Optional.
* filtering: boolean
* It means if Filter must be builded to Datatable. Type boolean. Optional. Default false.
* expandable: object
* It is object with properties for rows details. Optional.
* value property of expandable: boolean
* It is boolean value to build or not row details.
* detailsHeight property of expandable: boolean
* It is number value and mean height of row in pixels. Required.
* selected: array
* Array for selected items. Optional.
*/
cfDatatable: DatatableModel;
// differ: IterableDiffer;
cfOriginalRows: Array = [];
cfClonedRows: Array = [];
/**
* It is array of Datatable filtered items.
*/
filtered: Array = [];
/**
* It is array of Datatable selected items.
*/
selected: Array = [];
/**
* Temporary object for expanded row.
*/
expanded: any = {};
/**
* CfInput component used inside filtering section.
*/
input = new InputModel ({
type : "text",
placeholder: "Filter",
prefix : "",
width: "",
maxlength : "",
dividerColor : "",
value : "",
suffix : "",
hint : {
text : "",
align : ""
},
icon: {
name: "filter_list",
type: "mi",
size: "20px",
color: {
foreground: "#757575",
background: "transparent"
}
},
iconPosition:"right",
menu: null
});
/**
* It is [settings] attribute of cf-datatable component with json object generated by DatatableModel.
*/
@Input()
settings: DatatableModel;
/**
* It is (cfDatatableRowEvent) attribute of cf-datatable component. Means what event to call when row Button of Datatable was clicked.
*/
@Output()
cfDatatableRowEvent: EventEmitter = new EventEmitter();
/**
* It is (cfDatatableRowDetailsEvent) attribute of cf-datatable component. Means what event to call when Datatable details row was clicked.
*/
@Output()
cfDatatableRowDetailsEvent: EventEmitter = new EventEmitter();
// constructor(public elementRef: ElementRef, differs: IterableDiffers) {
constructor(public elementRef: ElementRef) {
super(elementRef);
// this.differ = differs.find([]).create(null);
}
/**
* It is method which will emit corresponding event when Datatable button row or Datatable details row where clicked. Event contains index and value of clicked row.
* @param type It is type of template. Can be item(for row Button) or details(for item details row template).
* @param i index of row
* @param it object of row
*/
cfDatatableRowEvents(type:string, i:number, it:Object) {
// console.log('cfDatatableRowEvent');
switch (type) {
case "item": this.cfDatatableRowEvent.emit({ cfIndex: i, cfItem: it }); break;
case "details": this.cfDatatableRowDetailsEvent.emit({ cfIndex: i, cfItem: it }); break;
}
this.checkFiltered(type, i, it);
}
/**
* Function for filtering Datatable. Goes to first page of Datatable after filtering.
*/
updateFilter(event) {
const val = this.input.value;
const filtered = this.cfDatatable.rows.filter(function(d) {
return JSON.stringify(d).toLowerCase().indexOf(val) !== -1 || !val;
});
this.cfClonedRows = filtered;
this.cfTable.offset = 0;
}
/**
* Function for selection rows with rows checkboxes.
*/
onSelect({ selected }) {
// console.log('selected', selected);
this.cfDatatable.selected.splice(0, this.cfDatatable.selected.length);
this.cfDatatable.selected.push(...selected);
}
onSort(event){
console.log('sort event', event);
// const sortTemp = this.cfDatatable.rows;
// this.cfDatatable.rows = [];
// setTimeout(() => {
// this.cfDatatable.rows = sortTemp;
// }, 0);
// this.cfDatatable.rows = sortTemp;
}
/**
* Function for toggle each row details section.
*/
toggleExpandRow(row) {
this.cfTable.rowDetail.toggleExpandRow(row);
}
onDetailToggle(event) {}
/**
* Function for updating Datatable when rows length was changed by user custom functions.
*/
checkFiltered(type:string, i:number, it:Object) {
if (this.cfDatatable.rows.length !== this.cfOriginalRows.length) {
this.cfOriginalRows.splice(0, this.cfOriginalRows.length);
this.cfOriginalRows = [...this.cfDatatable.rows];
this.cfClonedRows = [...this.cfOriginalRows];
}
// console.log('this.cfDatatable.rows.length', this.cfDatatable.rows.length);
// console.log('this.filtered.length', this.filtered.length);
}
/**
* It will be generated cfDatatable object and rendered inside component template. Also arrays for filtering/selection will be initialized.
*/
ngOnInit() {
this.cfDatatable = this.settings;
this.cfOriginalRows = [...this.cfDatatable.rows];
this.cfClonedRows = [...this.cfOriginalRows];
// if (!this.settings.selected) this.cfDatatable.selected = [];
}
// ngDoCheck() {
// const changes = this.differ.diff(this.cfDatatable.rows);
// if (changes) {
// console.log('change');
// // changes.forEachAddedItem(r => console.log('added ', r));
// // changes.forEachRemovedItem(r => {
// // console.log('removed ', r);
// // this.temp.splice(r.previousIndex);
// // });
// // changes.forEachMovedItem(r => console.log('moved ', r));
// }
// }
}