import { Type } from '@angular/core';
|
import { Types } from '@core/enum/types.enum';
|
export interface Column {
|
text?: string;
|
name?: string;
|
value?: any;
|
width?: string;
|
sort?: boolean;
|
type?: DataType;
|
}
|
export interface DataType {
|
name: Types;
|
format?: any;
|
}
|
// 作为http来回传递参数
|
export interface PageBean {
|
pageIndex?: number ; // 页码
|
pageSize?: number ; // 一页多少条
|
total?: number;
|
data?: any[];
|
queryParams?: string;
|
}
|
export class Grid implements PageBean {
|
title = '';
|
pageIndex = 0; // 页码
|
pageSize = 10; // 每页几行数据
|
data: any[] = [];
|
total = 0;
|
queryParams = '';
|
pages = 0; // 总页数
|
queryMap?: any = {};
|
size = 0; // 当前页数据条数
|
sorts?: any[] = [];
|
indeterminate = false;
|
allChecked = false;
|
selectedIndexs?: number[] = []; // 被选中序列号
|
columns?: Column[] = [];
|
|
checkAll(value: boolean) {
|
this.data.forEach(
|
row => {row.checked = value; }
|
);
|
this.refreshStatus();
|
}
|
getData(): any [] {
|
return this.data;
|
}
|
initData(pageData: {data?: any[], total?: number}) {
|
if ( pageData != null && pageData.data != null) {
|
let index = 0;
|
this.data = pageData.data.map(row => {
|
row['index'] = index++;
|
return row;
|
});
|
this.total = pageData.total;
|
}
|
}
|
refreshStatus() {
|
const data = this.data;
|
const allChecked = data.every(value => value.checked);
|
const allUnChecked = data.every(value => !value.checked);
|
this.allChecked = allChecked;
|
this.indeterminate = (!allChecked) && (!allUnChecked);
|
this.selectedIndexs = data.filter(value => value.checked).map(
|
row => {
|
return row['index'] != null ? row['index'] : 0;
|
}
|
);
|
this.selectedIndexs = this.selectedIndexs == null ? [] : this.selectedIndexs;
|
}
|
constructor(config: object) {
|
if (config != null) {
|
const keys = Object.getOwnPropertyNames(config);
|
for (const index in keys) {
|
const key = keys[index];
|
if (this[key] != null) {
|
this[key] = config[key];
|
}
|
}
|
}
|
}
|
public setColumns(obj: Object) {
|
this.columns = this.objectToArray(obj);
|
}
|
private objectToArray(obj: Object): any[] {
|
const arr = [];
|
const keys = Object.getOwnPropertyNames(obj);
|
for (const index in keys) {
|
const key = keys[index];
|
const item = obj[key];
|
if ( item['name'] == null) {
|
item['name'] = key;
|
}
|
arr.push(item);
|
}
|
return arr;
|
}
|
}
|