ce3537323ba0d75dbb2a25e9855fe43602999c84.svn-base 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <template>
  2. <div class="tree" :style="{'height':height + 'px','width':width == 0 ? 250 : width + 'px'}">
  3. <el-tree
  4. :data="treeData"
  5. @check-change="handleCheckChange"
  6. :props="treeProps"
  7. :expand-on-click-node="false"
  8. @node-click="nodeClick"
  9. node-key="id"
  10. :default-expanded-keys="tableDefaultExpandedKeys"
  11. ></el-tree>
  12. </div>
  13. </template>
  14. <script>
  15. import request from '../../utils/gw_ajax_request.js';
  16. import {dateFormat} from "../../utils/gw_date";
  17. export default {
  18. components: {},
  19. props: ['pid','type','pidNodes','height','width'],
  20. data() {
  21. return {
  22. treeData: [],
  23. code:'45',
  24. tableDefaultExpandedKeys: ['001','002','003','006'],
  25. treeProps: {
  26. label: "pnm",
  27. children: "children",
  28. disabled: false,
  29. expandOnClickNode: false,
  30. first: true,
  31. },
  32. checkedData: [],
  33. changedData: [],
  34. };
  35. },
  36. mounted: function () {
  37. if(this.width == undefined){
  38. this.width = '250';
  39. }
  40. if(this.height == undefined){
  41. this.height = '550';
  42. }
  43. this.getLeftTreeData();
  44. },
  45. computed: {
  46. userId: function () {
  47. return localStorage.getItem("userid")
  48. ? localStorage.getItem("userid")
  49. : "1098101939614724096";
  50. }
  51. },
  52. methods: {
  53. // 节点点击事件
  54. nodeClick: function (data) {
  55. this.$emit("provTreeSelectChange", {'id':data.id});
  56. },
  57. //根据用户id获取左侧树
  58. getLeftTreeData(){
  59. //
  60. var _self = this;
  61. request.get('/dc/organization/base/getAllNode',{params:{'userId':this.userId,'orgType':this.type}}).then((res)=>{
  62. let data = res.data
  63. data.forEach(ele => {
  64. if(ele.pid == null || ele.pid == ''){
  65. ele.pid = '001'
  66. }if(ele.sttm==null || ele.sttm == ''){
  67. ele.sttm = ''
  68. }else{
  69. ele.sttm = dateFormat(ele.sttm,'yy-MM-dd');
  70. }if(ele.entm==null || ele.entm == ''){
  71. ele.entm = ''
  72. }else{
  73. ele.entm = dateFormat(ele.entm,'yy-MM-dd');
  74. }
  75. });
  76. // data = _self.getTrees(data, data[0].pid);
  77. // 使用新的格式化树方法
  78. data = _self.getTreesNew(data);
  79. _self.treeData = data;
  80. })
  81. },
  82. //格式化树形数据
  83. getTrees(list,pId) {
  84. let items = {};
  85. // 获取每个节点的直属子节点,*记住是直属,不是所有子节点
  86. for(let i = 0; i < list.length; i++) {
  87. let key = list[i].parentId ? list[i].parentId : list[i].pid;
  88. if(items[key]) {
  89. items[key].push(list[i]);
  90. } else {
  91. items[key] = [];
  92. items[key].push(list[i]);
  93. }
  94. }
  95. return this.formatTree(items, pId);
  96. },
  97. getTreesNew(list) {
  98. //@wxcwater重写方法 适应多个树
  99. let highLevelLength = 99;//预设最大节点id长度,目前最大应该为12,所以设置为99保证正常
  100. let _self =this;
  101. list.forEach((item,index)=>{
  102. item['pidLength'] = item.pid&&item.pid!=''?item.pid.length:0;
  103. if(highLevelLength>item.pidLength){
  104. highLevelLength = item.pidLength;
  105. }
  106. });
  107. let heightLevelNode = [];
  108. list.forEach((item,index)=>{
  109. if(highLevelLength>=item.pidLength){
  110. heightLevelNode.push(item);
  111. }
  112. });
  113. list.forEach((item,index)=>{
  114. heightLevelNode.forEach((pItem)=>{
  115. _self.addTreeNode(pItem,item);
  116. });
  117. });
  118. return heightLevelNode;
  119. },
  120. /**利用递归格式化每个节点**/
  121. formatTree(items, pId) {
  122. let result = [];
  123. if(!items[pId]) {
  124. return result;
  125. }
  126. for(let t of items[pId]) {
  127. t.children = this.formatTree(items, t.id)
  128. result.push(t);
  129. }
  130. return result;
  131. },
  132. addTreeNode:function(treeNode,leafNode){
  133. let _self = this;
  134. if(!treeNode.hasOwnProperty('children')){
  135. treeNode['children'] = [];
  136. }
  137. if(leafNode.pid == treeNode.id){
  138. treeNode.children.push(leafNode);
  139. return;
  140. }
  141. treeNode.children.forEach((item) => {
  142. _self.addTreeNode(item,leafNode);
  143. });
  144. },
  145. }
  146. };
  147. </script>
  148. <style scoped>
  149. .tree{
  150. overflow-y: auto;
  151. overflow-x: auto;
  152. }
  153. .el-tree {
  154. min-width: 100%;
  155. display:inline-block !important;
  156. }
  157. /*滚动条样式*/
  158. .tree::-webkit-scrollbar {
  159. width: 8px;
  160. height: 8px;
  161. }
  162. /*正常情况下滑块的样式*/
  163. .tree::-webkit-scrollbar-thumb {
  164. background-color: rgba(0, 0, 0, .05);
  165. border-radius: 10px;
  166. -webkit-box-shadow: inset 1px 1px 0 rgba(0, 0, 0, .1);
  167. }
  168. /*鼠标悬浮在该类指向的控件上时滑块的样式*/
  169. .tree:hover::-webkit-scrollbar-thumb {
  170. background-color: rgba(0, 0, 0, .2);
  171. border-radius: 10px;
  172. -webkit-box-shadow: inset 1px 1px 0 rgba(0, 0, 0, .1);
  173. }
  174. /*鼠标悬浮在滑块上时滑块的样式*/
  175. .tree::-webkit-scrollbar-thumb:hover {
  176. background-color: rgba(0, 0, 0, .4);
  177. -webkit-box-shadow: inset 1px 1px 0 rgba(0, 0, 0, .1);
  178. }
  179. /*正常时候的主干部分*/
  180. .tree::-webkit-scrollbar-track {
  181. border-radius: 10px;
  182. -webkit-box-shadow: inset 0 0 6px rgba(0, 0, 0, 0);
  183. background-color: white;
  184. }
  185. /*鼠标悬浮在滚动条上的主干部分*/
  186. .tree::-webkit-scrollbar-track:hover {
  187. -webkit-box-shadow: inset 0 0 6px rgba(0, 0, 0, .4);
  188. background-color: rgba(0, 0, 0, .01);
  189. }
  190. </style>