fengxiang
2018-01-05 45d40501534b95bfd4a8f744b560651a87849fa4
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import { Column } from '@core/entity/grid';
import { Types } from '@core/enum/types.enum';
 
export interface AreaNames {
    provinceName?: string;
    cityName?: string;
    areaName?: string;
}
export interface Column {
   text?: string;
   name?: string;
   width?: string;
   sort?: string;
   isSort?: boolean;
   type?: DataType;
   format?: (value: any, col: Column, row: any) => string;
}
export interface DataType {
    name: Types;
    format?: any;
}
// 作为http来回传递参数
export interface PageBean {
    pageIndex?: number ; // 页码
    pageSize?: number ; // 一页多少条
    total?: number;
    data?: any[];
    queryParams?: string;
    orderByClause?: string;
    getOrderByClause ?(): string;
}
export class Grid<T> implements PageBean {
    getOrderByClause(): string {
        const orderby = this.sorts.map( (fn) => {
          return this.columns.find( (col: Column) => {
                 return fn === col.name;
           } );
        }).map( (col: Column) => {
            const sort = col.sort.startsWith('asc') ? 'asc' : 'desc' ;
            return col.name + '||' + sort;
        }).join('|||');
        return  encodeURI(orderby);
    }
    title = '';
    pageIndex = 0; // 页码
    pageSize = 10; // 每页几行数据
    data: T[] = [];
    total = 0;
    queryParams = '';   
    pages = 0; // 总页数
    size  = 0;  // 当前页数据条数
    sorts?: any[] = [];
    loading = false;
    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;
    }
}