list-view-children-if-show.uvue 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <template>
  2. <view class="p-10">
  3. <button id="init-btn" class="uni-btn" @click="init">init</button>
  4. <button id="clear-btn" class="uni-btn" @click="clear">clear</button>
  5. <view
  6. class="uni-common-mt list-view-container"
  7. :class="{ ' p-10': list.length > 0 }"
  8. >
  9. <list-view>
  10. <template v-for="(item, index) in list">
  11. <list-item
  12. id="toggle-children-show-btn"
  13. :class="{ 'uni-common-mt': index > 0 }"
  14. @click="toggleChildrenShow(item.id)"
  15. >
  16. <text>toggle children isShow</text>
  17. </list-item>
  18. <template v-if="expandIds.includes(item.id)">
  19. <template v-for="child in item.children">
  20. <list-item id="list-item-child" class="mt-5">
  21. <text>{{ child.id }}</text>
  22. </list-item>
  23. </template>
  24. </template>
  25. </template>
  26. </list-view>
  27. </view>
  28. </view>
  29. </template>
  30. <script>
  31. type Child = {
  32. id: string
  33. }
  34. type List = {
  35. id: string
  36. children: Child[]
  37. }
  38. export default {
  39. data(){
  40. return {
  41. list: [] as List[],
  42. expandIds: [] as string[],
  43. }
  44. },
  45. onLoad() {
  46. this.init()
  47. },
  48. methods: {
  49. init(){
  50. this.list = [
  51. {id:'1', children: [{ id: '1-1'},{ id: '1-2'},{ id: '1-3'}]},
  52. {id:'2', children: [{ id: '2-1'},{ id: '2-2'},{ id: '2-3'}]},
  53. {id:'3', children: [{ id: '3-1'},{ id: '3-2'},{ id: '3-3'}]},
  54. ] as List[]
  55. this.expandIds = [this.list[0].id]
  56. },
  57. clear(){
  58. this.list = [] as List[]
  59. this.expandIds = [] as string[]
  60. },
  61. toggleChildrenShow(id: string){
  62. const index = this.expandIds.findIndex((item) => item == id)
  63. if(index == -1) {
  64. this.expandIds.push(id)
  65. } else {
  66. this.expandIds.splice(index, 1)
  67. }
  68. },
  69. }
  70. }
  71. </script>
  72. <style>
  73. .p-10 {
  74. padding: 10px;
  75. }
  76. .list-view-container {
  77. background-color: #fff;
  78. }
  79. .mt-5 {
  80. margin-top: 5px;
  81. }
  82. </style>