chooseList.jsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. // console.log("2222", this.props);
  34. const options = [];
  35. const defaultValue = [];
  36. this.props.columns.forEach(item => {
  37. const obj = {
  38. label: item.title,
  39. value: item.title
  40. };
  41. // const all = {
  42. // label: "全选",
  43. // value: "全选"
  44. // }
  45. options.push(obj);
  46. // options.unshift(all)
  47. defaultValue.push(item.title);
  48. // defaultValue.unshift(item.label);
  49. });
  50. this.setState({
  51. columns: this.props.columns,
  52. options: options,
  53. defaultValue,
  54. checkedValues: defaultValue,
  55. flag: true
  56. });
  57. }
  58. onChange(checkedValues) {
  59. this.setState({
  60. checkedValues
  61. });
  62. // console.log("checked = ", this.state.checkedValues);
  63. let flag = false
  64. // checkedValues.forEach((v) => {
  65. // this.props.columns.forEach((i) => {
  66. // if( v != i.title ) {
  67. // flag = false
  68. // return
  69. // }
  70. // flag = true;
  71. // })
  72. // })
  73. if(checkedValues.length == this.props.columns.length) {
  74. flag = true
  75. }
  76. this.setState({
  77. flag
  78. })
  79. }
  80. showModal() {
  81. this.setState({
  82. visible: true
  83. });
  84. if(this.props.changeList != undefined) {
  85. const arr = []
  86. this.props.changeList.forEach((i) => {
  87. arr.push(i.title)
  88. })
  89. let flag = false
  90. if(arr.length == this.state.columns.length) {
  91. flag = true
  92. }
  93. this.setState({
  94. checkedValues: arr,
  95. flag
  96. })
  97. }else {
  98. const arr = [];
  99. this.props.columns.forEach(i => {
  100. arr.push(i.title);
  101. });
  102. let flag = false;
  103. if (arr.length == this.state.columns.length) {
  104. flag = true;
  105. }
  106. this.setState({
  107. checkedValues: arr,
  108. flag
  109. });
  110. }
  111. }
  112. handleOk() {
  113. this.setState({
  114. confirmLoading: true
  115. });
  116. setTimeout(() => {
  117. this.setState({
  118. visible: false,
  119. confirmLoading: false
  120. });
  121. this.props.changeFn(this.state.checkedValues);
  122. }, 0);
  123. }
  124. handleCancel() {
  125. // console.log("Clicked cancel button");
  126. // this.load()
  127. this.setState({
  128. visible: false
  129. });
  130. }
  131. allChange() {
  132. // console.log(123);
  133. if (this.state.flag) {
  134. this.setState({
  135. checkedValues: []
  136. });
  137. }else {
  138. this.setState({
  139. checkedValues: this.state.defaultValue
  140. });
  141. }
  142. this.setState({
  143. flag: !this.state.flag
  144. })
  145. }
  146. render() {
  147. const { visible, confirmLoading, columns } = this.state;
  148. return (
  149. // style={{ position: "absolute", right: "187px", top: this.props.top }}
  150. <div style={{ display: this.props.display }}>
  151. <Button type="primary" onClick={this.showModal} style={{ marginBottom: 10, marginTop: this.props.margin }}>
  152. 更改表格显示数据
  153. </Button>
  154. <Modal
  155. maskClosable={false}
  156. title="更改表格显示数据"
  157. visible={visible}
  158. onOk={this.handleOk}
  159. confirmLoading={confirmLoading}
  160. onCancel={this.handleCancel}
  161. >
  162. <div>
  163. <Checkbox onChange={this.allChange} checked={this.state.flag}>
  164. 全选
  165. </Checkbox>
  166. <Checkbox.Group
  167. options={this.state.options}
  168. defaultValue={this.state.defaultValue}
  169. onChange={this.onChange}
  170. className={"chooseList"}
  171. value={this.state.checkedValues}
  172. />
  173. </div>
  174. </Modal>
  175. </div>
  176. );
  177. }
  178. }
  179. export { ChooseList };