From f01d6b7e6bf8132524c1c2821fdcbbc5ca548369 Mon Sep 17 00:00:00 2001
From: fengxiang <110431245@qq.com>
Date: Fri, 29 Dec 2017 16:57:22 +0800
Subject: [PATCH] 设备型号完成

---
 src/app/routes/devices/version/version.service.ts                     |    4 +-
 src/app/routes/devices/version/add-or-edit/add-or-edit.component.html |    6 +-
 src/app/core/entity/grid.ts                                           |   22 +++++++++--
 src/app/routes/devices/version/add-or-edit/add-or-edit.component.ts   |   16 +++++++
 src/app/core/enum/types.enum.ts                                       |    2 
 src/app/routes/devices/version/version.component.html                 |    6 +-
 src/app/shared/shared.module.ts                                       |    1 
 src/app/routes/devices/version/version.component.ts                   |   53 +++++++++++++++-----------
 8 files changed, 73 insertions(+), 37 deletions(-)

diff --git a/src/app/core/entity/grid.ts b/src/app/core/entity/grid.ts
index a66711d..4567923 100644
--- a/src/app/core/entity/grid.ts
+++ b/src/app/core/entity/grid.ts
@@ -1,10 +1,11 @@
-import { Type } from '@angular/core';
+import { Column } from '@core/entity/grid';
 import { Types } from '@core/enum/types.enum';
 export interface Column {
    text?: string;
    name?: string;
    width?: string;
-   sort?: boolean;
+   sort?: string;
+   isSort?: boolean;
    type?: DataType;
 }
 export interface DataType {
@@ -18,8 +19,21 @@
     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; // ������������������
@@ -27,14 +41,14 @@
     total = 0;
     queryParams = '';   
     pages = 0; // ���������
-    queryMap?: any = {};
     size  = 0;  // ���������������������
     sorts?: any[] = [];
+    loading = false;
     indeterminate = false;
     allChecked = false;
     selectedIndexs?: number[] = []; // ������������������
     columns?: Column[] = [];    
-    
+
     checkAll(value: boolean) {
         this.data.forEach(
             row => {row['checked'] = value; }
diff --git a/src/app/core/enum/types.enum.ts b/src/app/core/enum/types.enum.ts
index e030afe..3fc5a08 100644
--- a/src/app/core/enum/types.enum.ts
+++ b/src/app/core/enum/types.enum.ts
@@ -1,3 +1,3 @@
 export enum Types {
-    Date, json
+    Date, Json
 }
diff --git a/src/app/routes/devices/version/add-or-edit/add-or-edit.component.html b/src/app/routes/devices/version/add-or-edit/add-or-edit.component.html
index fb9bb87..7f8f399 100644
--- a/src/app/routes/devices/version/add-or-edit/add-or-edit.component.html
+++ b/src/app/routes/devices/version/add-or-edit/add-or-edit.component.html
@@ -6,7 +6,7 @@
     <div nz-form-label nz-col [nzSm]="4" [nzXs]="24">
           <label nz-form-item-required>{{ cols.name.text }}</label>
     </div>
-    <div nz-form-control nz-col [nzSpan]="6" nzHasFeedback>
+    <div nz-form-control nz-col [nzSpan]="7" nzHasFeedback>
       <input nz-input formControlName="{{ cols.name.name }}" maxlength="20" />
     </div>
     <div nz-form-label nz-col [nzSpan]="4">
@@ -23,8 +23,8 @@
       <div nz-form-label nz-col [nzSm]="4" [nzXs]="24">
             <label nz-form-item-required>{{ cols.createTime.text }}</label>
       </div>
-      <div nz-form-control nz-col [nzSpan]="6" nzHasFeedback>
-        <nz-datepicker   formControlName="{{ cols.createTime.name }}" nzShowTime  [nzPlaceHolder]="'������������'"
+      <div nz-form-control nz-col [nzSpan]="7" nzHasFeedback>
+        <nz-datepicker nzSize="large"  style="width: 90%;" formControlName="{{ cols.createTime.name }}" nzShowTime  [nzPlaceHolder]="'������������'"
         [nzFormat]="'YYYY-MM-DD HH:mm:ss'" ></nz-datepicker>
       </div>
       <div nz-form-label nz-col [nzSpan]="4">
diff --git a/src/app/routes/devices/version/add-or-edit/add-or-edit.component.ts b/src/app/routes/devices/version/add-or-edit/add-or-edit.component.ts
index b875876..4e61d6d 100644
--- a/src/app/routes/devices/version/add-or-edit/add-or-edit.component.ts
+++ b/src/app/routes/devices/version/add-or-edit/add-or-edit.component.ts
@@ -42,8 +42,22 @@
         for (const i in this.validateForm.controls) {
           this.validateForm.controls[ i ].disable();
         } 
+       const isModified =  Object.keys(value).some(
+          (key: string) => {
+              return this.data[key] !== value[key];    
+          } 
+        );
+        // ������������
+        if (!isModified) { 
+          this.close();
+          return;
+        }
         this.isSaving = true;
-        this.data = value;
+        Object.keys(this.data).forEach( (key: string) => {
+              if ( value[key] != null ) {
+                   this.data[key] = value[key];
+              }
+        } );
         this.subject.next( this );
       }else {
           for (const i in this.validateForm.controls) {
diff --git a/src/app/routes/devices/version/version.component.html b/src/app/routes/devices/version/version.component.html
index 69d1282..ba633d3 100644
--- a/src/app/routes/devices/version/version.component.html
+++ b/src/app/routes/devices/version/version.component.html
@@ -22,10 +22,9 @@
               [nzTotal]="grid.total"
               [(nzPageIndex)]="grid.pageIndex"
               [(nzPageSize)]="grid.pageSize"
-              [nzLoading]="versionService.loading"
+              [nzLoading]="grid.loading"
               [nzShowTotal]="true"
-              (nzPageIndexChange)="load()"
-              (nzDataChange)="dataChange($event)">
+              (nzPageIndexChange)="load()">
               <thead nz-thead>
                   <tr>
                     <th nz-th [nzCheckbox]="true">
@@ -34,6 +33,7 @@
                     <th nz-th *ngFor="let col of grid.columns"
                      [ngStyle]="{'width':col.width,'text-align':col['align'] === undefined?'left':col.align}" >
                            <span>{{ col.text }}</span>
+                           <nz-table-sort *ngIf="col.isSort"   [(nzValue)]="col.sort" (nzValueChange)="sort(col.name,$event)"></nz-table-sort>
                     </th>
                     <th nz-th><span>������</span></th>
                   </tr>
diff --git a/src/app/routes/devices/version/version.component.ts b/src/app/routes/devices/version/version.component.ts
index b6f9849..d8fcc2d 100644
--- a/src/app/routes/devices/version/version.component.ts
+++ b/src/app/routes/devices/version/version.component.ts
@@ -12,6 +12,7 @@
 import { VersionService } from 'app/routes/devices/version/version.service';
 import { Types } from '@core/enum/types.enum';
 import { Column, Grid, PageBean } from '@core/entity/grid';
+import { filter } from 'rxjs/operators/filter';
 
 export interface DeviceVersion {
   createTime?: Column|any;
@@ -38,7 +39,8 @@
        },
        version: {
         text: '������',
-        width: '18%'
+        width: '18%',
+        isSort: true
       },
       createTime: {
           text: '������������',
@@ -46,7 +48,8 @@
           type: {
             name: Types.Date,
             format: 'YYYY-MM-DD HH:mm:ss'
-          }
+          },
+          isSort: true
       },
       description: {
           text: '������',
@@ -77,26 +80,28 @@
   queryTextChanged($event) {
       this.queryTextStream.next(this.queryMap.value);
   }
-  load(reload: boolean = false) {
+  load(reload: boolean = false) {   
     if (reload) {
       this.grid.pageIndex = 1 ;
     }
+    // ������������������ExpressionChangedAfterItHasBeenCheckedError
+    setTimeout(() => {
+      this.grid.loading = true;
+     }, 1);
     this.versionService.getPagingList(this.grid, this.queryMap.value).subscribe(
        (res: PageBean) => {
+            this.grid.loading = true;
             if (res != null && res.data != null) {
               this.grid.initData(res);
               this.grid.refreshStatus();
+              setTimeout(() => {
+                this.grid.loading = false;
+               }, 1);
             }
        }
     );
   }
 
-  edit() {
-    // this.modalHelper.static(VersionEditComponent, { user }).subscribe(() => {
-    //   this.load(true);
-    //   this.msgSrv.success('���������������������������');
-    // });
-  }
 // rowData���null���������������
   addOrModify(data) {
     if ( data == null) {
@@ -108,7 +113,7 @@
       this.versionService.save(ret.data).subscribe(
          ( res: any) => {
             if (res.code === 1) {
-              this.load(true);
+              this.load();
               ret.close();
               this.msgSrv.success('���������������������������');
             }
@@ -121,7 +126,7 @@
     this.versionService.delete( ...id ).subscribe(
       ( res: any) => {
          if (res.code === 1) {
-           this.load(true);
+           this.load();
            this.msgSrv.success('���������������������������');
          }
       }
@@ -138,24 +143,26 @@
        if (this.grid.selectedIndexs != null && this.grid.selectedIndexs.length > 0) {
           const ids = this.grid.selectedIndexs.map(
               (index: number) => {
-                  return Number.parseInt(this.grid.data[index].id);
+                  const id = this.grid.data[index].id;
+                  return Number.parseInt(id);
               }
           );
           this.delete( ...ids );
        }
     });
   }
-
-
-  dataChange(res: any) {
-
-  }
-
-  sort(field: string, value: any) {
-    // this.sortMap = {};
-    // this.sortMap[field] = value;
-    // this.q.sorter = value ? `${field}_${value}` : '';
-    // this.load(true);
+  sort(field: string, value: string) {
+    // ������������field
+    this.grid.sorts = this.grid.sorts.filter(
+       (fn: string) => {
+          return fn !== field;
+       }
+     );
+     // ������value������null������������������������������filed
+     if ( value != null ) {
+        this.grid.sorts.push(field);
+     }
+     this.load();
   }
 
   reset(ls: any[]) {
diff --git a/src/app/routes/devices/version/version.service.ts b/src/app/routes/devices/version/version.service.ts
index 4b14382..d0e65eb 100644
--- a/src/app/routes/devices/version/version.service.ts
+++ b/src/app/routes/devices/version/version.service.ts
@@ -15,7 +15,6 @@
       save: environment.SERVER_BASH_URL + '/device-version/add-or-modify',
       delete: environment.SERVER_BASH_URL + '/device-version/delete-by-ids'
   };
-  public loading = this.http.loading;
   constructor(private http: _HttpClient) { }
    public getPagingList(page: PageBean, queryText: string): Observable<PageBean> {
     const example = new ExampleService();
@@ -23,7 +22,8 @@
       example.or().andLike({name: 'name', value: '%' + queryText + '%'});
       example.or().andEqualTo({name: 'version', value: queryText});
     }
-    const param: PageBean = {pageSize: page.pageSize, pageIndex: page.pageIndex, queryParams: example.getSqlParam()};
+    const param: PageBean = {pageSize: page.pageSize, pageIndex: page.pageIndex, 
+      queryParams: example.getSqlParam(), orderByClause: page.getOrderByClause()};
     return this.http.get(this.urls.edit, param);
   }
   public save(data: any): Observable<any> {
diff --git a/src/app/shared/shared.module.ts b/src/app/shared/shared.module.ts
index d2f88eb..7bf1fa4 100644
--- a/src/app/shared/shared.module.ts
+++ b/src/app/shared/shared.module.ts
@@ -27,6 +27,7 @@
 import 'rxjs/add/operator/map';
 import 'rxjs/add/operator/switchMap';
 import 'rxjs/add/operator/toPromise';
+import 'rxjs/add/operator/filter';
 
 import {
     // LoggerModule,

--
Gitblit v1.8.0