index.vue 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. <template>
  2. <!-- 弹出框 -->
  3. <el-popover placement="bottom-start" :trigger="trigger" :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="checkShow? 13:24" :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 v-if="checkShow" :span="11" :xs="24">
  23. <el-checkbox :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. trigger:{
  68. type: String,
  69. default: 'click',
  70. },
  71. //组件禁用
  72. disabled: {
  73. type: Boolean,
  74. default: false,
  75. required: false,
  76. },
  77. checkShow: {
  78. type: Boolean,
  79. default: false,
  80. },
  81. queryData: {
  82. type: [Number,String],
  83. default: null,
  84. required: false,
  85. },
  86. // 机构类型筛选条件禁用
  87. disable: {
  88. type: Boolean,
  89. default: false,
  90. required: false,
  91. },
  92. // 机构树上能选择的机构类型
  93. enabledCheckOrgTypes: {
  94. type: [Number,Array],
  95. default: ()=>[],
  96. required: false,
  97. }
  98. },
  99. data() {
  100. return {
  101. //清除图标
  102. showClearable: false,
  103. //弹窗显示
  104. visible: false,
  105. //结构树
  106. orgTree: null,
  107. //过滤条件
  108. queryForm: {
  109. value: null,
  110. type: null,
  111. },
  112. //受否全选
  113. checked: false,
  114. //已选中的node
  115. sNodeList: [],
  116. //默认显示及展开的node
  117. nodes: [],
  118. treeProps:{
  119. disabled: this.getNodedisabled,
  120. children: "children",
  121. label: "shortName",
  122. }
  123. };
  124. },
  125. mounted() {
  126. this.getDeptTree();
  127. },
  128. watch: {
  129. defaultNode: {
  130. immediate: true,
  131. handler(n) {
  132. if(!n || n.length == 0){
  133. this.nodes = [];
  134. this.sNodeList = [];
  135. this.$nextTick(() => {
  136. this.$refs.tree.setCheckedKeys([]);
  137. });
  138. return
  139. }
  140. this.nodes = this.defaultNode;
  141. this.$nextTick(() => {
  142. let arr = [];
  143. this.defaultNode.forEach(v => {
  144. let node = this.$refs.tree.getNode(v);
  145. arr.push(node.data)
  146. })
  147. this.sNodeList = arr;
  148. });
  149. },
  150. },
  151. queryData: {
  152. handler(n) {
  153. //console.log("queryData",n);
  154. if (n) {
  155. this.queryForm.type = parseInt(n);
  156. //console.log("queryData",this.queryForm.type);
  157. this.serchTreeHandler();
  158. }
  159. },
  160. // queryForm: {
  161. // deep: true,
  162. // handler(n, o) {
  163. // if(!n.value&&!n.type){
  164. // this.$refs.tree.filter({});
  165. // }else{
  166. // this.$refs.tree.filter(n);
  167. // }
  168. // },
  169. // },
  170. },
  171. defaultProps: {
  172. handler(n) {
  173. if (n) {
  174. this.treeProps.label=n.label;
  175. this.treeProps.children=n.children;
  176. }
  177. },
  178. }
  179. },
  180. methods: {
  181. clear() {
  182. this.queryForm.value=null;
  183. this.sNodeList = [];
  184. this.$refs.tree.setCheckedKeys([]);
  185. this.$refs.tree.setCurrentKey(null);
  186. this.nodes = [];
  187. this.$emit("selectNode",[]);
  188. this.$emit("selectNodeId", []);
  189. },
  190. //筛选条件变化
  191. serchTreeHandler() {
  192. this.$refs.tree.filter(this.queryForm);
  193. },
  194. /** 查询机构下拉树结构 */
  195. getDeptTree() {
  196. if (this.customRequest) {
  197. this.customRequest().then((response) => {
  198. this.treeList = response.data;
  199. return
  200. });
  201. }
  202. this.orgTree = this.$store.getters.orgTree;
  203. if (this.hangsheTree) {
  204. this.orgTree = this.$store.getters.depTree;
  205. }
  206. },
  207. getNodedisabled(node) {
  208. if (this.enabledCheckOrgTypes) {
  209. if (Array.isArray(this.enabledCheckOrgTypes)) {
  210. if (this.enabledCheckOrgTypes.length>0 && this.enabledCheckOrgTypes.findIndex((x) => x === node.type) == -1) {
  211. return true;
  212. }
  213. }
  214. else {
  215. if (this.enabledCheckOrgTypes !== node.type) {
  216. return true;
  217. }
  218. }
  219. }
  220. return false;
  221. },
  222. filterNode(value, data, node) {
  223. if (this.queryForm.value && this.queryForm.type) {
  224. return (
  225. data.name.indexOf(this.queryForm.value) !== -1 &&
  226. data.type == this.queryForm.type
  227. );
  228. }
  229. if (this.queryForm.value) {
  230. return data.name.indexOf(this.queryForm.value) !== -1;
  231. }
  232. if (this.queryForm.type) {
  233. return data.type == this.queryForm.type;
  234. }
  235. return true;
  236. },
  237. handleNodeClick() {
  238. },
  239. onCheck(data, checked) {
  240. // if(this.sNodeList.length > 199){
  241. // this.$refs.tree.setChecked(data, false);
  242. // this.$message.error('所选机构过多,应小于200')
  243. // }
  244. if (this.checked) {
  245. let checkNodes = this.$refs.tree.getCheckedNodes();
  246. let type = this.queryForm.type;
  247. let recursionFn = function (tree) {
  248. if(!tree || !tree.length ) return;
  249. tree.forEach((item) => {
  250. if (type == item.type) {
  251. checkNodes.push(item);
  252. //console.log('name',checkNodes.indexOf(item))
  253. //checkNodes.indexOf(item) >= 0? checkNodes.splice(checkNodes.indexOf(item),1):checkNodes.push(item);
  254. }
  255. if (item.children?.length > 0) {
  256. recursionFn(item.children)
  257. } else {
  258. return;
  259. }
  260. });
  261. }
  262. // 递归查询
  263. recursionFn(data.children);
  264. let arr = checkNodes.filter(v => {
  265. return v.type == this.queryForm.type
  266. });
  267. this.$refs.tree.setCheckedNodes(arr);
  268. this.sNodeList = this.$refs.tree.getCheckedNodes();
  269. }else {
  270. this.sNodeList = this.$refs.tree.getCheckedNodes();
  271. }
  272. let arr = this.sNodeList.map(v=>{return v.id});
  273. this.$emit("selectNode", this.sNodeList);
  274. this.$emit("selectNodeId", arr);
  275. },
  276. getSubOrgIdsByOrgType(topOrg, orgType, orgIdList) {
  277. if (!topOrg) return;
  278. if (topOrg.type == orgType) {
  279. orgIdList.push(topOrg);
  280. }
  281. if (topOrg.children && topOrg.children.length > 0) {
  282. topOrg.children.forEach((item) => {
  283. this.getSubOrgIdsByOrgType(item, orgType, orgIdList);
  284. });
  285. }
  286. },
  287. },
  288. model: {
  289. prop: "defaultNode",
  290. event: "selectNodeId",
  291. },
  292. };
  293. </script>
  294. <style scoped lang="scss">
  295. .tags-box {
  296. background-color: #fff;
  297. border-radius: 4px;
  298. border: 1px solid #dcdfe6;
  299. color: #606266;
  300. outline: 0;
  301. padding: 0 40px 0 5px;
  302. width: 100%;
  303. min-height: 40px;
  304. max-height: 40px;
  305. position: relative;
  306. overflow: hidden;
  307. display: inline-block;
  308. text-overflow: ellipsis;
  309. white-space: nowrap;
  310. cursor: pointer;
  311. > span {
  312. margin: 5px 10px;
  313. }
  314. //&:hover {
  315. // .close-icon {
  316. // display: block;
  317. // color: #666;
  318. // }
  319. //}
  320. .close-icon {
  321. position: absolute;
  322. top: 30%;
  323. right: 5px;
  324. cursor: pointer;
  325. &:hover {
  326. color: #aaa;
  327. }
  328. }
  329. }
  330. .tags-box-disabled{
  331. cursor: default;
  332. }
  333. .tags-box-mini{
  334. min-height: 32px;
  335. max-height: 32px;
  336. line-height: 32px;
  337. > span {
  338. margin: 0 5px;
  339. }
  340. >.tags-num{
  341. line-height: 38px;
  342. }
  343. }
  344. .tags-num{
  345. color:#ccc;
  346. position: absolute;
  347. right: 20px;
  348. top:-4px;
  349. }
  350. .tree-box {
  351. margin-top: 20px;
  352. max-height: 300px;
  353. overflow: auto;
  354. border-radius: 4px;
  355. border: 1px solid #dcdfe6;
  356. user-select: none;
  357. }
  358. </style>