gui-tree.vue 3.9 KB

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