gui-tree.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <template>
  2. <view>
  3. <view
  4. v-for="(tree,index) in treesIN"
  5. :key="index">
  6. <view
  7. class="gui-flex gui-row gui-nowrap gui-align-items-center gui-tree"
  8. :data-havsons="tree.children"
  9. :data-treeindexs="indexesIn"
  10. :data-index="index"
  11. :data-id="tree.id"
  12. :data-treelevel="level"
  13. :data-cancheck="(allCanCheck || !tree.children)"
  14. :style="{paddingLeft:(indent*level)+'rpx'}"
  15. @tap.stop="taped">
  16. <view
  17. class="gui-tree-icons gui-color-gray"
  18. :data-id="tree.id"
  19. @tap.stop="fold"
  20. v-if="type == 'text' && isIcon">
  21. <text
  22. class="gui-tree-icons-text gui-icons"
  23. v-if="tree.children">&#xe62d;</text>
  24. </view>
  25. <view
  26. class="gui-tree-icons"
  27. v-if="type == 'radio' && (allCanCheck || !tree.children)">
  28. <text
  29. :class="checkedClass"
  30. class="gui-tree-icons-text gui-icons gui-fade-in"
  31. v-if="tree.id == checkedId">&#xe7f8;</text>
  32. <text class="gui-tree-icons-text gui-icons"
  33. v-else>&#xe762;</text>
  34. </view>
  35. <view
  36. class="gui-tree-icons"
  37. v-if="type == 'checkbox' && (allCanCheck || !tree.children)">
  38. <text
  39. :class="checkedClass"
  40. class="gui-tree-icons-text gui-icons gui-fade-in"
  41. v-if="isChecked(tree.id)">&#xe685;</text>
  42. <text
  43. class="gui-tree-icons-text gui-icons icon-checkbox" v-else>&#xe762;</text>
  44. </view>
  45. <text
  46. class="gui-block gui-tree-title gui-flex1">{{tree.label}}</text>
  47. </view>
  48. <view>
  49. <gui-tree
  50. v-if="arrayIndexOf(notids, tree.id) == -1"
  51. :trees="tree.children"
  52. :indent="indent"
  53. :level="level+1"
  54. :allCanCheck="allCanCheck"
  55. :isIcon="isIcon"
  56. :checkedId="checkedId"
  57. :checkedIds="checkedIds"
  58. :type="type"
  59. :indexes="[indexesIn,index]"
  60. :checkedClass="checkedClass"
  61. @taped="tapbase"></gui-tree>
  62. </view>
  63. </view>
  64. </view>
  65. </template>
  66. <script>
  67. export default{
  68. name : "gui-tree",
  69. props : {
  70. trees : {type:Array, default:function () {return [];}},
  71. indent : {type:Number, default:28},
  72. level : {type:Number, default:0},
  73. type : {type:String, default:'text'},
  74. isIcon : {type:Boolean, default:true},
  75. indexes : {type:Array, default:function () {return ['',0];}},
  76. checkedId : {type:[String, Number], default:'-1'},
  77. checkedIds : {type:Array, default:function () {return [];}},
  78. allCanCheck : {type:Boolean, default:true},
  79. isFold : {type:Boolean, default:true},
  80. checkedClass : {type:Array, default:function () {return ['gui-primary-color'];}}
  81. },
  82. data() {
  83. return {
  84. treesIN : [],
  85. indexesIn : [],
  86. notids : []
  87. }
  88. },
  89. created:function(){
  90. this.treesIN = this.trees;
  91. if(this.indexes[0] != ''){
  92. var indexes = this.indexes[0].split(',');
  93. }else{
  94. var indexes = [];
  95. }
  96. indexes.push(this.indexes[1]);
  97. this.indexesIn = indexes.join(',');
  98. },
  99. watch:{
  100. type : function(){
  101. this.notids = [];
  102. }
  103. },
  104. methods:{
  105. fold : function (e) {
  106. var id = e.currentTarget.dataset.id;
  107. if(this.isFold){
  108. var res = this.arrayIndexOf(this.notids, id);
  109. if(res == -1){
  110. this.notids.push(id);
  111. }else{
  112. this.notids.splice(res,1);
  113. }
  114. }
  115. e.stopPropagation();
  116. return ;
  117. },
  118. taped : function(e){
  119. var treeindexs = e.currentTarget.dataset.treeindexs;
  120. treeindexs = treeindexs.split(',');
  121. var index = e.currentTarget.dataset.index;
  122. treeindexs.push(index);
  123. treeindexs.shift();
  124. if(this.type == 'text'){
  125. this.tapbase(treeindexs);
  126. }else{
  127. var cancheck = e.currentTarget.dataset.cancheck;
  128. if(cancheck){this.tapbase(treeindexs);}
  129. }
  130. e.stopPropagation();
  131. },
  132. tapbase : function(e){
  133. this.$emit('taped', e);
  134. },
  135. setTrees : function (trees) {
  136. this.treesIN = trees;
  137. },
  138. isChecked : function(checkedId){
  139. for(let i = 0; i < this.checkedIds.length; i++){
  140. if(this.checkedIds[i] == checkedId){
  141. return true;
  142. }
  143. }
  144. return false;
  145. },
  146. arrayIndexOf : function(arr, needFind){
  147. var index = -1;
  148. for(let i = 0; i < arr.length; i++){
  149. if(arr[i] == needFind){index = i; return i;}
  150. }
  151. return index;
  152. }
  153. },
  154. emits : ['taped']
  155. }
  156. </script>
  157. <style scoped>
  158. </style>