vue3 setup语法糖 多条件搜索(带时间范围)

简介: vue3 setup语法糖 多条件搜索(带时间范围)

前言:

不管哪个后台管理中都会用到对条件搜索带有时间范围的也不少见接下来就跟着我步入vue的多条件搜索(带时间范围)

Vue 3 中,你可以使用 setup 函数来处理多条件搜索和时间范围的情况。下面是一个示例,展示了如何在 Vue 3 中使用 setup 处理带有多条件搜索和时间范围的场景:


setup介绍:

在 Vue 3 中,setup 是用于配置组件选项的函数。它是 Vue 3 中引入的新特性,作为组件的一部分,用于设置组件的响应式数据、计算属性、方法等。

setup 函数是在组件创建过程中被调用的,在组件实例创建之前执行,用于初始化组件的数据和方法。它接收两个参数:propscontext

  • props:包含组件接收的属性。在 setup 函数内部,可以直接使用 props 对象中的属性,或者使用解构赋值获取特定的属性。
  • context:包含一些额外的信息,例如 attrs(非响应式的属性)、slots(用于处理插槽内容)、emit(用于触发事件)等。


setup 函数中,你可以通过使用 Vue 提供的函数(例如 refreactivecomputed 等)创建响应式数据,并将其返回以供模板使用。此外,也可以在 setup 函数内定义组件的方法,并将其暴露给模板部分。


setup用法:

下面是一个简单的示例,展示了如何在 Vue 3 中使用 setup 函数:

<template>
  <div>
    <p>{{ message }}</p>
    <button @click="increment">Increment</button>
  </div>
</template>
<script>
import { ref } from 'vue';
export default {
  setup() {
    // 创建一个响应式的变量
    const message = ref('Hello, Vue 3!');
    // 定义一个方法
    const increment = () => {
      message.value += '!';
    };
    // 返回数据和方法,使其可在模板中使用
    return {
      message,
      increment,
    };
  },
};
</script>


在这个示例中,setup 函数创建了一个名为 message 的响应式变量,并定义了一个名为 increment 的方法。这些数据和方法通过 return 语句暴露给模板,以便在模板中使用。


vue3多条件搜索:

Vue 3 提供了一种语法糖,允许您使用 setup 函数来更简洁地设置组件的响应式数据和生命周期钩子。在 Vue 3 中,您可以使用 ref 和 reactive 函数来创建响应式数据,并使用 onMounted、onUpdated 等函数来处理生命周期钩子。


对于多条件搜索并带有时间范围的搜索,您可以结合使用 Vue 3 的响应式数据和生命周期钩子来实现。下面是一个示例代码,展示了如何使用 Vue 3 的 setup 函数来实现这个功能:


当涉及到在 Vue 3 中实现多条件搜索,包括时间范围时,你可以使用 setup 函数和响应式变量来处理这些条件。以下是一个示例,演示了如何在 Vue 3 中处理带有多条件搜索和时间范围的情况:


假设你有一个搜索功能,其中包含关键词搜索、状态筛选和时间范围选择。以下是一个简单的示例:

<template>
  <div>
    <input v-model="searchKeyword" placeholder="Search...">
    <select v-model="selectedStatus">
      <option value="active">Active</option>
      <option value="inactive">Inactive</option>
    </select>
    <input type="date" v-model="startDate">
    <input type="date" v-model="endDate">
    <button @click="search">Search</button>
    <!-- Display search results here -->
    <ul>
      <li v-for="item in filteredItems" :key="item.id">{{ item.name }}</li>
    </ul>
  </div>
</template>
<script>
import { ref, computed } from 'vue';
export default {
  setup() {
    const searchKeyword = ref('');
    const selectedStatus = ref('active');
    const startDate = ref(null);
    const endDate = ref(null);
    // Replace this with your actual data source
    const items = ref([
      { id: 1, name: 'Item 1', status: 'active', date: '2023-01-01' },
      { id: 2, name: 'Item 2', status: 'inactive', date: '2023-02-01' },
      // Other items...
    ]);
    const search = () => {
      // Logic for filtering based on searchKeyword, selectedStatus, startDate, endDate
    };
    const filteredItems = computed(() => {
      // Implement your filtering logic here based on searchKeyword, selectedStatus, startDate, endDate
      return items.value.filter(item => {
        const keywordMatch = item.name.toLowerCase().includes(searchKeyword.value.toLowerCase());
        const statusMatch = item.status === selectedStatus.value;
        let dateMatch = true;
        if (startDate.value && endDate.value) {
          dateMatch = item.date >= startDate.value && item.date <= endDate.value;
        }
        return keywordMatch && statusMatch && dateMatch;
      });
    });
    return {
      searchKeyword,
      selectedStatus,
      startDate,
      endDate,
      filteredItems,
      search
    };
  }
};
</script>


介绍:

在这个示例中,我们使用了 ref 来创建响应式数据,并使用 computed 创建了一个计算属性 filteredItems 来根据搜索条件过滤项目列表。搜索条件包括关键词搜索、状态筛选和时间范围选择。setup 函数返回了这些变量和一个用于执行搜索的函数。

需要注意的是,上面的示例中仅包含了搜索逻辑的框架结构,你需要根据实际的业务需求和数据源来编写适合的过滤逻辑和数据操作。

这是一个基本的示例,你可以根据自己的需求进行调整和扩展。


vue2多条件搜索:


简介:

在 Vue 2 中实现多条件搜索,包括时间范围,原理与 Vue 3 中的类似。你可以使用 Vue 2 提供的数据绑定、计算属性和方法来处理多条件搜索的功能。

以下是一个示例,展示了如何在 Vue 2 中实现带有多条件搜索和时间范围的功能:

示例:

<template>
  <div>
    <input v-model="searchKeyword" placeholder="Search...">
    <select v-model="selectedStatus">
      <option value="active">Active</option>
      <option value="inactive">Inactive</option>
    </select>
    <input type="date" v-model="startDate">
    <input type="date" v-model="endDate">
    <button @click="search">Search</button>
    <!-- Display search results here -->
    <ul>
      <li v-for="item in filteredItems" :key="item.id">{{ item.name }}</li>
    </ul>
  </div>
</template>
<script>
export default {
  data() {
    return {
      searchKeyword: '',
      selectedStatus: 'active',
      startDate: null,
      endDate: null,
      // Replace this with your actual data source
      items: [
        { id: 1, name: 'Item 1', status: 'active', date: '2023-01-01' },
        { id: 2, name: 'Item 2', status: 'inactive', date: '2023-02-01' },
        // Other items...
      ]
    };
  },
  computed: {
    filteredItems() {
      return this.items.filter(item => {
        const keywordMatch = item.name.toLowerCase().includes(this.searchKeyword.toLowerCase());
        const statusMatch = item.status === this.selectedStatus;
        let dateMatch = true;
        if (this.startDate && this.endDate) {
          dateMatch = item.date >= this.startDate && item.date <= this.endDate;
        }
        return keywordMatch && statusMatch && dateMatch;
      });
    }
  },
  methods: {
    search() {
      // Logic for filtering based on searchKeyword, selectedStatus, startDate, endDate
    }
  }
};
</script>


思路:

在这个示例中,我们使用了 Vue 2 的数据绑定和计算属性。data 函数中定义了存储搜索条件和数据的响应式属性。computed 计算属性 filteredItems 用于根据搜索条件进行过滤,并返回符合条件的项。methods 中定义了执行搜索的方法 search

filteredItems 计算属性中,根据搜索关键词、状态和时间范围对 items 进行筛选,仅返回符合条件的项。

技术交流邮箱:binghailianyv@foxmail.com

原创:冰海恋雨.

b803c8a1ba6847bebabf41f7b05fd592.jpg

相关文章
|
11月前
|
JavaScript 前端开发 安全
Vue 3
Vue 3以组合式API、Proxy响应式系统和全面TypeScript支持,重构前端开发范式。性能优化与生态协同并进,兼顾易用性与工程化,引领Web开发迈向高效、可维护的新纪元。(238字)
1294 139
|
11月前
|
缓存 JavaScript 算法
Vue 3性能优化
Vue 3 通过 Proxy 和编译优化提升性能,但仍需遵循最佳实践。合理使用 v-if、key、computed,避免深度监听,利用懒加载与虚拟列表,结合打包优化,方可充分发挥其性能优势。(239字)
684 1
|
12月前
|
开发工具 iOS开发 MacOS
基于Vite7.1+Vue3+Pinia3+ArcoDesign网页版webos后台模板
最新版研发vite7+vue3.5+pinia3+arco-design仿macos/windows风格网页版OS系统Vite-Vue3-WebOS。
1191 11
|
11月前
|
JavaScript 安全
vue3使用ts传参教程
Vue 3结合TypeScript实现组件传参,提升类型安全与开发效率。涵盖Props、Emits、v-model双向绑定及useAttrs透传属性,建议明确声明类型,保障代码质量。
758 0
|
缓存 前端开发 大数据
虚拟列表在Vue3中的具体应用场景有哪些?
虚拟列表在 Vue3 中通过仅渲染可视区域内容,显著提升大数据列表性能,适用于 ERP 表格、聊天界面、社交媒体、阅读器、日历及树形结构等场景,结合 `vue-virtual-scroller` 等工具可实现高效滚动与交互体验。
1212 1
|
缓存 JavaScript UED
除了循环引用,Vue3还有哪些常见的性能优化技巧?
除了循环引用,Vue3还有哪些常见的性能优化技巧?
626 0
|
JavaScript
vue3循环引用自已实现
当渲染大量数据列表时,使用虚拟列表只渲染可视区域的内容,显著减少 DOM 节点数量。
331 0
|
JavaScript
Vue中如何实现兄弟组件之间的通信
在Vue中,兄弟组件可通过父组件中转、事件总线、Vuex/Pinia或provide/inject实现通信。小型项目推荐父组件中转或事件总线,大型项目建议使用Pinia等状态管理工具,确保数据流清晰可控,避免内存泄漏。
877 2
|
11月前
|
缓存 JavaScript
vue中的keep-alive问题(2)
vue中的keep-alive问题(2)
749 137
|
人工智能 JavaScript 算法
Vue 中 key 属性的深入解析:改变 key 导致组件销毁与重建
Vue 中 key 属性的深入解析:改变 key 导致组件销毁与重建
1621 0