list-expand.vue 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <template>
  2. <view class="expandable-list-container">
  3. <!-- 使用作用域插槽,将item数据传回给父组件 -->
  4. <slot :record="data" :items="visibleItems"></slot>
  5. <!-- 当项目总数大于限制时,显示展开/收起按钮 -->
  6. <view v-if="items.length > limit" class="expand-trigger" @click.stop="toggleExpand">
  7. <text>{{ isExpanded ? '收起' : '展开查看' }}</text>
  8. <!-- <u-icon :name="isExpanded ? 'arrow-up' : 'arrow-down'" color="#909399" size="12"></u-icon> -->
  9. <image :src="`/static/order/${isExpanded ? 'hidden' : 'show'}.png`"></image>
  10. </view>
  11. </view>
  12. </template>
  13. <script>
  14. export default {
  15. name: 'ExpandableList',
  16. props: {
  17. // 兼容小程序,slot内如果使用到父级变量(此变量正好是遍历出来的),会取不到值
  18. data: {
  19. type: Object,
  20. default: () => ({})
  21. },
  22. // 接收完整的列表数据
  23. items: {
  24. type: Array,
  25. default: () => []
  26. },
  27. // 默认显示的条目数量
  28. limit: {
  29. type: Number,
  30. default: 2
  31. }
  32. },
  33. data() {
  34. return {
  35. isExpanded: false // 内部维护的展开状态
  36. };
  37. },
  38. computed: {
  39. // 根据展开状态,计算出实际应该显示的列表
  40. visibleItems() {
  41. if (this.isExpanded) {
  42. return this.items; // 展开时,返回所有项目
  43. }
  44. return this.items.slice(0, this.limit); // 折叠时,返回限制数量的项目
  45. }
  46. },
  47. methods: {
  48. // 切换展开/收起状态
  49. toggleExpand() {
  50. this.isExpanded = !this.isExpanded;
  51. }
  52. }
  53. };
  54. </script>
  55. <style lang="scss" scoped>
  56. .expand-trigger {
  57. font-size: 24rpx;
  58. color: #2D88F4;
  59. padding: 16rpx 0;
  60. display: flex;
  61. justify-content: center;
  62. align-items: center;
  63. text {
  64. margin-right: 8rpx;
  65. }
  66. image {
  67. width: 20rpx;
  68. height: 20rpx;
  69. }
  70. }
  71. </style>