This commit is contained in:
insanity 2017-07-27 20:27:54 +09:00
commit 09226de460

View File

@ -16,15 +16,50 @@ export type Props = StateProps & DispatchProps;
export interface State { export interface State {
column: string; column: string;
data: any; 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 = [ const tableData = [
{ 'name': 'John', 'age': 15, 'gender': 'Male' }, {
{ 'name': 'Amber', 'age': 40, 'gender': 'Female' }, 'id': '11',
{ 'name': 'Leslie', 'age': 25, 'gender': 'Female' },
{ 'name': 'Ben', 'age': 70, 'gender': 'Male' }, '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<Props, State> { export class TableSort extends React.Component<Props, State> {
@ -45,29 +80,75 @@ export class TableSort extends React.Component<Props, State> {
{this.generationHeaderRow()} {this.generationHeaderRow()}
</Table.Header> </Table.Header>
<Table.Body> <Table.Body>
{this.generationBodyRow()}
</Table.Body> </Table.Body>
</Table> </Table>
); );
} }
private generationHeaderRow(): JSX.Element { private getHeaderValue = ():Map<string, string> => {
let tem:any = this.state.data;
let column:Map<string, string> = 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 ( return (
<Table.Row> <Table.Row>
{
this.renderHeaderCells()
}
</Table.Row> </Table.Row>
); );
} }
private generationBodyRow(): JSX.Element { private renderHeaderCells = ():JSX.Element[] => {
return ( const { column, data, direction } = this.state;
<Table.Row> let te = this.getHeaderValue();
let arr = new Array();
te.forEach((value:string, key:string ) => {
</Table.Row> arr.push(
); <Table.HeaderCell
sorted={column === value ? direction : 'ascending'}
onClick={this.handleSort(value)} >{value}</Table.HeaderCell> );
});
return arr;
} }
private handleSort(clickedColumn:any):void { private generationBodyRow(): JSX.Element[] {
let arr = new Array<JSX.Element>();
let idx:number = 0;
for(let temp of this.state.data) {
arr.push( <Table.Row key={idx++}> { this.renderBodyCells(temp) } </Table.Row> );
}
return arr;
}
private renderBodyCells = (temp:any): JSX.Element[] => {
let te = this.getHeaderValue();
let arr = new Array();
te.forEach((value:string, key:string ) => {
arr.push(<Table.Cell>{temp[key]}</Table.Cell>);
});
return arr;
}
private handleSort = (clickedColumn:string) => ():void => {
const {column, data, direction} = this.state; const {column, data, direction} = this.state;
if (column !== clickedColumn) { if (column !== clickedColumn) {
@ -85,19 +166,5 @@ export class TableSort extends React.Component<Props, State> {
direction: direction === 'ascending' ? 'descending' : 'ascending', 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;
}
} }