cs
fengxiang
2017-12-27 ecb20eb98f24b3f2e163301b7619678c87804c7e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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;
    }
}