index.vue 8.0 KB

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