From 952c531482bb5c2d35ff158d106cd4594dec7061 Mon Sep 17 00:00:00 2001 From: geek Date: Thu, 27 Jul 2017 20:12:05 +0900 Subject: [PATCH] table sort --- .../temp/react/components/TableSort.tsx | 123 ++++++++++++++---- 1 file changed, 95 insertions(+), 28 deletions(-) diff --git a/src/ts/@overflow/temp/react/components/TableSort.tsx b/src/ts/@overflow/temp/react/components/TableSort.tsx index 5c9382e..d03f033 100644 --- a/src/ts/@overflow/temp/react/components/TableSort.tsx +++ b/src/ts/@overflow/temp/react/components/TableSort.tsx @@ -16,15 +16,50 @@ export type Props = StateProps & DispatchProps; export interface State { column: string; data: any; - direction: string; + direction: 'ascending' | 'descending'; } +// +// const tableData = [ +// { 'name': 'John', 'age': 15, 'gender': 'Male' }, +// { 'name': 'Amber', 'age': 40, 'gender': 'Female' }, +// { 'name': 'Leslie', 'age': 25, 'gender': 'Female' }, +// { 'name': 'Ben', 'age': 70, 'gender': 'Male' }, +// ]; const tableData = [ - { 'name': 'John', 'age': 15, 'gender': 'Male' }, - { 'name': 'Amber', 'age': 40, 'gender': 'Female' }, - { 'name': 'Leslie', 'age': 25, 'gender': 'Female' }, - { 'name': 'Ben', 'age': 70, 'gender': 'Male' }, + { + 'id': '11', + + 'cidr': '192.168.1.0/24', + 'displayName': '192.168.1.105\'s probe', + // 'infraHost': { + // 'ip': '192.168.1.105', + // 'mac': 'ab:cd:ef:gh:ij', + // }, + 'targetCount': '20', + 'sensorCount': '30', + 'probeKey': '1AGBLKDFJ2452ASDGFL2KWJLKSDJ', + 'description': 'description1111111111', + }, + { + 'id': '22', + 'displayName': '192.168.1.105\'s probe', + 'cidr': '192.168.1.0/24', + 'targetCount': '20', + 'sensorCount': '50', + 'probeKey': '1AGBLKDFJ2452ASDGFL2KWJLKSDJ', + 'description': 'description1111111111', + }, + { + 'id': '33', + 'cidr': '192.168.1.0/24', + 'displayName': '192.168.1.105\'s probe', + 'targetCount': '20', + 'sensorCount': '60', + 'probeKey': '1AGBLKDFJ2452ASDGFL2KWJLKSDJ', + 'description': 'description1111111111', + }, ]; export class TableSort extends React.Component { @@ -34,7 +69,7 @@ export class TableSort extends React.Component { this.state = { column: null, data: tableData, - direction: null, + direction: null, }; } @@ -45,29 +80,75 @@ export class TableSort extends React.Component { {this.generationHeaderRow()} - + {this.generationBodyRow()} ); } - private generationHeaderRow(): JSX.Element { + private getHeaderValue = ():Map => { + let tem:any = this.state.data; + let column:Map = new Map(); + for (let obj in tem) { + if (tem.hasOwnProperty(obj)) { + Object.keys(tem[obj]).map((key:string, index:number ) => { + if (tem[obj].hasOwnProperty(key)) { + column.set(key, key); + } + }); + } + } + + return column; + } + + private generationHeaderRow(): (JSX.Element | JSX.Element[]) { return ( + + { + this.renderHeaderCells() + } ); } - private generationBodyRow(): JSX.Element { - return ( - + private renderHeaderCells = ():JSX.Element[] => { + const { column, data, direction } = this.state; + let te = this.getHeaderValue(); + let arr = new Array(); + te.forEach((value:string, key:string ) => { - - ); + arr.push( + {value} ); + }); + + return arr; } - private handleSort(clickedColumn:any):void { + private generationBodyRow(): JSX.Element[] { + let arr = new Array(); + let idx:number = 0; + for(let temp of this.state.data) { + arr.push( { this.renderBodyCells(temp) } ); + } + return arr; + } + + private renderBodyCells = (temp:any): JSX.Element[] => { + let te = this.getHeaderValue(); + let arr = new Array(); + + te.forEach((value:string, key:string ) => { + + arr.push({temp[key]}); + }); + return arr; + } + private handleSort = (clickedColumn:string) => ():void => { const {column, data, direction} = this.state; if (column !== clickedColumn) { @@ -85,19 +166,5 @@ export class TableSort extends React.Component { direction: direction === 'ascending' ? 'descending' : 'ascending', }); } - - private handleDirection():string { - const { column, data, direction } = this.state; - - if (column === 'name') { - return direction; - } else if(column === 'age') { - return direction; - } else if (column === 'gender' ) { - return direction; - } - - return null; - } }