index.jsx 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. import React, { Component } from "react";
  2. import { Button, message, Checkbox, Collapse, } from "antd";
  3. import $ from "jquery/src/ajax";
  4. import "./index.less";
  5. /**
  6. * 部门多选组件 2022-07-12
  7. * <Cascaders
  8. * ref={ref => this.Cascaders = ref}
  9. * placeholder="请选择公出部门"
  10. * id="id"
  11. * name="name"
  12. * children="list"
  13. * height={28}
  14. * onSel={(e) => {}}
  15. * />
  16. */
  17. class Cascaders extends Component {
  18. constructor(props) {
  19. super(props);
  20. this.state = {
  21. visible: false, // 弹窗开关
  22. authTree: [], // 列表显示数据
  23. checkedList: [], // 选中数据
  24. isFous: true, // 是否在组件外
  25. };
  26. this.itemHandle = this.itemHandle.bind(this);
  27. this.onSelect = this.onSelect.bind(this);
  28. this.onOks = this.onOks.bind(this);
  29. this.onCancel = this.onCancel.bind(this);
  30. }
  31. componentDidMount() {
  32. // 后端返回的数据,转成前端要用的数据保存页面使用
  33. // const authTree = [...params.authTree];
  34. // this.setState({
  35. // authTree
  36. // })
  37. }
  38. // 转成前端要用的数据
  39. authTreeFun() {
  40. const checkedList = this.state.checkedList;
  41. checkedList.forEach((item) => {
  42. this.itemHandle(item);
  43. });
  44. }
  45. // 获取部门数据
  46. selectSuperId() {
  47. $.ajax({
  48. method: "get",
  49. dataType: "json",
  50. url: globalConfig.context + "/api/admin/organization/getAllDep",
  51. data: {},
  52. success: function (data) {
  53. if (data.error && data.error.length === 0) {
  54. localStorage.setItem("departmentData", JSON.stringify(data.data));
  55. this.setState({
  56. visible: true,
  57. authTree: data.data || []
  58. }, () => {
  59. this.authTreeFun();
  60. })
  61. } else {
  62. // message.warning(data.error[0].message);
  63. }
  64. }.bind(this),
  65. }).always(
  66. );
  67. }
  68. // 点击选择部门
  69. onSelect() {
  70. // 缓存的后端返回数据,转成前端要用的数据保存页面使用
  71. const list = JSON.parse(localStorage.getItem("departmentData")) || []
  72. const authTree = list
  73. if (authTree.length > 0) {
  74. this.setState({
  75. visible: !this.state.visible,
  76. authTree: authTree,
  77. }, () => {
  78. this.authTreeFun();
  79. })
  80. } else {
  81. //若缓存清理则重新请求接口拿
  82. this.selectSuperId()
  83. }
  84. }
  85. // 确定
  86. onOks() {
  87. const { authTree } = this.state
  88. const { id = "id", children = "list", onSel } = this.props
  89. let list = []
  90. authTree.forEach((its) => {
  91. if (its.checked) {
  92. list.push(its[id])
  93. }
  94. if (!!its[children] && its[children].length > 0) {
  95. its[children].forEach((itl) => {
  96. if (itl.checked) {
  97. list.push(itl[id])
  98. }
  99. });
  100. }
  101. })
  102. if (list.length === 0) {
  103. message.warn("还没有选择的部门")
  104. return
  105. }
  106. this.setState({
  107. checkedList: list,
  108. visible: false,
  109. isFous: true,
  110. })
  111. onSel(list)
  112. }
  113. // 取消
  114. onCancel() {
  115. this.setState({
  116. visible: false,
  117. isFous: true,
  118. })
  119. }
  120. // 代码二级联动
  121. itemHandle(iD, checked = true) {
  122. const { id = "id", children = "list" } = this.props
  123. const { authTree } = this.state
  124. authTree.forEach((item1) => {
  125. if (item1[id] == iD) {
  126. item1.checked = checked;
  127. if (!!item1[children] && item1[children].length > 0) {
  128. item1[children].forEach((item2) => {
  129. item2.checked = checked;
  130. });
  131. }
  132. } else {
  133. if (!!item1[children] && item1[children].length > 0) {
  134. // 反向思路: 保存子选项框的个数, 子选项选中就-1, 等于0说明都选中了, 父选项就勾上
  135. let temp1 = item1[children].length;
  136. item1[children].forEach((item2) => {
  137. if (item2[id] == iD) {
  138. item2.checked = checked;
  139. }
  140. // 选中-1, 未选中不动
  141. item2.checked ? temp1 -= 1 : temp1;
  142. });
  143. // 长度是0, 父选项框就勾选
  144. temp1 === 0 ? item1.checked = true : item1.checked = false;
  145. } else {
  146. }
  147. }
  148. });
  149. this.setState({
  150. authTree,
  151. });
  152. }
  153. // 清空 现用于父级重置
  154. empty() {
  155. this.setState({
  156. visible: false,
  157. isFous: true,
  158. checkedList: []
  159. })
  160. }
  161. // checked组件样式
  162. inde(item) {
  163. const { children = "list" } = this.props
  164. if (item.checked) {
  165. return false
  166. } else {
  167. if (!!item[children] && item[children].length > 0) {
  168. let clist = []
  169. for (var i = 0; i < item[children].length; i++) {
  170. if (item[children][i].checked == true) {
  171. clist.push(item[children][i])
  172. }
  173. }
  174. if (!!clist.length && (clist.length < item[children].length)) {
  175. return true
  176. } else {
  177. return false
  178. }
  179. } else {
  180. return false
  181. }
  182. }
  183. }
  184. render() {
  185. const { Panel } = Collapse;
  186. const { visible, isFous, authTree, checkedList } = this.state
  187. const {
  188. placeholder = "选择部门",
  189. width = 200,
  190. height = 32,
  191. id = "id",//接口需要的字段名
  192. name = "name",//显示内容字段名
  193. children = "list",//二级列表字段名
  194. } = this.props
  195. const authTreeMap = authTree.map((item1) => (
  196. // Panel是手风琴
  197. <Panel
  198. key={item1[id]}
  199. header={(
  200. <div className="item">
  201. {/* 一级复选框(向下操控二级) */}
  202. <Checkbox
  203. indeterminate={this.inde(item1)}
  204. name={item1[id]}
  205. checked={item1.checked}
  206. onClick={(e) => {
  207. e.stopPropagation();
  208. this.itemHandle(e.target.name, e.target.checked);
  209. }}
  210. >
  211. </Checkbox>
  212. <div className="iname">{item1[name]}</div>
  213. </div>
  214. )}
  215. >
  216. {/* 二级复选框(向上联动一级) */}
  217. {!!item1[children] && item1[children].map((item2) => (
  218. <div key={item2[id]} className="item">
  219. <Checkbox
  220. name={item2[id]}
  221. checked={item2.checked}
  222. onClick={(e) => {
  223. this.itemHandle(e.target.name, e.target.checked);
  224. }}
  225. >
  226. </Checkbox>
  227. <div className="iname" style={{ width: "90%" }}>{item2[name]}</div>
  228. </div>
  229. ))}
  230. </Panel>
  231. ));
  232. return (
  233. <div
  234. className="cascaders"
  235. tabIndex={0}
  236. onBlur={(e) => {
  237. isFous && this.setState({
  238. visible: false
  239. })
  240. }}
  241. >
  242. <div
  243. className="cinput"
  244. style={{
  245. height: height,
  246. width: width,
  247. borderColor: visible && "#80b9ff",
  248. color: checkedList.length > 0
  249. ? "rgba(0, 0, 0, 0.65)"
  250. : "rgba(191, 191, 191, 1)"
  251. }}
  252. onClick={this.onSelect}
  253. >
  254. <div className="ctext">
  255. {checkedList.length > 0 ? `已选择${checkedList.length}项` : placeholder}
  256. </div>
  257. </div>
  258. <div
  259. data-reactroot
  260. onMouseLeave={() => {
  261. this.setState({
  262. isFous: true
  263. })
  264. }}
  265. onMouseEnter={() => {
  266. this.setState({
  267. isFous: false
  268. })
  269. }}
  270. >
  271. {
  272. visible &&
  273. <div className="cpop" style={{ width: 260, }}>
  274. <div className="clist">
  275. <Collapse expandIconPosition="right">
  276. {authTreeMap}
  277. </Collapse>
  278. </div>
  279. <div className="cboot">
  280. <Button type="primary" style={{ marginLeft: 10 }} onClick={this.onOks}>确定</Button>
  281. <Button onClick={this.onCancel}>取消</Button>
  282. </div>
  283. </div>
  284. }
  285. </div>
  286. </div>
  287. );
  288. }
  289. }
  290. export default Cascaders;