ly-tree.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. <template>
  2. <view>
  3. <template v-if="showLoading">
  4. <view class="ly-loader ly-flex-center">
  5. <view class="ly-loader-inner">加载中...</view>
  6. </view>
  7. </template>
  8. <template v-else>
  9. <view v-if="isEmpty || !visible" class="ly-empty">{{emptyText}}</view>
  10. <view class="ly-tree" :class="{'is-empty': isEmpty || !visible}" role="tree" name="LyTreeExpand">
  11. <ly-tree-node v-for="nodeId in childNodesId"
  12. :nodeId="nodeId"
  13. :render-after-expand="renderAfterExpand"
  14. :show-checkbox="showCheckbox"
  15. :show-radio="showRadio"
  16. :check-only-leaf="checkOnlyLeaf"
  17. :key="getNodeKey(nodeId)"
  18. :indent="indent"
  19. :icon-class="iconClass">
  20. </ly-tree-node>
  21. </view>
  22. </template>
  23. </view>
  24. </template>
  25. <script>
  26. import Vue from 'vue'
  27. import TreeStore from './model/tree-store.js';
  28. import {getNodeKey} from './tool/util.js';
  29. import LyTreeNode from './ly-tree-node.vue';
  30. export default {
  31. name: 'LyTree',
  32. componentName: 'LyTree',
  33. components: {
  34. LyTreeNode
  35. },
  36. data() {
  37. return {
  38. elId: `ly_${Math.ceil(Math.random() * 10e5).toString(36)}`,
  39. visible: true,
  40. store: {
  41. ready: false
  42. },
  43. currentNode: null,
  44. childNodesId: []
  45. };
  46. },
  47. provide() {
  48. return {
  49. tree: this
  50. }
  51. },
  52. props: {
  53. // 展示数据
  54. treeData: Array,
  55. // 自主控制loading加载,避免数据还没获取到的空档出现“暂无数据”字样
  56. ready: {
  57. type: Boolean,
  58. default: true
  59. },
  60. // 内容为空的时候展示的文本
  61. emptyText: {
  62. type: String,
  63. default: '暂无数据'
  64. },
  65. // 是否在第一次展开某个树节点后才渲染其子节点
  66. renderAfterExpand: {
  67. type: Boolean,
  68. default: true
  69. },
  70. // 每个树节点用来作为唯一标识的属性,整棵树应该是唯一的
  71. nodeKey: String,
  72. // 在显示复选框的情况下,是否严格的遵循父子不互相关联的做法,默认为 false
  73. checkStrictly: Boolean,
  74. // 是否默认展开所有节点
  75. defaultExpandAll: Boolean,
  76. // 切换全部展开、全部折叠
  77. toggleExpendAll: Boolean,
  78. // 是否在点击节点的时候展开或者收缩节点, 默认值为 true,如果为 false,则只有点箭头图标的时候才会展开或者收缩节点
  79. expandOnClickNode: {
  80. type: Boolean,
  81. default: true
  82. },
  83. // 选中的时候展开节点
  84. expandOnCheckNode: {
  85. type: Boolean,
  86. default: true
  87. },
  88. // 是否在点击节点的时候选中节点,默认值为 false,即只有在点击复选框时才会选中节点
  89. checkOnClickNode: Boolean,
  90. checkDescendants: {
  91. type: Boolean,
  92. default: false
  93. },
  94. // 展开子节点的时候是否自动展开父节点
  95. autoExpandParent: {
  96. type: Boolean,
  97. default: true
  98. },
  99. // 默认勾选的节点的 key 的数组
  100. defaultCheckedKeys: Array,
  101. // 默认展开的节点的 key 的数组
  102. defaultExpandedKeys: Array,
  103. // 是否展开当前节点的父节点
  104. expandCurrentNodeParent: Boolean,
  105. // 当前选中的节点
  106. currentNodeKey: [String, Number],
  107. // 是否最后一层叶子节点才显示单选/多选框
  108. checkOnlyLeaf: {
  109. type: Boolean,
  110. default: false
  111. },
  112. // 节点是否可被选择
  113. showCheckbox: {
  114. type: Boolean,
  115. default: false
  116. },
  117. // 节点单选
  118. showRadio: {
  119. type: Boolean,
  120. default: false
  121. },
  122. // 配置选项
  123. props: {
  124. type: [Object, Function],
  125. default () {
  126. return {
  127. children: 'children', // 指定子树为节点对象的某个属性值
  128. label: 'label', // 指定节点标签为节点对象的某个属性值
  129. disabled: 'disabled' // 指定节点选择框是否禁用为节点对象的某个属性值
  130. };
  131. }
  132. },
  133. // 是否懒加载子节点,需与 load 方法结合使用
  134. lazy: {
  135. type: Boolean,
  136. default: false
  137. },
  138. // 是否高亮当前选中节点,默认值是 false
  139. highlightCurrent: Boolean,
  140. // 加载子树数据的方法,仅当 lazy 属性为true 时生效
  141. load: Function,
  142. // 对树节点进行筛选时执行的方法,返回 true 表示这个节点可以显示,返回 false 则表示这个节点会被隐藏
  143. filterNodeMethod: Function,
  144. // 搜索时是否展示匹配项的所有子节点
  145. childVisibleForFilterNode: {
  146. type: Boolean,
  147. default: false
  148. },
  149. // 是否每次只打开一个同级树节点展开
  150. accordion: Boolean,
  151. // 相邻级节点间的水平缩进,单位为像素
  152. indent: {
  153. type: Number,
  154. default: 18
  155. },
  156. // 自定义树节点的展开图标
  157. iconClass: String,
  158. // 是否显示节点图标,如果配置为true,需要配置props中对应的图标属性名称
  159. showNodeIcon: {
  160. type: Boolean,
  161. default: false
  162. },
  163. // 如果数据量较大,建议不要在node节点中添加parent属性,会造成性能损耗
  164. isInjectParentInNode: {
  165. type: Boolean,
  166. default: false
  167. }
  168. },
  169. computed: {
  170. isEmpty() {
  171. if (this.store.root) {
  172. const childNodes = this.store.root.getChildNodes(this.childNodesId);
  173. return !childNodes || childNodes.length === 0 || childNodes.every(({visible}) => !visible);
  174. }
  175. return true;
  176. },
  177. showLoading() {
  178. return !(this.store.ready && this.ready);
  179. }
  180. },
  181. watch: {
  182. toggleExpendAll(newVal) {
  183. this.store.toggleExpendAll(newVal);
  184. },
  185. defaultCheckedKeys(newVal) {
  186. this.store.setDefaultCheckedKey(newVal);
  187. },
  188. defaultExpandedKeys(newVal) {
  189. this.store.defaultExpandedKeys = newVal;
  190. this.store.setDefaultExpandedKeys(newVal);
  191. },
  192. treeData(newVal) {
  193. this.store.setData(newVal);
  194. },
  195. checkStrictly(newVal) {
  196. this.store.checkStrictly = newVal || this.checkOnlyLeaf;
  197. },
  198. 'store.root.childNodesId'(newVal) {
  199. this.childNodesId = newVal;
  200. },
  201. 'store.root.visible'(newVal) {
  202. this.visible = newVal;
  203. },
  204. childNodesId(){
  205. this.$nextTick(() => {
  206. this.$emit('ly-tree-render-completed');
  207. });
  208. }
  209. },
  210. methods: {
  211. /*
  212. * @description 对树节点进行筛选操作
  213. * @method filter
  214. * @param {all} value 在 filter-node-method 中作为第一个参数
  215. * @param {Object} data 搜索指定节点的节点数据,不传代表搜索所有节点,假如要搜索A节点下面的数据,那么nodeData代表treeData中A节点的数据
  216. */
  217. filter(value, data) {
  218. if (!this.filterNodeMethod) throw new Error('[Tree] filterNodeMethod is required when filter');
  219. this.store.filter(value, data);
  220. },
  221. /*
  222. * @description 获取节点的唯一标识符
  223. * @method getNodeKey
  224. * @param {String, Number} nodeId
  225. * @return {String, Number} 匹配到的数据中的某一项数据
  226. */
  227. getNodeKey(nodeId) {
  228. let node = this.store.root.getChildNodes([nodeId])[0];
  229. return getNodeKey(this.nodeKey, node.data);
  230. },
  231. /*
  232. * @description 获取节点路径
  233. * @method getNodePath
  234. * @param {Object} data 节点数据
  235. * @return {Array} 路径数组
  236. */
  237. getNodePath(data) {
  238. return this.store.getNodePath(data);
  239. },
  240. /*
  241. * @description 若节点可被选择(即 show-checkbox 为 true),则返回目前被选中的节点所组成的数组
  242. * @method getCheckedNodes
  243. * @param {Boolean} leafOnly 是否只是叶子节点,默认false
  244. * @param {Boolean} includeHalfChecked 是否包含半选节点,默认false
  245. * @return {Array} 目前被选中的节点所组成的数组
  246. */
  247. getCheckedNodes(leafOnly, includeHalfChecked) {
  248. return this.store.getCheckedNodes(leafOnly, includeHalfChecked);
  249. },
  250. /*
  251. * @description 若节点可被选择(即 show-checkbox 为 true),则返回目前被选中的节点的 key 所组成的数组
  252. * @method getCheckedKeys
  253. * @param {Boolean} leafOnly 是否只是叶子节点,默认false,若为 true 则仅返回被选中的叶子节点的 keys
  254. * @param {Boolean} includeHalfChecked 是否返回indeterminate为true的节点,默认false
  255. * @return {Array} 目前被选中的节点所组成的数组
  256. */
  257. getCheckedKeys(leafOnly, includeHalfChecked) {
  258. return this.store.getCheckedKeys(leafOnly, includeHalfChecked);
  259. },
  260. /*
  261. * @description 获取当前被选中节点的 data,若没有节点被选中则返回 null
  262. * @method getCurrentNode
  263. * @return {Object} 当前被选中节点的 data,若没有节点被选中则返回 null
  264. */
  265. getCurrentNode() {
  266. const currentNode = this.store.getCurrentNode();
  267. return currentNode ? currentNode.data : null;
  268. },
  269. /*
  270. * @description 获取当前被选中节点的 key,若没有节点被选中则返回 null
  271. * @method getCurrentKey
  272. * @return {all} 当前被选中节点的 key, 若没有节点被选中则返回 null
  273. */
  274. getCurrentKey() {
  275. const currentNode = this.getCurrentNode();
  276. return currentNode ? currentNode[this.nodeKey] : null;
  277. },
  278. /*
  279. * @description 设置全选/取消全选
  280. * @method setCheckAll
  281. * @param {Boolean} isCheckAll 选中状态,默认为true
  282. */
  283. setCheckAll(isCheckAll = true) {
  284. if (this.showRadio) throw new Error('You set the "show-radio" property, so you cannot select all nodes');
  285. if (!this.showCheckbox) console.warn('You have not set the property "show-checkbox". Please check your settings');
  286. this.store.setCheckAll(isCheckAll);
  287. },
  288. /*
  289. * @description 设置目前勾选的节点
  290. * @method setCheckedNodes
  291. * @param {Array} nodes 接收勾选节点数据的数组
  292. * @param {Boolean} leafOnly 是否只是叶子节点, 若为 true 则仅设置叶子节点的选中状态,默认值为 false
  293. */
  294. setCheckedNodes(nodes, leafOnly) {
  295. this.store.setCheckedNodes(nodes, leafOnly);
  296. },
  297. /*
  298. * @description 通过 keys 设置目前勾选的节点
  299. * @method setCheckedKeys
  300. * @param {Array} keys 勾选节点的 key 的数组
  301. * @param {Boolean} leafOnly 是否只是叶子节点, 若为 true 则仅设置叶子节点的选中状态,默认值为 false
  302. */
  303. setCheckedKeys(keys, leafOnly) {
  304. if (!this.nodeKey) throw new Error('[Tree] nodeKey is required in setCheckedKeys');
  305. this.store.setCheckedKeys(keys, leafOnly);
  306. },
  307. /*
  308. * @description 通过 key / data 设置某个节点的勾选状态
  309. * @method setChecked
  310. * @param {all} data 勾选节点的 key 或者 data
  311. * @param {Boolean} checked 节点是否选中
  312. * @param {Boolean} deep 是否设置子节点 ,默认为 false
  313. */
  314. setChecked(data, checked, deep) {
  315. this.store.setChecked(data, checked, deep);
  316. },
  317. /*
  318. * @description 若节点可被选择(即 show-checkbox 为 true),则返回目前半选中的节点所组成的数组
  319. * @method getHalfCheckedNodes
  320. * @return {Array} 目前半选中的节点所组成的数组
  321. */
  322. getHalfCheckedNodes() {
  323. return this.store.getHalfCheckedNodes();
  324. },
  325. /*
  326. * @description 若节点可被选择(即 show-checkbox 为 true),则返回目前半选中的节点的 key 所组成的数组
  327. * @method getHalfCheckedKeys
  328. * @return {Array} 目前半选中的节点的 key 所组成的数组
  329. */
  330. getHalfCheckedKeys() {
  331. return this.store.getHalfCheckedKeys();
  332. },
  333. /*
  334. * @description 通过 node 设置某个节点的当前选中状态
  335. * @method setCurrentNode
  336. * @param {Object} node 待被选节点的 node
  337. */
  338. setCurrentNode(node) {
  339. if (!this.nodeKey) throw new Error('[Tree] nodeKey is required in setCurrentNode');
  340. this.store.setUserCurrentNode(node);
  341. },
  342. /*
  343. * @description 通过 key 设置某个节点的当前选中状态
  344. * @method setCurrentKey
  345. * @param {all} key 待被选节点的 key,若为 null 则取消当前高亮的节点
  346. */
  347. setCurrentKey(key) {
  348. if (!this.nodeKey) throw new Error('[Tree] nodeKey is required in setCurrentKey');
  349. this.store.setCurrentNodeKey(key);
  350. },
  351. /*
  352. * @description 根据 data 或者 key 拿到 Tree 组件中的 node
  353. * @method getNode
  354. * @param {all} data 要获得 node 的 key 或者 data
  355. */
  356. getNode(data) {
  357. return this.store.getNode(data);
  358. },
  359. /*
  360. * @description 删除 Tree 中的一个节点
  361. * @method remove
  362. * @param {all} data 要删除的节点的 data 或者 node
  363. */
  364. remove(data) {
  365. this.store.remove(data);
  366. },
  367. /*
  368. * @description 为 Tree 中的一个节点追加一个子节点
  369. * @method append
  370. * @param {Object} data 要追加的子节点的 data
  371. * @param {Object} parentNode 子节点的 parent 的 data、key 或者 node
  372. */
  373. append(data, parentNode) {
  374. this.store.append(data, parentNode);
  375. },
  376. /*
  377. * @description 为 Tree 的一个节点的前面增加一个节点
  378. * @method insertBefore
  379. * @param {Object} data 要增加的节点的 data
  380. * @param {all} refNode 要增加的节点的后一个节点的 data、key 或者 node
  381. */
  382. insertBefore(data, refNode) {
  383. this.store.insertBefore(data, refNode);
  384. },
  385. /*
  386. * @description 为 Tree 的一个节点的后面增加一个节点
  387. * @method insertAfter
  388. * @param {Object} data 要增加的节点的 data
  389. * @param {all} refNode 要增加的节点的前一个节点的 data、key 或者 node
  390. */
  391. insertAfter(data, refNode) {
  392. this.store.insertAfter(data, refNode);
  393. },
  394. /*
  395. * @description 通过 keys 设置节点子元素
  396. * @method updateKeyChildren
  397. * @param {String, Number} key 节点 key
  398. * @param {Object} data 节点数据的数组
  399. */
  400. updateKeyChildren(key, data) {
  401. if (!this.nodeKey) throw new Error('[Tree] nodeKey is required in updateKeyChild');
  402. this.store.updateChildren(key, data);
  403. }
  404. },
  405. created() {
  406. this.isTree = true;
  407. let props = this.props;
  408. if (typeof this.props === 'function') props = this.props();
  409. if (typeof props !== 'object') throw new Error('props must be of object type.');
  410. this.store = new TreeStore({
  411. key: this.nodeKey,
  412. data: this.treeData,
  413. lazy: this.lazy,
  414. props: props,
  415. load: this.load,
  416. showCheckbox: this.showCheckbox,
  417. showRadio: this.showRadio,
  418. currentNodeKey: this.currentNodeKey,
  419. checkStrictly: this.checkStrictly || this.checkOnlyLeaf,
  420. checkDescendants: this.checkDescendants,
  421. expandOnCheckNode: this.expandOnCheckNode,
  422. defaultCheckedKeys: this.defaultCheckedKeys,
  423. defaultExpandedKeys: this.defaultExpandedKeys,
  424. expandCurrentNodeParent: this.expandCurrentNodeParent,
  425. autoExpandParent: this.autoExpandParent,
  426. defaultExpandAll: this.defaultExpandAll,
  427. filterNodeMethod: this.filterNodeMethod,
  428. childVisibleForFilterNode: this.childVisibleForFilterNode,
  429. showNodeIcon: this.showNodeIcon,
  430. isInjectParentInNode: this.isInjectParentInNode
  431. });
  432. this.childNodesId = this.store.root.childNodesId;
  433. },
  434. beforeDestroy() {
  435. if (this.accordion) {
  436. uni.$off(`${this.elId}-tree-node-expand`)
  437. }
  438. }
  439. };
  440. </script>
  441. <style>
  442. .ly-tree {
  443. position: relative;
  444. cursor: default;
  445. background: #FFF;
  446. color: #606266;
  447. padding: 30rpx;
  448. }
  449. .ly-tree.is-empty {
  450. background: transparent;
  451. }
  452. /* lyEmpty-start */
  453. .ly-empty {
  454. width: 100%;
  455. display: flex;
  456. justify-content: center;
  457. margin-top: 100rpx;
  458. }
  459. /* lyEmpty-end */
  460. /* lyLoader-start */
  461. .ly-loader {
  462. margin-top: 100rpx;
  463. display: flex;
  464. align-items: center;
  465. justify-content: center;
  466. }
  467. .ly-loader-inner,
  468. .ly-loader-inner:before,
  469. .ly-loader-inner:after {
  470. background: #efefef;
  471. animation: load 1s infinite ease-in-out;
  472. width: .5em;
  473. height: 1em;
  474. }
  475. .ly-loader-inner:before,
  476. .ly-loader-inner:after {
  477. position: absolute;
  478. top: 0;
  479. content: '';
  480. }
  481. .ly-loader-inner:before {
  482. left: -1em;
  483. }
  484. .ly-loader-inner {
  485. text-indent: -9999em;
  486. position: relative;
  487. font-size: 22rpx;
  488. animation-delay: 0.16s;
  489. }
  490. .ly-loader-inner:after {
  491. left: 1em;
  492. animation-delay: 0.32s;
  493. }
  494. /* lyLoader-end */
  495. @keyframes load {
  496. 0%,
  497. 80%,
  498. 100% {
  499. box-shadow: 0 0 #efefef;
  500. height: 1em;
  501. }
  502. 40% {
  503. box-shadow: 0 -1.5em #efefef;
  504. height: 1.5em;
  505. }
  506. }
  507. </style>