chooseList.jsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import React, { Component } from "react";
  2. import { Form, Upload, Modal, Button, message, Table, Input, Checkbox } from "antd";
  3. import "../../../../../css/base.less"
  4. class ChooseList extends Component {
  5. constructor(props) {
  6. super(props);
  7. this.state = {
  8. visible: false,
  9. confirmLoading: false,
  10. columns: [],
  11. // 复选框数组
  12. options: [],
  13. defaultValue: [],
  14. // 被选中的数组
  15. checkedValues: [],
  16. flag: false
  17. };
  18. this.showModal = this.showModal.bind(this);
  19. this.handleOk = this.handleOk.bind(this);
  20. this.handleCancel = this.handleCancel.bind(this);
  21. this.onChange = this.onChange.bind(this);
  22. this.allChange = this.allChange.bind(this);
  23. this.load = this.load.bind(this);
  24. }
  25. // componentWillReceiveProps(nextProp) {
  26. // this.setState({
  27. // })
  28. // }
  29. componentDidMount() {
  30. this.load()
  31. }
  32. load() {
  33. const options = [];
  34. const defaultValue = [];
  35. this.props.columns.forEach(item => {
  36. let obj = {
  37. label: item.title,
  38. value: item.title
  39. };
  40. options.push(obj);
  41. defaultValue.push(item.title);
  42. });
  43. this.setState({
  44. columns: this.props.columns,
  45. options: options,
  46. defaultValue,
  47. checkedValues: defaultValue,
  48. flag: true
  49. });
  50. }
  51. onChange(checkedValues) {
  52. this.setState({
  53. checkedValues
  54. });
  55. let flag = false
  56. // checkedValues.forEach((v) => {
  57. // this.props.columns.forEach((i) => {
  58. // if( v != i.title ) {
  59. // flag = false
  60. // return
  61. // }
  62. // flag = true;
  63. // })
  64. // })
  65. if (checkedValues.length == this.props.columns.length) {
  66. flag = true
  67. }
  68. this.setState({
  69. flag
  70. })
  71. }
  72. showModal() {
  73. this.load()
  74. this.setState({
  75. visible: true
  76. });
  77. if (this.props.changeList != undefined) {
  78. const arr = []
  79. this.props.changeList.forEach((i) => {
  80. arr.push(i.title)
  81. })
  82. let flag = false
  83. if (arr.length == this.state.columns.length) {
  84. flag = true
  85. }
  86. this.setState({
  87. checkedValues: arr,
  88. flag
  89. })
  90. } else {
  91. const arr = [];
  92. this.props.columns.forEach(i => {
  93. arr.push(i.title);
  94. });
  95. let flag = false;
  96. if (arr.length == this.state.columns.length) {
  97. flag = true;
  98. }
  99. this.setState({
  100. checkedValues: arr,
  101. flag
  102. });
  103. }
  104. }
  105. handleOk() {
  106. this.setState({
  107. confirmLoading: true
  108. });
  109. setTimeout(() => {
  110. this.setState({
  111. visible: false,
  112. confirmLoading: false
  113. });
  114. this.props.changeFn(this.state.checkedValues);
  115. }, 0);
  116. }
  117. handleCancel() {
  118. // this.load()
  119. this.setState({
  120. visible: false
  121. });
  122. }
  123. allChange() {
  124. if (this.state.flag) {
  125. this.setState({
  126. checkedValues: []
  127. });
  128. } else {
  129. this.setState({
  130. checkedValues: this.state.defaultValue
  131. });
  132. }
  133. this.setState({
  134. flag: !this.state.flag
  135. })
  136. }
  137. render() {
  138. const { visible, confirmLoading, columns } = this.state;
  139. return (
  140. // style={{ position: "absolute", right: "187px", top: this.props.top }}
  141. <div style={{ display: this.props.display }}>
  142. <Button type="primary" onClick={this.showModal} style={{ marginBottom: 10, marginTop: this.props.margin }}>
  143. 更改表格显示数据
  144. </Button>
  145. <Modal
  146. maskClosable={false}
  147. title="更改表格显示数据"
  148. visible={visible}
  149. onOk={this.handleOk}
  150. confirmLoading={confirmLoading}
  151. onCancel={this.handleCancel}
  152. >
  153. <div>
  154. <Checkbox onChange={this.allChange} checked={this.state.flag}>
  155. 全选
  156. </Checkbox>
  157. <Checkbox.Group
  158. options={this.state.options}
  159. defaultValue={this.state.defaultValue}
  160. onChange={this.onChange}
  161. className={"chooseList"}
  162. value={this.state.checkedValues}
  163. />
  164. </div>
  165. </Modal>
  166. </div>
  167. );
  168. }
  169. }
  170. export { ChooseList };