index.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import React, { Component } from 'react';
  2. import './index.less';
  3. import Select from '../Select';
  4. export default class AnswerTable extends Component {
  5. constructor(props) {
  6. super(props);
  7. this.state = { data: this.props.data, index: -1 };
  8. }
  9. componentWillMount() { }
  10. componentWillUnmount() { }
  11. resort(index) {
  12. const { data } = this.state;
  13. let number = true;
  14. const ii = data.map((row, i) => {
  15. if (`${Number(row[index])}` !== row[index]) number = false;
  16. return { value: row[index], index: i };
  17. }).sort((x, y) => {
  18. if (x.value === y.value) return 0;
  19. return number ? Number(x.value) - Number(y.value) : x.value > y.value ? 1 : -1;
  20. });
  21. this.setState({
  22. index,
  23. data: ii.map((row) => {
  24. return data[row.index];
  25. }),
  26. });
  27. }
  28. render() {
  29. const { columns = [], list = [] } = this.props;
  30. const { data = [], index } = this.state;
  31. return (
  32. <div className="answer-table">
  33. <div className="select-line">
  34. <Select size="basic" theme="default" value={index} list={list} onChange={(item) => {
  35. this.resort(item.key);
  36. }} />
  37. </div>
  38. <table border="1" bordercolor="#d8d8d8">
  39. <thead>
  40. <tr className="bg">
  41. {columns.map(item => {
  42. return <th>{item.title}</th>;
  43. })}
  44. </tr>
  45. </thead>
  46. <tbody>
  47. {data.map(row => {
  48. return (
  49. <tr>
  50. {columns.map(item => {
  51. return <td>{row[item.key]}</td>;
  52. })}
  53. </tr>
  54. );
  55. })}
  56. </tbody>
  57. </table>
  58. </div>
  59. );
  60. }
  61. }