index.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. <template>
  2. <!-- 弹出框 -->
  3. <el-popover placement="bottom-start" :disabled="disabled" v-model="visible">
  4. <!-- tag盒子 -->
  5. <div slot="reference" class="tags-box" :class="{'tags-box-disabled':!disabled,'tags-box-mini':size === 'mini'}">
  6. <span v-show="sNodeList.length > 0" class="tags-num">{{sNodeList.length}}</span>
  7. <i v-if="sNodeList.length > 0 && !disabled" class="el-icon-circle-close close-icon" @click="clear"></i>
  8. <el-tag type="success" :size="size" v-for="v in sNodeList" :key="v.id">{{ v.shortName }}</el-tag>
  9. </div>
  10. <!-- 文本框 -->
  11. <el-input style="width: 100%; margin-bottom: 20px" v-model="queryForm.value" @input="serchTreeHandler"
  12. placeholder="请输入查询机构名称"></el-input>
  13. <!-- 下拉框 -->
  14. <el-row :gutter="20" type="flex" align="middle">
  15. <el-col :span="14" :xs="24">
  16. <el-select :disabled="disable" :popper-append-to-body="false" v-model="queryForm.type" placeholder="请选择过滤机构类型" clearable
  17. @change="serchTreeHandler">
  18. <el-option v-for="dict in dict.type.sys_org_type" :key="dict.value" :label="dict.label"
  19. :value="parseInt(dict.value)"></el-option>
  20. </el-select>
  21. </el-col>
  22. <el-col :span="10" :xs="24">
  23. <el-checkbox v-if="checkShow" :disabled="!queryForm.type" v-model="checked">按过滤机构类型勾选</el-checkbox>
  24. </el-col>
  25. </el-row>
  26. <!-- 机构树 -->
  27. <div class="tree-box">
  28. <el-tree ref="tree" :data="orgTree" :props="treeProps" node-key="id" show-checkbox :check-strictly="true"
  29. :expand-on-click-node="false" :default-checked-keys="nodes" :default-expanded-keys="nodes"
  30. @node-click="handleNodeClick" @check="onCheck" :filter-node-method="filterNode">
  31. </el-tree>
  32. </div>
  33. </el-popover>
  34. </template>
  35. <script>
  36. export default {
  37. dicts: ["sys_org_type"],
  38. props: {
  39. //默认选中的node
  40. defaultNode: {
  41. type: Array,
  42. default: () => [],
  43. required: false,
  44. },
  45. //行社机构
  46. hangsheTree: {
  47. type: Boolean,
  48. default: false,
  49. },
  50. defaultProps: {
  51. type: Object,
  52. default: () => {
  53. return {
  54. children: "children",
  55. label: "shortName",
  56. };
  57. },
  58. },
  59. //自定义请求
  60. customRequest: {
  61. type: Function,
  62. },
  63. size:{
  64. type: String,
  65. default: 'small',
  66. },
  67. //组件禁用
  68. disabled: {
  69. type: Boolean,
  70. default: false,
  71. required: false,
  72. },
  73. checkShow: {
  74. type: Boolean,
  75. default: false,
  76. },
  77. queryData: {
  78. type: Number,
  79. default: null,
  80. required: false,
  81. },
  82. // 机构类型筛选条件禁用
  83. disable: {
  84. type: Boolean,
  85. default: false,
  86. required: false,
  87. },
  88. // 机构树上能选择的机构类型
  89. enabledCheckOrgTypes: {
  90. type: [Number,Array],
  91. default: ()=>[],
  92. required: false,
  93. }
  94. },
  95. data() {
  96. return {
  97. //清除图标
  98. showClearable: false,
  99. //弹窗显示
  100. visible: false,
  101. //结构树
  102. orgTree: null,
  103. //过滤条件
  104. queryForm: {
  105. value: null,
  106. type: null,
  107. },
  108. //受否全选
  109. checked: false,
  110. //已选中的node
  111. sNodeList: [],
  112. //默认显示及展开的node
  113. nodes: [],
  114. treeProps:{
  115. disabled: this.getNodedisabled,
  116. children: "children",
  117. label: "shortName",
  118. }
  119. };
  120. },
  121. mounted() {
  122. this.getDeptTree();
  123. },
  124. watch: {
  125. defaultNode: {
  126. immediate: true,
  127. handler(n) {
  128. if(!n || n.length == 0){
  129. this.nodes = [];
  130. this.sNodeList = [];
  131. this.$nextTick(() => {
  132. this.$refs.tree.setCheckedKeys([]);
  133. });
  134. return
  135. }
  136. this.nodes = this.defaultNode;
  137. this.$nextTick(() => {
  138. let arr = [];
  139. this.defaultNode.forEach(v => {
  140. let node = this.$refs.tree.getNode(v);
  141. arr.push(node.data)
  142. })
  143. this.sNodeList = arr;
  144. });
  145. },
  146. },
  147. queryData: {
  148. handler(n) {
  149. if (n) {
  150. this.queryForm.type = parseInt(n);
  151. this.serchTreeHandler();
  152. }
  153. },
  154. // queryForm: {
  155. // deep: true,
  156. // handler(n, o) {
  157. // if(!n.value&&!n.type){
  158. // this.$refs.tree.filter({});
  159. // }else{
  160. // this.$refs.tree.filter(n);
  161. // }
  162. // },
  163. // },
  164. },
  165. defaultProps: {
  166. handler(n) {
  167. if (n) {
  168. this.treeProps.label=n.label;
  169. this.treeProps.children=n.children;
  170. }
  171. },
  172. }
  173. },
  174. methods: {
  175. clear() {
  176. this.queryForm.value=null;
  177. this.sNodeList = [];
  178. this.$refs.tree.setCheckedKeys([]);
  179. this.$refs.tree.setCurrentKey(null);
  180. this.nodes = [];
  181. this.$emit("selectNode",[]);
  182. this.$emit("selectNodeId", []);
  183. },
  184. //筛选条件变化
  185. serchTreeHandler() {
  186. this.$refs.tree.filter(this.queryForm);
  187. },
  188. /** 查询机构下拉树结构 */
  189. getDeptTree() {
  190. if (this.customRequest) {
  191. this.customRequest().then((response) => {
  192. this.treeList = response.data;
  193. return
  194. });
  195. }
  196. this.orgTree = this.$store.getters.orgTree;
  197. if (this.hangsheTree) {
  198. this.orgTree = this.$store.getters.depTree;
  199. }
  200. },
  201. getNodedisabled(node) {
  202. if (this.enabledCheckOrgTypes) {
  203. if (Array.isArray(this.enabledCheckOrgTypes)) {
  204. if (this.enabledCheckOrgTypes.length>0 && this.enabledCheckOrgTypes.findIndex((x) => x === node.type) == -1) {
  205. return true;
  206. }
  207. }
  208. else {
  209. if (this.enabledCheckOrgTypes !== node.type) {
  210. return true;
  211. }
  212. }
  213. }
  214. return false;
  215. },
  216. filterNode(value, data, node) {
  217. if (this.queryForm.value && this.queryForm.type) {
  218. return (
  219. data.name.indexOf(this.queryForm.value) !== -1 &&
  220. data.type == this.queryForm.type
  221. );
  222. }
  223. if (this.queryForm.value) {
  224. return data.name.indexOf(this.queryForm.value) !== -1;
  225. }
  226. if (this.queryForm.type) {
  227. return data.type == this.queryForm.type;
  228. }
  229. return true;
  230. },
  231. handleNodeClick() {
  232. },
  233. onCheck(data, checked, tree) {
  234. console.log(1111123213123)
  235. if (this.checked) {
  236. let checkNodes = this.$refs.tree.getCheckedNodes();
  237. let type = this.queryForm.type;
  238. let recursionFn = function (tree) {
  239. console.log(tree,'TREE')
  240. tree.forEach((item) => {
  241. if (type == item.type) {
  242. checkNodes.push(item);
  243. //console.log('name',checkNodes.indexOf(item))
  244. //checkNodes.indexOf(item) >= 0? checkNodes.splice(checkNodes.indexOf(item),1):checkNodes.push(item);
  245. }
  246. if (item.children?.length > 0) {
  247. recursionFn(item.children)
  248. } else {
  249. return;
  250. }
  251. });
  252. }
  253. // 递归查询
  254. recursionFn(data.children);
  255. let arr = checkNodes.filter(v => {
  256. return v.type == this.queryForm.type
  257. });
  258. this.$refs.tree.setCheckedNodes(arr);
  259. this.sNodeList = this.$refs.tree.getCheckedNodes();
  260. }else {
  261. this.sNodeList = this.$refs.tree.getCheckedNodes();
  262. }
  263. let arr = this.sNodeList.map(v=>{return v.id});
  264. this.$emit("selectNode", this.sNodeList);
  265. this.$emit("selectNodeId", arr);
  266. },
  267. getSubOrgIdsByOrgType(topOrg, orgType, orgIdList) {
  268. if (!topOrg) return;
  269. if (topOrg.type == orgType) {
  270. orgIdList.push(topOrg);
  271. }
  272. if (topOrg.children && topOrg.children.length > 0) {
  273. topOrg.children.forEach((item) => {
  274. this.getSubOrgIdsByOrgType(item, orgType, orgIdList);
  275. });
  276. }
  277. },
  278. },
  279. model: {
  280. prop: "defaultNode",
  281. event: "selectNodeId",
  282. },
  283. };
  284. </script>
  285. <style scoped lang="scss">
  286. .tags-box {
  287. background-color: #fff;
  288. border-radius: 4px;
  289. border: 1px solid #dcdfe6;
  290. color: #606266;
  291. outline: 0;
  292. padding: 0 40px 0 5px;
  293. width: 100%;
  294. min-height: 40px;
  295. max-height: 40px;
  296. position: relative;
  297. overflow: hidden;
  298. display: inline-block;
  299. text-overflow: ellipsis;
  300. white-space: nowrap;
  301. cursor: pointer;
  302. > span {
  303. margin: 5px 10px;
  304. }
  305. //&:hover {
  306. // .close-icon {
  307. // display: block;
  308. // color: #666;
  309. // }
  310. //}
  311. .close-icon {
  312. position: absolute;
  313. top: 30%;
  314. right: 5px;
  315. cursor: pointer;
  316. &:hover {
  317. color: #aaa;
  318. }
  319. }
  320. }
  321. .tags-box-disabled{
  322. cursor: default;
  323. }
  324. .tags-box-mini{
  325. min-height: 32px;
  326. max-height: 32px;
  327. line-height: 32px;
  328. > span {
  329. margin: 0 5px;
  330. }
  331. >.tags-num{
  332. line-height: 38px;
  333. }
  334. }
  335. .tags-num{
  336. color:#ccc;
  337. position: absolute;
  338. right: 20px;
  339. top:-4px;
  340. }
  341. .tree-box {
  342. margin-top: 20px;
  343. max-height: 300px;
  344. overflow: auto;
  345. border-radius: 4px;
  346. border: 1px solid #dcdfe6;
  347. user-select: none;
  348. }
  349. </style>