index.jsx 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. setDefault(list) {
  122. this.setState({
  123. checkedList: list
  124. })
  125. }
  126. // 代码二级联动
  127. itemHandle(iD, checked = true) {
  128. const { id = "id", children = "list" } = this.props
  129. const { authTree } = this.state
  130. authTree.forEach((item1) => {
  131. if (item1[id] == iD) {
  132. item1.checked = checked;
  133. if (!!item1[children] && item1[children].length > 0) {
  134. item1[children].forEach((item2) => {
  135. item2.checked = checked;
  136. });
  137. }
  138. } else {
  139. if (!!item1[children] && item1[children].length > 0) {
  140. // 反向思路: 保存子选项框的个数, 子选项选中就-1, 等于0说明都选中了, 父选项就勾上
  141. let temp1 = item1[children].length;
  142. item1[children].forEach((item2) => {
  143. if (item2[id] == iD) {
  144. item2.checked = checked;
  145. }
  146. // 选中-1, 未选中不动
  147. item2.checked ? temp1 -= 1 : temp1;
  148. });
  149. // 长度是0, 父选项框就勾选
  150. temp1 === 0 ? item1.checked = true : item1.checked = false;
  151. } else {
  152. }
  153. }
  154. });
  155. this.setState({
  156. authTree,
  157. });
  158. }
  159. // 清空 现用于父级重置
  160. empty() {
  161. this.setState({
  162. visible: false,
  163. isFous: true,
  164. checkedList: []
  165. })
  166. }
  167. // checked组件样式
  168. inde(item) {
  169. const { children = "list" } = this.props
  170. if (item.checked) {
  171. return false
  172. } else {
  173. if (!!item[children] && item[children].length > 0) {
  174. let clist = []
  175. for (var i = 0; i < item[children].length; i++) {
  176. if (item[children][i].checked == true) {
  177. clist.push(item[children][i])
  178. }
  179. }
  180. if (!!clist.length && (clist.length < item[children].length)) {
  181. return true
  182. } else {
  183. return false
  184. }
  185. } else {
  186. return false
  187. }
  188. }
  189. }
  190. render() {
  191. const { Panel } = Collapse;
  192. const { visible, isFous, authTree, checkedList } = this.state
  193. const {
  194. placeholder = "选择部门",
  195. width = 200,
  196. height = 32,
  197. id = "id",//接口需要的字段名
  198. name = "name",//显示内容字段名
  199. children = "list",//二级列表字段名
  200. } = this.props
  201. const authTreeMap = authTree.map((item1) => (
  202. // Panel是手风琴
  203. <Panel
  204. key={item1[id]}
  205. header={(
  206. <div className="item">
  207. {/* 一级复选框(向下操控二级) */}
  208. <Checkbox
  209. indeterminate={this.inde(item1)}
  210. name={item1[id]}
  211. checked={item1.checked}
  212. onClick={(e) => {
  213. e.stopPropagation();
  214. this.itemHandle(e.target.name, e.target.checked);
  215. }}
  216. >
  217. </Checkbox>
  218. <div className="iname">{item1[name]}</div>
  219. </div>
  220. )}
  221. >
  222. {/* 二级复选框(向上联动一级) */}
  223. {!!item1[children] && item1[children].map((item2) => (
  224. <div key={item2[id]} className="item">
  225. <Checkbox
  226. name={item2[id]}
  227. checked={item2.checked}
  228. onClick={(e) => {
  229. this.itemHandle(e.target.name, e.target.checked);
  230. }}
  231. >
  232. </Checkbox>
  233. <div className="iname" style={{ width: "90%" }}>{item2[name]}</div>
  234. </div>
  235. ))}
  236. </Panel>
  237. ));
  238. return (
  239. <div
  240. className="cascaders"
  241. tabIndex={0}
  242. onBlur={(e) => {
  243. isFous && this.setState({
  244. visible: false
  245. })
  246. }}
  247. >
  248. <div
  249. className="cinput"
  250. style={{
  251. height: height,
  252. width: width,
  253. borderColor: visible && "#80b9ff",
  254. color: checkedList.length > 0
  255. ? "rgba(0, 0, 0, 0.65)"
  256. : "rgba(191, 191, 191, 1)"
  257. }}
  258. onClick={this.onSelect}
  259. >
  260. <div className="ctext">
  261. {checkedList.length > 0 ? `已选择${checkedList.length}项` : placeholder}
  262. </div>
  263. </div>
  264. <div
  265. data-reactroot
  266. onMouseLeave={() => {
  267. this.setState({
  268. isFous: true
  269. })
  270. }}
  271. onMouseEnter={() => {
  272. this.setState({
  273. isFous: false
  274. })
  275. }}
  276. >
  277. {
  278. visible &&
  279. <div className="cpop" style={{ width: 260, }}>
  280. <div className="clist">
  281. <Collapse expandIconPosition="right">
  282. {authTreeMap}
  283. </Collapse>
  284. </div>
  285. <div className="cboot">
  286. <Button type="primary" style={{ marginLeft: 10 }} onClick={this.onOks}>确定</Button>
  287. <Button onClick={this.onCancel}>取消</Button>
  288. </div>
  289. </div>
  290. }
  291. </div>
  292. </div>
  293. );
  294. }
  295. }
  296. export default Cascaders;