NaviGation.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <template>
  2. <view class="title" style="background-color: #fff;margin-top: 20rpx;">
  3. <scroll-view ref="sea" scroll-x style="width: 100%;white-space: nowrap;">
  4. <!-- 全部 -->
  5. <view class="inline-item" @click="clickItem(null,-1)">
  6. <text v-if="!isre && treeStack.length == 0" class="none">全部</text>
  7. <text v-else class="active">全部</text>
  8. </view>
  9. <!-- 全部 -->
  10. <!-- 搜索结果 -->
  11. <view v-if="isre" @click="clickItem(null,-2)"
  12. :class="activeSearch?'active inline-item':' none inline-item'">
  13. <i class="iconfont icon-z043 iconclass" />
  14. 搜索结果
  15. </view>
  16. <!-- 搜索结果 -->
  17. <!-- 当前树的层级值 -->
  18. <view v-for="(item,index) in treeStack" class="inline-item" :key="index">
  19. <view class="inline-item" @click="clickItem(item,index)">
  20. <i class="iconfont icon-z043 iconclass" />
  21. <text v-if="index== treeStack.length-1" class="none inline-item">
  22. {{item[slabel]}}
  23. </text>
  24. <text v-else class="active">
  25. {{item[slabel]}}
  26. </text>
  27. </view>
  28. </view>
  29. <!-- 当前树的层级值 -->
  30. </scroll-view>
  31. </view>
  32. </template>
  33. <script>
  34. /**
  35. * 无限级树-面包屑导航
  36. * @description 无限级树的面包屑导航
  37. * @property {String} slabel 显示的label值
  38. * @return {Function} clickItem(item , index) 点击导航栏的索引
  39. * @item 表示导航项对应的值
  40. * @index 表示导航项的层级别索引
  41. * @value -1 全部
  42. * @value -2 表示层级
  43. * @value 其他 (从最外层开始,依次0,1,2,3……)
  44. * @return {Object} outF 导航条内部的方法
  45. * @param {Function} isIre 设置是否搜索状态
  46. * @param {Function} setTreeStack 设置导航树的值
  47. * @param {Function} pushTreeStack 为导航树添加项
  48. * @param {Function} clearTreeStack 清空导航树
  49. */
  50. // scrollLeft : 暂时
  51. export default {
  52. name: "luyj-tree-navigation",
  53. props: {
  54. // 显示的label值
  55. slabel: {
  56. type: String,
  57. default: 'label'
  58. },
  59. },
  60. data() {
  61. return {
  62. isre: false, // 是否进行了搜索(返回是否进行了搜索)
  63. treeStack: [], // 当前搜索值
  64. }
  65. },
  66. computed: {
  67. // 是否可点击搜索结果
  68. activeSearch() {
  69. return this.treeStack.length > 0;
  70. }
  71. },
  72. created() {
  73. // 浅拷贝导航列表的每一个对象(为了不改变item值,也不复制过多的数据)
  74. this.treeStack.forEach(item => {
  75. let tempItem = Object.assign(item);
  76. this.treeStack.push(tempItem);
  77. });
  78. let obj = {
  79. setIsre: this.setIsre,
  80. getIsre: this.getIsre,
  81. setTreeStack: this.setTreeStack,
  82. concatTreeStack: this.concatTreeStack,
  83. pushTreeStack: this.pushTreeStack,
  84. clearTreeStack: this.clearTreeStack,
  85. getTreeStack: this.getTreeStack
  86. };
  87. this.$emit("inF", obj); // 导出的导航栏调用方法
  88. },
  89. methods: {
  90. // ================================== 初始化时导出方法(用于外部调用内部结果) =========================================================
  91. /** 设置isre值(是否搜索)
  92. * @param {Boolean} isre 设置是否搜索
  93. */
  94. setIsre(isre) {
  95. this.isre = isre;
  96. },
  97. /**
  98. * 获取isr值(获取是否搜索中)
  99. */
  100. getIsre() {
  101. return this.isre;
  102. },
  103. /** 设置导航树
  104. * @param {Array} treeStack 导航树
  105. */
  106. setTreeStack(treeStack) {
  107. this.treeStack = treeStack;
  108. },
  109. /** 拼接导航树
  110. * @param {Object} treeStack 导航树
  111. */
  112. concatTreeStack(treeStack) {
  113. this.treeStack = this.treeStack.concat(treeStack);
  114. },
  115. /** 为导航树添加项
  116. * @param {Object} item 待添加的对象
  117. */
  118. pushTreeStack(item) {
  119. this.treeStack.push(item);
  120. },
  121. /**
  122. * 获取当前导航条
  123. */
  124. getTreeStack() {
  125. return this.treeStack;
  126. },
  127. /**
  128. * 清空导航树
  129. */
  130. clearTreeStack() {
  131. this.treeStack.splice(0);
  132. },
  133. // ================================== 监听事件 ===========================================================
  134. /** 点击导航栏索引
  135. * @param {Object} item 当前层的值
  136. * @param {Number} index 索引值
  137. */
  138. clickItem(item, index) {
  139. if (index == -1) {
  140. // 点击全部
  141. this.isre = false;
  142. this.treeStack.splice(0);
  143. } else if (index == -2) {
  144. // 搜索结果
  145. if (this.activeSearch) {
  146. this.isre = true;
  147. this.treeStack.splice(0);
  148. }
  149. } else {
  150. // 点击某一层级树
  151. this.isre = false;
  152. if (this.treeStack.length - 1 > index) {
  153. this.treeStack.splice(index + 1);
  154. }
  155. }
  156. this.$emit("clickItem", item, index);
  157. },
  158. },
  159. // ============================================================================================================
  160. }
  161. </script>
  162. <style lang="scss" scoped>
  163. @import "navigation.scss";
  164. @import "icon.css";
  165. </style>