TreeList.vue 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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: true
  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. // arr当前以及上级的id
  106. selectChange(option) {
  107. let newArr = []
  108. let arr = [], level = null, currentNodeId = null
  109. if (option.length != 0) {
  110. if (option.length == 2) {
  111. var result = []; // 两组数组对象取不同属性的值
  112. for(var i = 0; i < option.length; i++){
  113. var obj = option[i];
  114. var num = obj.title;
  115. var isExist = false;
  116. for(var j = 0; j < this.currentStaData.length; j++){
  117. var aj = this.currentStaData[j];
  118. var n = aj.title;
  119. if(n == num){
  120. isExist = true;
  121. break;
  122. }
  123. }
  124. if(!isExist){
  125. result.push(obj);
  126. }
  127. }
  128. newArr = result
  129. } else {
  130. newArr = option
  131. }
  132. this.treeData.forEach((first, firstIndex,firstArr) => {
  133. first.expand = false; //expand 是否展开直子节点
  134. if (this.clickAllNode) {
  135. first.disabled = false; // disabled 是否禁止选中
  136. }
  137. // first.disabled = false; // disabled 是否禁止选中
  138. first.selected = false;
  139. if (first.title == newArr[0].title && first.id == newArr[0].id) {
  140. first.selected = true;
  141. first.disabled = true; // disabled 是否禁止选中
  142. first.expand = true;
  143. this.selectVal = first.title;
  144. level = first.level
  145. currentNodeId = first.nodeId
  146. }
  147. if (first.children) {
  148. first.children.forEach((second, secondIndex,secondArr) => {
  149. second.expand = false; //expand 是否展开直子节点
  150. second.disabled = false; // disabled 是否禁止选中
  151. second.selected = false;
  152. if (newArr[0].level == 1) {
  153. secondArr[firstIndex].expand = true
  154. }
  155. if (second.title == newArr[0].title && second.id == newArr[0].id) {
  156. second.selected = true;
  157. second.disabled = true; // disabled 是否禁止选中
  158. second.expand = true;
  159. this.selectVal = second.title;
  160. currentNodeId = second.nodeId
  161. first.expand = true
  162. arr.push(second.nodeId)
  163. level = second.level
  164. }
  165. if (second.children && second.children.length>0) {
  166. second.children.forEach((thrid,thridIndex,thridArr) => {
  167. thrid.expand = false; //expand 是否展开直子节点
  168. thrid.disabled = false;
  169. thrid.selected = false;
  170. if (thrid.title == newArr[0].title && thrid.id == newArr[0].id) {
  171. thrid.selected = true;
  172. thrid.disabled = true; // disabled 是否禁止选中
  173. thrid.expand = true;
  174. second.expand = true
  175. first.expand = true
  176. this.selectVal = thrid.title;
  177. currentNodeId = thrid.nodeId
  178. arr.push(second.nodeId)
  179. arr.push(thrid.nodeId)
  180. level = thrid.level
  181. }
  182. if (thrid.children && thrid.children.length>0) {
  183. thrid.children.forEach((forth) => {
  184. forth.expand = false; //expand 是否展开直子节点
  185. forth.disabled = false;
  186. forth.selected = false;
  187. // if (newArr[0].level == 1) {
  188. // }
  189. if (forth.title == newArr[0].title && forth.id == newArr[0].id) {
  190. forth.selected = true;
  191. forth.disabled = true; // disabled 是否禁止选中
  192. forth.expand = true;
  193. second.expand = true
  194. first.expand = true
  195. thrid.expand = true;
  196. this.selectVal = forth.title;
  197. currentNodeId = forth.nodeId
  198. arr.push(second.nodeId)
  199. arr.push(thrid.nodeId)
  200. arr.push(forth.nodeId)
  201. level = forth.level
  202. }
  203. if (forth.children && forth.children.length>0) {
  204. forth.children.forEach((five) => {
  205. five.expand = false; //expand 是否展开直子节点
  206. five.disabled = false;
  207. five.selected = false;
  208. // if (newArr[0].level == 1) {
  209. // console.log('level4')
  210. // }
  211. if (five.title == newArr[0].title && five.id == newArr[0].id) {
  212. five.selected = true;
  213. five.disabled = true; // disabled 是否禁止选中
  214. five.expand = true;
  215. second.expand = true
  216. first.expand = true
  217. thrid.expand = true
  218. forth.expand = true
  219. this.selectVal = five.title;
  220. currentNodeId = five.nodeId
  221. arr.push(second.nodeId)
  222. arr.push(thrid.nodeId)
  223. arr.push(forth.nodeId)
  224. arr.push(five.nodeId)
  225. level = five.level
  226. }
  227. })
  228. }
  229. })
  230. }
  231. })
  232. }
  233. });
  234. }
  235. });
  236. }
  237. this.$emit('treeChange',this.selectVal,arr,level,currentNodeId)
  238. },
  239. }
  240. }
  241. </script>
  242. <style scoped lang="stylus">
  243. >>> .ivu-tree-arrow {
  244. color: #41aeff;
  245. width: 15px;
  246. }
  247. >>> .ivu-tree-title {
  248. color: #fff;
  249. min-width: 90%;
  250. padding: 6px 4px;
  251. vertical-align: middle;
  252. }
  253. >>> .ivu-tree-title:hover {
  254. background-color: transparent;
  255. }
  256. >>> .ivu-tree-title-selected, >>> .ivu-tree-title-selected:hover {
  257. background-color: #3565BC;
  258. // background-color: transparent;
  259. color: #fff;
  260. }
  261. >>> .ivu-tree-title img {
  262. width: 18px;
  263. height: 18px;
  264. }
  265. // >>> .ivu-tree .ivu-icon {// 禁止旋转
  266. // -webkit-transform: rotate(0deg);
  267. // transform: rotate(0deg);
  268. // }
  269. // >>> .ivu-tree .ivu-icon-ios-arrow-forward:before { //改变tree默认折叠的三角箭头
  270. // background: url('../../assets/images/yu.png') no-repeat 0 3px;
  271. // content: '';
  272. // display: block;
  273. // width: 16px;
  274. // height: 16px;
  275. // font-size: 16px;
  276. // background-size: 16px;
  277. // }
  278. // >>> .ivu-tree .ivu-tree-arrow-open .ivu-icon-ios-arrow-forward:before { //改变tree默认展开的三角箭头
  279. // background: url('../../assets/images/warning.png') no-repeat 0 0px;
  280. // content: '';
  281. // display: block;
  282. // width: 16px;
  283. // height: 16px;
  284. // font-size: 16px;
  285. // background-size: 16px;
  286. // }
  287. </style>