若依 ruoyi 默认显示搜索框 保留搜索条件 HeaderSearch/index.vue

HeaderSearch/index.vue

修改部分代码

<template>
  <div :class="{'show':show}" class="header-search">
    <svg-icon class-name="search-icon" icon-class="search" @click.stop="click" />
    <el-select
      ref="headerSearchSelect"
      v-model="search"
      :remote-method="querySearch"
      filterable
      default-first-option
      remote
      placeholder="菜单快捷搜索"
      class="header-search-select"
      @change="change"
    >
      <el-option v-for="option in options" :key="option.item.path" :value="option.item" :label="option.item.title.join(' > ')" />
    </el-select>
  </div>
</template>

<script>
// fuse is a lightweight fuzzy-search module
// make search results more in line with expectations
import Fuse from 'fuse.js/dist/fuse.min.js'
import path from 'path'

export default {
  name: 'HeaderSearch',
  data() {
    return {
      search: '',
      options: [],
      searchPool: [],
      show: true,//右上角搜索框 默认开启
      fuse: undefined
    }
  },
  computed: {
    routes() {
      return this.$store.getters.permission_routes
    }
  },
  watch: {
    routes() {
      this.searchPool = this.generateRoutes(this.routes)
    },
    searchPool(list) {
      this.initFuse(list)
    },
    show(value) {
      if (value) {
        document.body.addEventListener('click', this.close)
      } else {
        document.body.removeEventListener('click', this.close)
      }
    }
  },
  mounted() {
    this.searchPool = this.generateRoutes(this.routes)
  },
  methods: {
    click() {
      this.show = !this.show
      if (this.show) {
        this.$refs.headerSearchSelect && this.$refs.headerSearchSelect.focus()
      }
    },
    close() {
      this.$refs.headerSearchSelect && this.$refs.headerSearchSelect.blur()
      // this.options = [] //清空搜索结果;注释掉:保留上一次的搜索结果
      //this.show = false //false 关闭搜索框;注释掉:搜索框停留
    },
    change(val) {
      const path = val.path;
      if(this.ishttp(val.path)) {
        // http(s):// 路径新窗口打开
        const pindex = path.indexOf("http");
        window.open(path.substr(pindex, path.length), "_blank");
      } else {
        this.$router.push(val.path)
      }
      //this.search = '' //清空搜索词;注释掉:保留上一次的搜索词
      //this.options = [] //清空搜索结果;注释掉:保留上一次的搜索结果
      this.$nextTick(() => {
        //this.show = false //false 关闭搜索框;注释掉:搜索框停留
      })
    },
    initFuse(list) {
      this.fuse = new Fuse(list, {
        shouldSort: true,
        threshold: 0.4,
        location: 0,
        distance: 100,
        minMatchCharLength: 1,
        keys: [{
          name: 'title',
          weight: 0.7
        }, {
          name: 'path',
          weight: 0.3
        }]
      })
    },
    // Filter out the routes that can be displayed in the sidebar
    // And generate the internationalized title
    generateRoutes(routes, basePath = '/', prefixTitle = []) {
      let res = []

      for (const router of routes) {
        // skip hidden router
        if (router.hidden) { continue }

        const data = {
          path: !this.ishttp(router.path) ? path.resolve(basePath, router.path) : router.path,
          title: [...prefixTitle]
        }

        if (router.meta && router.meta.title) {
          data.title = [...data.title, router.meta.title]

          if (router.redirect !== 'noRedirect') {
            // only push the routes with title
            // special case: need to exclude parent router without redirect
            res.push(data)
          }
        }

        // recursive child routes
        if (router.children) {
          const tempRoutes = this.generateRoutes(router.children, data.path, data.title)
          if (tempRoutes.length >= 1) {
            res = [...res, ...tempRoutes]
          }
        }
      }
      return res
    },
    querySearch(query) {
      if (query !== '') {
        this.options = this.fuse.search(query)
      } else {
        this.options = []
      }
    },
    ishttp(url) {
      return url.indexOf('http://') !== -1 || url.indexOf('https://') !== -1
    }
  }
}
</script>

<style lang="scss" scoped>
.header-search {
  font-size: 0 !important;

  .search-icon {
    cursor: pointer;
    font-size: 18px;
    vertical-align: middle;
  }

  .header-search-select {
    font-size: 18px;
    transition: width 0.2s;
    width: 0;
    overflow: hidden;
    background: transparent;
    border-radius: 0;
    display: inline-block;
    vertical-align: middle;

    ::v-deep .el-input__inner {
      border-radius: 0;
      border: 0;
      padding-left: 0;
      padding-right: 0;
      box-shadow: none !important;
      border-bottom: 1px solid #d9d9d9;
      vertical-align: middle;
    }
  }

  &.show {
    .header-search-select {
      width: 210px;
      margin-left: 10px;
    }
  }
}
</style>

默认展开搜索框

不关闭搜索框

保留上一次的搜索结果

### 关于 RuoYi 项目的搜索框实现 在 RuoYi 项目中,搜索框通常用于前端页面的数据筛选和查询操作。具体来说,在 `ruoyi-ui` 前端部分实现了基于 Vue.js 的数据表格组件中的搜索功能。 #### 搜索框的基本实现原理 搜索框的实现主要依赖于 Vue 组件化开发模式以及 Axios 请求库来完成前后端交互。当用户输入关键字并触发提交事件时,前端会收集表单数据并通过 HTTP GET 或 POST 方法发送至服务器接口获取过滤后的结果集[^1]。 ```javascript // 示例:定义一个简单的搜索方法 methods: { handleSearch() { const params = this.queryParams; // 获取当前页码和其他参数 listUser(params).then(response => { // 调用 API 接口函数 this.loading = false; this.userList = response.rows; this.total = response.total; }); } } ``` 此代码片段展示了如何封装一个名为 `handleSearch()` 的方法来进行搜索请求处理,并更新视图上的列表显示内容。 #### 动态路由下的懒加载优化 考虑到大型应用可能存在的多个模块及其各自的子页面,为了提升用户体验度与首屏渲染效率,RuoYi 框架采用了 Webpack 提供的异步加载特性——动态导入 (import()) 来延迟加载不常用的组件文件。对于包含复杂业务逻辑或大量资源消耗的操作界面而言尤为重要[^4]。 例如: ```javascript { path: '/system/user', name: 'SystemUser', component: () => import('@/views/system/user/index'), // 使用 ES6 Module 动态引入语法 meta: { title: '用户管理', icon: 'people' } }, ``` 这种做法不仅减少了初次进入网站所需下载的整体体积,还能够显著改善特定场景下用户的响应时间体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值