chooseList.jsx 4.2 KB

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