checkboxTree.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <template>
  2. <view>
  3. <form v-for="(item, index) in selectList" :key="index">
  4. <!-- 一级-->
  5. <view class="cu-form-group " :class="index==0 ? '' :'margin-top-sm'" >
  6. <checkbox-group @change="chooseAll(item,index)">
  7. <checkbox :class="item.checked?'checked':''" :checked="item.checked" class="round"></checkbox>
  8. <text class="title margin-left-sm">{{item.name}}</text>
  9. </checkbox-group>
  10. <view>
  11. <text v-if="item.show==true" class="cuIcon-fold" @click="changeShow(index,item)"></text>
  12. <text v-else class="cuIcon-unfold" @click="changeShow(index,item)"></text>
  13. </view>
  14. </view>
  15. <!-- 二级 -->
  16. <view v-if="item.show && item.children" v-for="(items, indexs) in item.children" :key="indexs" class="cu-form-group " >
  17. <checkbox-group class="margin-left" @change="chooseOne(index,items)">
  18. <checkbox :class="items.checked?'checked':''" :checked="items.checked" class="round"></checkbox>
  19. <text v-if="items.name" class="title margin-left-sm">{{items.name}}</text>
  20. <text v-else class="title margin-left-sm">{{items.user_name}}</text>
  21. </checkbox-group>
  22. </view>
  23. </form>
  24. </view>
  25. </template>
  26. <script>
  27. export default {
  28. props: {
  29. selectList: {
  30. type: Array,
  31. default: [
  32. {
  33. name: '施工方',
  34. checked:false,//是否选中
  35. show:false,//是否展开,展开为子级
  36. children: [{name: '姓名1',checked:false,}, { name: '姓名2',checked:false,}]
  37. }
  38. ]
  39. },
  40. },
  41. data() {
  42. return {
  43. }
  44. },
  45. methods: {
  46. changeShow(index,item){//显示子节点
  47. if(item.show){
  48. this.$set(this.selectList[index], 'show', false)
  49. }else{
  50. this.$set(this.selectList[index], 'show', true)
  51. }
  52. },
  53. chooseAll(item, index){//全选
  54. if (this.selectList[index].checked) {
  55. this.$set(this.selectList[index], 'checked', false)
  56. this.selectList[index].children.forEach(item => {
  57. item.checked = false
  58. })
  59. } else {
  60. this.$set(this.selectList[index], 'checked', true)
  61. this.selectList[index].children.forEach(item => {
  62. item.checked = true
  63. })
  64. }
  65. this.select();
  66. },
  67. chooseOne(index,items){//单选
  68. this.$set(this.selectList[index], 'checked', false)//取消父级级选中
  69. if(items.checked) {
  70. this.$set(items, 'checked', false)
  71. } else {
  72. this.$set(items, 'checked', true)
  73. }
  74. this.select()
  75. },
  76. select(){
  77. var i=0;
  78. this.selectList.forEach(item => {
  79. if (item.children) {
  80. item.children.forEach(item2 => {
  81. if (item2.checked) {
  82. i=i+1
  83. }
  84. })
  85. }
  86. })
  87. //全局事件订阅只要注册的页面都可以收到回调值
  88. uni.$emit('num',i)
  89. }
  90. },
  91. }
  92. </script>
  93. <style>
  94. .margin-left-sm{
  95. margin-left: 10rpx;
  96. }
  97. .margin-left{
  98. margin-left:60rpx;
  99. }
  100. </style>