• vue可展开/收缩搜索条件且支持自适应功能


    需求描述

    有个搜索栏,总共有7个搜索条件(可想象为7个input输入框)支持搜索条件的展开与收缩,在A分辨率下,默认显示2个,隐藏5个。B分辨率下默认显示3个,隐藏4个。点击展开时则全部显示,点击收缩则回到默认状态

    预期的效果

    代码实现

    1. <script>
    2. export default {
    3. name: 'AdvSearch',
    4. data() {
    5. return {
    6. isSmall: false,
    7. forceHidden: true,
    8. }
    9. },
    10. computed: {
    11. spanSize() {
    12. if (this.isSmall) {
    13. return 8
    14. } else {
    15. return 6
    16. }
    17. },
    18. },
    19. mounted() {
    20. console.log(this.$children[0].$children)
    21. const smallMedia = window.matchMedia('(max-width: 1600px)')
    22. this.listenerSmallMedia(smallMedia)
    23. smallMedia.addEventListener('change', this.listenerSmallMedia)
    24. },
    25. methods: {
    26. listenerSmallMedia(smallMedia) {
    27. console.log('listenerSmallMedia')
    28. if (smallMedia.matches) {
    29. this.isSmall = true
    30. } else {
    31. this.isSmall = false
    32. }
    33. },
    34. needShow(idx) {
    35. if (!this.forceHidden || (!this.isSmall && idx < 4)) {
    36. // 如果不是强制隐藏 或 (不是小分辨率且当前项的索引号小于4)则显示
    37. return true
    38. } else {
    39. // 其他情况都隐藏
    40. return false
    41. }
    42. },
    43. },
    44. }
    45. script>
    46. <template>
    47. <div>
    48. <h3>是否小分辨率:{{ isSmall }}h3>
    49. <el-row :gutter="20">
    50. <el-col ref="col" :span="spanSize">
    51. <div class="grid-content bg-purple">条件1div>
    52. el-col>
    53. <el-col ref="col" :span="spanSize">
    54. <div class="grid-content bg-purple">条件2div>
    55. el-col>
    56. <el-col v-if="needShow(3)" ref="col" :span="spanSize">
    57. <div class="grid-content bg-purple">条件3div>
    58. el-col>
    59. <el-col v-if="needShow(4)" ref="col" :span="spanSize">
    60. <div class="grid-content bg-purple">条件4div>
    61. el-col>
    62. <el-col v-if="needShow(5)" ref="col" :span="spanSize">
    63. <div class="grid-content bg-purple">条件5div>
    64. el-col>
    65. <el-col v-if="needShow(6)" ref="col" :span="spanSize">
    66. <div class="grid-content bg-purple">条件6div>
    67. el-col>
    68. <el-col v-if="needShow(7)" ref="col" :span="spanSize">
    69. <div class="grid-content bg-purple">条件7div>
    70. el-col>
    71. <el-col ref="col" :span="spanSize">
    72. <div class="grid-content bg-purple">
    73. <el-button @click="forceHidden = false">展开el-button>
    74. <el-button @click="forceHidden = true">收缩el-button>
    75. div>
    76. el-col>
    77. el-row>
    78. div>
    79. template>
    80. <style lang="scss" scoped>
    81. .el-row {
    82. margin-bottom: 20px;
    83. &:last-child {
    84. margin-bottom: 0;
    85. }
    86. }
    87. .el-col {
    88. border-radius: 4px;
    89. }
    90. .bg-purple-dark {
    91. background: #99a9bf;
    92. }
    93. .bg-purple {
    94. background: #d3dce6;
    95. }
    96. .bg-purple-light {
    97. background: #e5e9f2;
    98. }
    99. .grid-content {
    100. border-radius: 4px;
    101. min-height: 36px;
    102. margin-bottom: 20px;
    103. }
    104. .row-bg {
    105. padding: 10px 0;
    106. background-color: #f9fafc;
    107. }
    108. .forceHide {
    109. display: none;
    110. }
    111. style>
  • 相关阅读:
    大彩串口屏读写文件问题
    Flutter中GetX系列二--Snackbar基本使用(顶部弹窗)
    五个DIY表情背后的故事
    【Android笔记44】Android利用DrawerLayout布局组件实现侧滑导航菜单的效果
    un7.29:Linux——如何在docker中安装tomcat?
    【机器学习】机器学习知识点全面总结(监督学习+无监督学习)
    万界星空科技云MES系统生产全流程追溯功能介绍
    postgresql 创建listen notify .net core6.0监听连接
    嵌入式学习笔记(48)什么是I2C通信
    7000字详解Spring Boot项目集成RabbitMQ实战以及坑点分析
  • 原文地址:https://blog.csdn.net/sinat_17775997/article/details/126336202