Advertisement
Guest User

Untitled

a guest
Mar 14th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class TableHeaderWithSorting extends React.Component {
  2.     createSortHandler = property => event => {
  3.         this.props.onRequestSort(event, property);
  4.     };
  5.  
  6.     render() {
  7.         const {order, orderBy, columnData} = this.props;
  8.  
  9.         return (
  10.             <TableHead>
  11.                 <TableRow>
  12.                     {columnData.map(column => {
  13.                         return (
  14.                             <TableCell
  15.                                 key={column.id}
  16.                                 numeric={column.numeric}
  17.                                 padding={column.disablePadding ? 'none' : 'default'}
  18.                                 sortDirection={orderBy === column.id ? order : false}
  19.                             >
  20.                                 <Tooltip
  21.                                     title="Отсортировать"
  22.                                     placement={column.numeric ? 'bottom-end' : 'bottom-start'}
  23.                                     enterDelay={300}
  24.                                 >
  25.                                     <TableSortLabel
  26.                                         active={orderBy === column.id}
  27.                                         direction={order}
  28.                                         onClick={this.createSortHandler(column.id)}
  29.                                     >
  30.                                         {column.label}
  31.                                     </TableSortLabel>
  32.                                 </Tooltip>
  33.                             </TableCell>
  34.                         );
  35.                     }, this)}
  36.                 </TableRow>
  37.             </TableHead>
  38.         );
  39.     }
  40. }
  41.  
  42. class OrganizationsTable extends React.Component {
  43.  
  44.     constructor(props) {
  45.         super(props);
  46.  
  47.         this.state = {
  48.             dataSource: [],
  49.             columnData: [{id: 'bin', numeric: true, disablePadding: false, label: 'БИН'},
  50.                 {id: 'fullName', numeric: false, disablePadding: false, label: 'Полное название'},
  51.                 {id: 'shortName', numeric: false, disablePadding: false, label: 'Сокращённое название'},
  52.                 {id: 'organizationType', numeric: false, disablePadding: false, label: 'Тип'},
  53.                 {id: 'registrationDate', numeric: false, disablePadding: false, label: 'Дата регистрации'},
  54.                 {id: 'primaryEconomicActivity', numeric: false, disablePadding: false, label: 'Основная деятельность'},
  55.                 {id: 'founder', numeric: false, disablePadding: false, label: 'Учредитель'},
  56.                 {id: 'address', numeric: false, disablePadding: false, label: 'Адрес'},
  57.                 {id: 'phoneNumber', numeric: false, disablePadding: false, label: 'Номер телефона'},
  58.                 {id: 'taxesCommittee', numeric: false, disablePadding: false, label: 'Налоговый комитет'}],
  59.             order: 'asc',
  60.             orderBy: 'fullName',
  61.             page: 0
  62.         }
  63.     }
  64.  
  65.     componentDidMount() {
  66.         this.fetchData(this.state.page);
  67.     }
  68.  
  69.     handleRequestSort = (event, property) => {
  70.         const orderBy = property;
  71.         let order = 'desc';
  72.  
  73.         if (this.state.orderBy === property && this.state.order === 'desc') {
  74.             order = 'asc';
  75.         }
  76.  
  77.         const dataSource =
  78.             order === 'desc'
  79.                 ? this.state.dataSource.sort((a, b) => (b[orderBy] < a[orderBy] ? -1 : 1))
  80.                 : this.state.dataSource.sort((a, b) => (a[orderBy] < b[orderBy] ? -1 : 1));
  81.  
  82.         this.setState({ dataSource, order, orderBy });
  83.     };
  84.  
  85.     fetchData(page) {
  86.         Api.getOrganizations(page).then(response => {
  87.             let dataSource = [];
  88.             response.data.forEach(element => {
  89.                 dataSource.push(element);
  90.             });
  91.             this.setState({dataSource: dataSource});
  92.         })
  93.     }
  94.  
  95.     render() {
  96.         const { dataSource, order, orderBy, page, columnData} = this.state;
  97.         let paperStyle = {
  98.             width: '100%',
  99.             overflowX: 'auto',
  100.         };
  101.  
  102.         return <Paper style={paperStyle}>
  103.             <div>
  104.                 <Table>
  105.                     <TableHeaderWithSorting order={order}
  106.                                             orderBy={orderBy}
  107.                                             columnData={columnData}
  108.                                             onRequestSort={this.handleRequestSort}
  109.                     />
  110.                     <TableBody>
  111.                         {dataSource.map(organization => {
  112.                             return (
  113.                                 <TableRow key={organization.bin}>
  114.                                     <TableCell numeric>{organization.bin}</TableCell>
  115.                                     <TableCell>{organization.fullName}</TableCell>
  116.                                     <TableCell>{organization.shortName !== null ? organization.shortName : '-'}</TableCell>
  117.                                     <TableCell>{organization.organizationType.name}</TableCell>
  118.                                     <TableCell>{organization.registrationDate}</TableCell>
  119.                                     <TableCell>{organization.primaryEconomicActivity.name}</TableCell>
  120.                                     <TableCell>{organization.founder}</TableCell>
  121.                                     <TableCell>{organization.address}</TableCell>
  122.                                     <TableCell>{organization.phoneNumber}</TableCell>
  123.                                     <TableCell>{organization.taxesCommittee.name}</TableCell>
  124.                                 </TableRow>
  125.                             )
  126.                         })}
  127.                     </TableBody>
  128.                 </Table>
  129.             </div>
  130.         </Paper>
  131.     }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement