TreeList.vue 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <template>
  2. <div>
  3. <Tree :data="treeData" @on-select-change="selectChange" ref="tree" :render="renderContent"></Tree>
  4. </div>
  5. </template>
  6. <script>
  7. export default {
  8. name: "TreeList",
  9. props: {
  10. currentStaData: {
  11. type: Array,
  12. default: () => {
  13. return [];
  14. }
  15. },
  16. defaultData: {
  17. type: Array,
  18. default: () => {
  19. return [];
  20. }
  21. },
  22. clickAllNode: { // 是否能点击全部节点 (true可点击 false只能点击最后层级站点)
  23. type: Boolean,
  24. default: false
  25. },
  26. firstCalssName: {
  27. type: String,
  28. default: 'icon-hefeiditie'
  29. },
  30. secondCalssName: {
  31. type: String,
  32. default: 'icon-sutpczhihuizhantai'
  33. },
  34. thirdCalssName: {
  35. type: String,
  36. default: 'icon-ditiezhaofang'
  37. },
  38. },
  39. data() {
  40. return {
  41. treeData: [],
  42. selectVal: '合肥火车站',
  43. };
  44. },
  45. watch: {
  46. defaultData: {
  47.  handler(newValue, oldValue) {
  48. this.treeData = newValue;
  49. }, 
  50. }
  51. },
  52. mounted() {
  53. this.treeData = this.defaultData;
  54. },
  55. methods: {
  56. renderContent(h, { root, node, data }) {
  57. let calssName = "", color="";
  58. if (data.level == 1) {
  59. calssName = 'iconfont '+ this.firstCalssName;
  60. color = 'red'
  61. } else if (data.level == 2) {
  62. calssName = 'iconfont '+ this.secondCalssName;
  63. color = '#41aeff'
  64. } else {
  65. calssName = 'iconfont '+ this.thirdCalssName
  66. color = '#41aeff'
  67. }
  68. return h(
  69. "span",
  70. {
  71. style: {
  72. display: "inline-block",
  73. width: "100%",
  74. }
  75. },
  76. [
  77. h("span", [
  78. h("i", {
  79. style: {
  80. verticalAlign: "sub",
  81. display: "inline-block",
  82. margin: "0px 5px",
  83. color: color,
  84. fontSize:'24px'
  85. },
  86. class: calssName
  87. }),
  88. // h("img", {
  89. // attrs: {
  90. // src: src
  91. // },
  92. // style: {
  93. // verticalAlign: "text-bottom",
  94. // display: "inline-block",
  95. // margin: "0px 5px"
  96. // }
  97. // }),
  98. h(
  99. "span",data.title
  100. )
  101. ])
  102. ]
  103. );
  104. },
  105. selectChange(option) {
  106. let newArr = []
  107. let arr = [], level = null
  108. if (option.length != 0) {
  109. if (option.length == 2) {
  110. var result = []; // 两组数组对象取不同属性的值
  111. for(var i = 0; i < option.length; i++){
  112. var obj = option[i];
  113. var num = obj.title;
  114. var isExist = false;
  115. for(var j = 0; j < this.currentStaData.length; j++){
  116. var aj = this.currentStaData[j];
  117. var n = aj.title;
  118. if(n == num){
  119. isExist = true;
  120. break;
  121. }
  122. }
  123. if(!isExist){
  124. result.push(obj);
  125. }
  126. }
  127. newArr = result
  128. } else {
  129. newArr = option
  130. }
  131. if (this.clickAllNode) {
  132. this.treeData.forEach((items, index,itemsArr) => {
  133. items.expand = false; //expand 是否展开直子节点
  134. items.disabled = false; // disabled 是否禁止选中
  135. items.selected = false;
  136. if (items.title == newArr[0].title && items.id == newArr[0].id) {
  137. items.selected = true;
  138. items.disabled = true; // disabled 是否禁止选中
  139. items.expand = true;
  140. this.selectVal = items.title;
  141. level = items.level
  142. }
  143. if (items.children) {
  144. items.children.forEach((val, i,valArr) => {
  145. val.expand = false; //expand 是否展开直子节点
  146. val.disabled = false; // disabled 是否禁止选中
  147. val.selected = false;
  148. if (val.title == newArr[0].title && val.id == newArr[0].id) {
  149. val.selected = true;
  150. val.disabled = true; // disabled 是否禁止选中
  151. val.expand = true;
  152. this.selectVal = val.title;
  153. items.expand = true
  154. arr.push(val.id)
  155. level = val.level
  156. }
  157. if (val.children && val.children.length>0) {
  158. val.children.forEach((selectItem, j) => {
  159. selectItem.expand = false; //expand 是否展开直子节点
  160. selectItem.disabled = false;
  161. selectItem.selected = false;
  162. if (newArr[0].title =='合肥轨道交通') {
  163. valArr[index].expand = true
  164. }
  165. if (selectItem.title == newArr[0].title && selectItem.id == newArr[0].id) {
  166. selectItem.selected = true;
  167. selectItem.disabled = true; // disabled 是否禁止选中
  168. selectItem.expand = true;
  169. val.expand = true
  170. items.expand = true
  171. this.selectVal = selectItem.title;
  172. arr.push(val.id)
  173. arr.push(selectItem.nodeId)
  174. level = selectItem.level
  175. }
  176. })
  177. }
  178. });
  179. }
  180. });
  181. } else {
  182. this.treeData.forEach((items, index) => {
  183. items.expand = false; //expand 是否展开直子节点
  184. items.children.forEach((val, i) => {
  185. val.selected = false; // selected 是否选中子节点
  186. val.disabled = false;
  187. if (val.title == newArr[0].title && val.id == newArr[0].id) {
  188. val.selected = true;
  189. val.disabled = true; // disabled 是否禁止选中
  190. items.expand = true;
  191. this.selectVal = val.title;
  192. }
  193. });
  194. });
  195. }
  196. }
  197. this.$emit('treeChange',this.selectVal,arr,level)
  198. },
  199. }
  200. }
  201. </script>
  202. <style scoped lang="stylus">
  203. >>> .ivu-tree-arrow {
  204. color: #41aeff;
  205. // height: 30px;
  206. // line-height: 30px;
  207. width: 15px;
  208. }
  209. >>> .ivu-tree-children li {
  210. // width: 100%;
  211. }
  212. >>> .ivu-tree-title {
  213. color: #fff;
  214. min-width: 90%;
  215. padding: 6px 4px;
  216. vertical-align: middle;
  217. }
  218. >>> .ivu-tree-title:hover {
  219. background-color: transparent;
  220. }
  221. >>> .ivu-tree-title-selected, >>> .ivu-tree-title-selected:hover {
  222. background-color: #3565BC;
  223. // background-color: transparent;
  224. color: #fff;
  225. }
  226. >>> .ivu-tree-title img {
  227. width: 18px;
  228. height: 18px;
  229. }
  230. // >>> .ivu-tree .ivu-icon {// 禁止旋转
  231. // -webkit-transform: rotate(0deg);
  232. // transform: rotate(0deg);
  233. // }
  234. // >>> .ivu-tree .ivu-icon-ios-arrow-forward:before { //改变tree默认折叠的三角箭头
  235. // background: url('../../assets/images/yu.png') no-repeat 0 3px;
  236. // content: '';
  237. // display: block;
  238. // width: 16px;
  239. // height: 16px;
  240. // font-size: 16px;
  241. // background-size: 16px;
  242. // }
  243. // >>> .ivu-tree .ivu-tree-arrow-open .ivu-icon-ios-arrow-forward:before { //改变tree默认展开的三角箭头
  244. // background: url('../../assets/images/warning.png') no-repeat 0 0px;
  245. // content: '';
  246. // display: block;
  247. // width: 16px;
  248. // height: 16px;
  249. // font-size: 16px;
  250. // background-size: 16px;
  251. // }
  252. </style>