ly-tree-node.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. <template>
  2. <view ref="node"
  3. name="LyTreeNode"
  4. v-show="node.visible"
  5. class="ly-tree-node"
  6. :class="{
  7. 'is-expanded': expanded,
  8. 'is-hidden': !node.visible,
  9. 'is-checked': !node.disabled && node.checked
  10. }"
  11. role="treeitem"
  12. @tap.stop="handleClick" >
  13. <view class="ly-tree-node__content"
  14. :class="{
  15. 'is-current': node.isCurrent && highlightCurrent
  16. }"
  17. :style="{
  18. 'padding-left': (node.level - 1) * indent + 'px'
  19. }">
  20. <text
  21. @tap.stop="handleExpandIconClick"
  22. :class="[
  23. {
  24. 'is-leaf': node.isLeaf,
  25. expanded: !node.isLeaf && node.expanded
  26. },
  27. 'ly-tree-node__expand-icon',
  28. iconClass ? iconClass : 'ly-iconfont ly-icon-caret-right'
  29. ]">
  30. </text>
  31. <ly-checkbox v-if="checkboxVisible || radioVisible"
  32. :type="checkboxVisible ? 'checkbox' : 'radio'"
  33. :checked="node.checked"
  34. :indeterminate="node.indeterminate"
  35. :disabled="!!node.disabled"
  36. @check="handleCheckChange(!node.checked)"/>
  37. <text v-if="node.loading"
  38. class="ly-tree-node__loading-icon ly-iconfont ly-icon-loading">
  39. </text>
  40. <template v-if="node.icon && node.icon.length > 0">
  41. <image
  42. v-if="node.icon.indexOf('/') !== -1"
  43. class="ly-tree-node__icon"
  44. mode="widthFix"
  45. :src="node.icon"
  46. @error="handleImageError"
  47. >
  48. </image>
  49. <text v-else
  50. class="ly-tree-node__icon"
  51. :class="node.icon">
  52. </text>
  53. </template>
  54. <view style="display: flex;justify-content: space-between;width: 100%;">
  55. <text class="ly-tree-node__label">{{node.label.split('&*(')[0] || 0 }}</text>
  56. <text
  57. @click.stop="nodeIdClick(node.data.value)"
  58. class="ly-tree-node__label"
  59. style="width: 120rpx;text-align: center;">
  60. {{node.label.split('&*(')[1] !== 'undefined' ?node.label.split('&*(')[1] : 0 }}
  61. </text>
  62. </view>
  63. </view>
  64. <view v-if="!renderAfterExpand || childNodeRendered"
  65. v-show="expanded"
  66. class="ly-tree-node__children"
  67. role="group">
  68. <ly-tree-node v-for="cNodeId in node.childNodesId"
  69. :nodeId="cNodeId"
  70. :render-after-expand="renderAfterExpand"
  71. :show-checkbox="showCheckbox"
  72. :show-radio="showRadio"
  73. :check-only-leaf="checkOnlyLeaf"
  74. :key="getNodeKey(cNodeId)"
  75. :indent="indent"
  76. :icon-class="iconClass">
  77. </ly-tree-node>
  78. </view>
  79. </view>
  80. </template>
  81. <script>
  82. import {getNodeKey} from './tool/util.js';
  83. import lyCheckbox from './components/ly-checkbox.vue';
  84. export default {
  85. name: 'LyTreeNode',
  86. componentName: 'LyTreeNode',
  87. components: {
  88. lyCheckbox
  89. },
  90. props: {
  91. nodeId: [Number, String],
  92. renderAfterExpand: {
  93. type: Boolean,
  94. default: true
  95. },
  96. checkOnlyLeaf: {
  97. type: Boolean,
  98. default: false
  99. },
  100. showCheckbox: {
  101. type: Boolean,
  102. default: false
  103. },
  104. showRadio: {
  105. type: Boolean,
  106. default: false
  107. },
  108. indent: Number,
  109. iconClass: String
  110. },
  111. data() {
  112. return {
  113. node: {
  114. indeterminate: false,
  115. checked: false,
  116. expanded: false
  117. },
  118. expanded: false,
  119. childNodeRendered: false,
  120. oldChecked: null,
  121. oldIndeterminate: null,
  122. highlightCurrent: false
  123. };
  124. },
  125. inject: ['tree'],
  126. computed: {
  127. checkboxVisible() {
  128. if (this.checkOnlyLeaf) {
  129. return this.showCheckbox && this.node.isLeaf;
  130. }
  131. return this.showCheckbox;
  132. },
  133. radioVisible() {
  134. if (this.checkOnlyLeaf) {
  135. return this.showRadio && this.node.isLeaf;
  136. }
  137. return this.showRadio;
  138. }
  139. },
  140. watch: {
  141. 'node.indeterminate'(val) {
  142. this.handleSelectChange(this.node.checked, val);
  143. },
  144. 'node.checked'(val) {
  145. this.handleSelectChange(val, this.node.indeterminate);
  146. },
  147. 'node.expanded'(val) {
  148. this.$nextTick(() => this.expanded = val);
  149. if (val) {
  150. this.childNodeRendered = true;
  151. }
  152. }
  153. },
  154. methods: {
  155. // 点击数量后获取id跳转详情列表
  156. nodeIdClick(id) {
  157. uni.$emit('getTreeId', id)
  158. },
  159. getNodeKey(nodeId) {
  160. let node = this.tree.store.root.getChildNodes([nodeId])[0];
  161. return getNodeKey(this.tree.nodeKey, node.data);
  162. },
  163. handleSelectChange(checked, indeterminate) {
  164. if (this.oldChecked !== checked && this.oldIndeterminate !== indeterminate) {
  165. if (this.checkOnlyLeaf && !this.node.isLeaf) return;
  166. if (this.checkboxVisible) {
  167. const allNodes = this.tree.store._getAllNodes();
  168. this.tree.$emit('check-change', {
  169. checked,
  170. indeterminate,
  171. node: this.node,
  172. data: this.node.data,
  173. checkedall: allNodes.every(item => item.checked)
  174. });
  175. } else {
  176. this.tree.$emit('radio-change', {
  177. checked,
  178. node: this.node,
  179. data: this.node.data,
  180. checkedall: false
  181. });
  182. }
  183. }
  184. if (!this.expanded && this.tree.expandOnCheckNode && checked) {
  185. this.handleExpandIconClick();
  186. }
  187. this.oldChecked = checked;
  188. this.indeterminate = indeterminate;
  189. },
  190. handleClick() {
  191. this.tree.store.setCurrentNode(this.node);
  192. this.tree.$emit('current-change', {
  193. node: this.node,
  194. data: this.tree.store.currentNode ? this.tree.store.currentNode.data : null,
  195. currentNode: this.tree.store.currentNode
  196. });
  197. this.tree.currentNode = this.node;
  198. if (this.tree.expandOnClickNode) {
  199. this.handleExpandIconClick();
  200. }
  201. if (this.tree.checkOnClickNode && !this.node.disabled) {
  202. (this.checkboxVisible || this.radioVisible) && this.handleCheckChange(!this.node.checked);
  203. }
  204. this.tree.$emit('node-click', this.node);
  205. },
  206. handleExpandIconClick() {
  207. if (this.node.isLeaf) return;
  208. if (this.expanded) {
  209. this.tree.$emit('node-collapse', this.node);
  210. this.node.collapse();
  211. } else {
  212. this.node.expand();
  213. this.tree.$emit('node-expand', this.node);
  214. if (this.tree.accordion) {
  215. uni.$emit(`${this.tree.elId}-tree-node-expand`, this.node);
  216. }
  217. }
  218. },
  219. handleCheckChange(checked) {
  220. if (this.node.disabled) return;
  221. if (this.checkboxVisible) {
  222. this.node.setChecked(checked, !(this.tree.checkStrictly || this.checkOnlyLeaf));
  223. } else {
  224. this.node.setRadioChecked(checked);
  225. }
  226. this.$nextTick(() => {
  227. this.tree.$emit('check', {
  228. node: this.node,
  229. data: this.node.data,
  230. checkedNodes: this.tree.store.getCheckedNodes(),
  231. checkedKeys: this.tree.store.getCheckedKeys(),
  232. halfCheckedNodes: this.tree.store.getHalfCheckedNodes(),
  233. halfCheckedKeys: this.tree.store.getHalfCheckedKeys()
  234. });
  235. });
  236. },
  237. handleImageError() {
  238. this.node.icon = this.tree.defaultNodeIcon;
  239. }
  240. },
  241. created() {
  242. if (!this.tree) {
  243. throw new Error('Can not find node\'s tree.');
  244. }
  245. this.node = this.tree.store.nodesMap[this.nodeId];
  246. this.highlightCurrent = this.tree.highlightCurrent;
  247. if (this.node.expanded) {
  248. this.expanded = true;
  249. this.childNodeRendered = true;
  250. }
  251. const props = this.tree.props || {};
  252. const childrenKey = props['children'] || 'children';
  253. this.$watch(`node.data.${childrenKey}`, () => {
  254. this.node.updateChildren();
  255. });
  256. if (this.tree.accordion) {
  257. uni.$on(`${this.tree.elId}-tree-node-expand`, node => {
  258. if (this.node.id !== node.id && this.node.level === node.level) {
  259. this.node.collapse();
  260. }
  261. });
  262. }
  263. },
  264. beforeDestroy() {
  265. this.$parent = null;
  266. }
  267. };
  268. </script>
  269. <style>
  270. .ly-tree-node {
  271. white-space: nowrap;
  272. outline: 0
  273. }
  274. .ly-tree-node__content {
  275. display: flex;
  276. align-items: center;
  277. height: 70rpx;
  278. }
  279. .ly-tree-node__content.is-current {
  280. background-color: #F5F7FA;
  281. }
  282. .ly-tree-node__content>.ly-tree-node__expand-icon {
  283. padding: 12rpx;
  284. }
  285. .ly-tree-node__checkbox {
  286. display: flex;
  287. margin-right: 16rpx;
  288. width: 40rpx;
  289. height: 40rpx;
  290. }
  291. .ly-tree-node__checkbox>image {
  292. width: 40rpx;
  293. height: 40rpx;
  294. }
  295. .ly-tree-node__expand-icon {
  296. color: #C0C4CC;
  297. font-size: 28rpx;
  298. -webkit-transform: rotate(0);
  299. transform: rotate(0);
  300. -webkit-transition: -webkit-transform .3s ease-in-out;
  301. transition: -webkit-transform .3s ease-in-out;
  302. transition: transform .3s ease-in-out;
  303. transition: transform .3s ease-in-out, -webkit-transform .3s ease-in-out
  304. }
  305. .ly-tree-node__expand-icon.expanded {
  306. -webkit-transform: rotate(90deg);
  307. transform: rotate(90deg)
  308. }
  309. .ly-tree-node__expand-icon.is-leaf {
  310. color: transparent;
  311. }
  312. .ly-tree-node__icon {
  313. width: 34rpx;
  314. height: 34rpx;
  315. overflow: hidden;
  316. margin-right: 16rpx;
  317. }
  318. .ly-tree-node__label {
  319. font-size: 28rpx
  320. }
  321. .ly-tree-node__loading-icon {
  322. margin-right: 16rpx;
  323. font-size: 28rpx;
  324. color: #C0C4CC;
  325. -webkit-animation: rotating 2s linear infinite;
  326. animation: rotating 2s linear infinite
  327. }
  328. .ly-tree-node>.ly-tree-node__children {
  329. overflow: hidden;
  330. background-color: transparent
  331. }
  332. .ly-tree-node>.ly-tree-node__children.collapse-transition {
  333. transition: height .3s ease-in-out;
  334. }
  335. .ly-tree-node.is-expanded>.ly-tree-node__children {
  336. display: block
  337. }
  338. .ly-tree-node_collapse {
  339. overflow: hidden;
  340. padding-top: 0;
  341. padding-bottom: 0;
  342. }
  343. /* lyTree-end */
  344. /* iconfont-start */
  345. @font-face {
  346. font-family: "ly-iconfont";
  347. src: url('data:application/x-font-woff2;charset=utf-8;base64,d09GMgABAAAAAAPsAAsAAAAACKwAAAOeAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHEIGVgCDBgqFDIQPATYCJAMMCwgABCAFhG0HQBtfB8gekiSCdAwUAKgCFMA5Hj7H0PeTlABUr57PVyGqugqzSWJnNwWoWJjx/9rUr4TPL1ZSQpU2mycqwoRwIN3p+MkqMqyEW+OtMBLPSUBb8v//XtWMKTavxYIUsT/Wy1qbQzkBDOYEKGB7dVpPyVqgCnJNwvMvhZl10nMCtQbFoPVhY8ZDncJfF4grbqpQ13AqE52hWqgcOFrEQ6hWnW5VfMCD7Pfjn4WoI6nI/K0bl0MNGPBz0qcflVqYnvCA4vNDPUXGPFCIw8HgtsqiOK9SrW2smm6sVITElWlpISMdVBn8wyMJopLfXg+myZ48KCrSkvj9g37U1ItbXYke4APwXxK3N4TuehyBfmM0I3zbNdt7uk3VnjPtzX0rnIl7z7bZvb/thHohsu9QuykKo+Cws4nL7LsPmI3n2qN9B9upZEIKd4hu0NCKi0rt7fNtdl+I1N25hOJMDQK6odS123tROR7Pg8toEhDaF+kR0TYjxW6M58F5+ZNQOxmZHtE2g+IYjxjlNy/yIRQpCmrgq5R4/3jx8PvT8Ha8d3/xiLnt4EGyaDnznzRv8vpyZ+9TFHf/ntX9e59A+b6+fPHd5+dy0wYHVvHOroWbnWe879O9DnL53bN/gUHuwm28b/n8i/V3ry4E3IoXNqS6Rvs0LhJxeNVjoUkM3LKosU+0a6rh45FVvLt+2oz7Zd53b4QOy7/9snDXHbqVu+A+f8r7PnM2H8kXrWm5c8/vLu7LqRee7HW637mz3kHc5U/RCXf25d7G8tkdgEfwIpzpkknGpaMw3ww55q9Mn9OQNyua/wB/49OOWydn4eL/6roCfjx6FMmcxfJStYRKfd3UwoHiML4rF4uMSK+SvYTuNxMHrpl8yd3Q6v32cAeo/KFaowBJlQHIqo3zi3geKtRZhErVlqDWnOGn67QRKkWpwaw1AkKza5A0egFZszf8In4HFTp9h0rNUQm1NqP1lXUmgyuDBVUlNYi2gHA98FnokUreOZaac1xV1JlMMZGKEs+QdCLVrgynPhUcO0pzzYyUjDAReGSYeBl13YCEIrCpLhOWlGE+mWRD35TQAw8UawRKJVEGQrMAwekCPpaMlpTOz49FmeZwqcREX1t3Ikoo4dMTaQmpBfzhRn9R30uZXTKXKUOSmLSKEQIeYhjqKZcrcIzhMLLRrJMSrA35UF4yGMaWGhPHm733dwJq+Z/NkSJHUXemCirjgpuWrHMD1eC+mQUAAAA=') format('woff2');
  348. }
  349. .ly-iconfont {
  350. font-family: "ly-iconfont" !important;
  351. font-size: 30rpx;
  352. font-style: normal;
  353. -webkit-font-smoothing: antialiased;
  354. -moz-osx-font-smoothing: grayscale;
  355. }
  356. .ly-icon-caret-right:before {
  357. content: "\e8ee";
  358. }
  359. .ly-icon-loading:before {
  360. content: "\e657";
  361. }
  362. /* iconfont-end */
  363. /* animate-start */
  364. @keyframes rotating {
  365. 0% {
  366. -webkit-transform: rotateZ(0);
  367. transform: rotateZ(0)
  368. }
  369. 100% {
  370. -webkit-transform: rotateZ(360deg);
  371. transform: rotateZ(360deg)
  372. }
  373. }
  374. /* animate-end */
  375. </style>