index.vue 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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,
  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. if (n) {
  154. this.queryForm.type = parseInt(n);
  155. this.serchTreeHandler();
  156. }
  157. },
  158. // queryForm: {
  159. // deep: true,
  160. // handler(n, o) {
  161. // if(!n.value&&!n.type){
  162. // this.$refs.tree.filter({});
  163. // }else{
  164. // this.$refs.tree.filter(n);
  165. // }
  166. // },
  167. // },
  168. },
  169. defaultProps: {
  170. handler(n) {
  171. if (n) {
  172. this.treeProps.label=n.label;
  173. this.treeProps.children=n.children;
  174. }
  175. },
  176. }
  177. },
  178. methods: {
  179. clear() {
  180. this.queryForm.value=null;
  181. this.sNodeList = [];
  182. this.$refs.tree.setCheckedKeys([]);
  183. this.$refs.tree.setCurrentKey(null);
  184. this.nodes = [];
  185. this.$emit("selectNode",[]);
  186. this.$emit("selectNodeId", []);
  187. },
  188. //筛选条件变化
  189. serchTreeHandler() {
  190. this.$refs.tree.filter(this.queryForm);
  191. },
  192. /** 查询机构下拉树结构 */
  193. getDeptTree() {
  194. if (this.customRequest) {
  195. this.customRequest().then((response) => {
  196. this.treeList = response.data;
  197. return
  198. });
  199. }
  200. this.orgTree = this.$store.getters.orgTree;
  201. if (this.hangsheTree) {
  202. this.orgTree = this.$store.getters.depTree;
  203. }
  204. },
  205. getNodedisabled(node) {
  206. if (this.enabledCheckOrgTypes) {
  207. if (Array.isArray(this.enabledCheckOrgTypes)) {
  208. if (this.enabledCheckOrgTypes.length>0 && this.enabledCheckOrgTypes.findIndex((x) => x === node.type) == -1) {
  209. return true;
  210. }
  211. }
  212. else {
  213. if (this.enabledCheckOrgTypes !== node.type) {
  214. return true;
  215. }
  216. }
  217. }
  218. return false;
  219. },
  220. filterNode(value, data, node) {
  221. if (this.queryForm.value && this.queryForm.type) {
  222. return (
  223. data.name.indexOf(this.queryForm.value) !== -1 &&
  224. data.type == this.queryForm.type
  225. );
  226. }
  227. if (this.queryForm.value) {
  228. return data.name.indexOf(this.queryForm.value) !== -1;
  229. }
  230. if (this.queryForm.type) {
  231. return data.type == this.queryForm.type;
  232. }
  233. return true;
  234. },
  235. handleNodeClick() {
  236. },
  237. onCheck(data, checked) {
  238. if(this.sNodeList.length > 199){
  239. this.$refs.tree.setChecked(data, false);
  240. this.$message.error('所选机构过多,应小于200')
  241. }
  242. if (this.checked) {
  243. let checkNodes = this.$refs.tree.getCheckedNodes();
  244. let type = this.queryForm.type;
  245. let recursionFn = function (tree) {
  246. if(!tree || !tree.length ) return;
  247. tree.forEach((item) => {
  248. if (type == item.type) {
  249. checkNodes.push(item);
  250. //console.log('name',checkNodes.indexOf(item))
  251. //checkNodes.indexOf(item) >= 0? checkNodes.splice(checkNodes.indexOf(item),1):checkNodes.push(item);
  252. }
  253. if (item.children?.length > 0) {
  254. recursionFn(item.children)
  255. } else {
  256. return;
  257. }
  258. });
  259. }
  260. // 递归查询
  261. recursionFn(data.children);
  262. let arr = checkNodes.filter(v => {
  263. return v.type == this.queryForm.type
  264. });
  265. this.$refs.tree.setCheckedNodes(arr);
  266. this.sNodeList = this.$refs.tree.getCheckedNodes();
  267. }else {
  268. this.sNodeList = this.$refs.tree.getCheckedNodes();
  269. }
  270. let arr = this.sNodeList.map(v=>{return v.id});
  271. this.$emit("selectNode", this.sNodeList);
  272. this.$emit("selectNodeId", arr);
  273. },
  274. getSubOrgIdsByOrgType(topOrg, orgType, orgIdList) {
  275. if (!topOrg) return;
  276. if (topOrg.type == orgType) {
  277. orgIdList.push(topOrg);
  278. }
  279. if (topOrg.children && topOrg.children.length > 0) {
  280. topOrg.children.forEach((item) => {
  281. this.getSubOrgIdsByOrgType(item, orgType, orgIdList);
  282. });
  283. }
  284. },
  285. },
  286. model: {
  287. prop: "defaultNode",
  288. event: "selectNodeId",
  289. },
  290. };
  291. </script>
  292. <style scoped lang="scss">
  293. .tags-box {
  294. background-color: #fff;
  295. border-radius: 4px;
  296. border: 1px solid #dcdfe6;
  297. color: #606266;
  298. outline: 0;
  299. padding: 0 40px 0 5px;
  300. width: 100%;
  301. min-height: 40px;
  302. max-height: 40px;
  303. position: relative;
  304. overflow: hidden;
  305. display: inline-block;
  306. text-overflow: ellipsis;
  307. white-space: nowrap;
  308. cursor: pointer;
  309. > span {
  310. margin: 5px 10px;
  311. }
  312. //&:hover {
  313. // .close-icon {
  314. // display: block;
  315. // color: #666;
  316. // }
  317. //}
  318. .close-icon {
  319. position: absolute;
  320. top: 30%;
  321. right: 5px;
  322. cursor: pointer;
  323. &:hover {
  324. color: #aaa;
  325. }
  326. }
  327. }
  328. .tags-box-disabled{
  329. cursor: default;
  330. }
  331. .tags-box-mini{
  332. min-height: 32px;
  333. max-height: 32px;
  334. line-height: 32px;
  335. > span {
  336. margin: 0 5px;
  337. }
  338. >.tags-num{
  339. line-height: 38px;
  340. }
  341. }
  342. .tags-num{
  343. color:#ccc;
  344. position: absolute;
  345. right: 20px;
  346. top:-4px;
  347. }
  348. .tree-box {
  349. margin-top: 20px;
  350. max-height: 300px;
  351. overflow: auto;
  352. border-radius: 4px;
  353. border: 1px solid #dcdfe6;
  354. user-select: none;
  355. }
  356. </style>