Vesko_dev
Vesko_dev

Reputation: 436

Javascript - How to search no matter upperCase or lowercase in Vue.js?

I m using Vue.js 3 and here I have search in data, but this data is from a fake API. So this search works only for small or big characters, but I want to search throw data no matter uppercase or lowercase? Here is my code

<template>
  <div class='home'>
      <h1>Third</h1>
      <div v-if="error"> {{error}} </div>
      <div v-if="posts.length">
         <input type="text" v-model.trim="search" />
         <!-- <p>search term - {{search}}</p> -->
          <div v-for="post in matchingNames" :key='post.id'>
            <h3>{{post.title }}</h3>
            <h1>{{post.id}}</h1>
          </div>
      </div>
    <div v-else> Loading...</div>
  </div>
</template>

<script>
import { computed, ref, watch, watchEffect} from 'vue'
// import Listing from '../components/Listing.vue' 
import getPosts from '../composables/getPosts'

export default {
    name:'Third',
    components: {} ,
    setup(){ 

      const search = ref('')

      const{posts,error,load} = getPosts()

      load()

      const matchingNames = computed (()=> {  
      return posts.value.filter((post)=>post.title.match(search.value)) 
    })
     

      return {posts, error, search, matchingNames}
    }
}
</script>

<style>

</style>

I also tried to replace match method with includes js method but the result is the same

This is a gif of how that now works, can anyone help me I'm new at Vue.js 3

enter image description here

Upvotes: 0

Views: 1339

Answers (1)

tauzN
tauzN

Reputation: 7011

Convert both to lowercase first :)

...
return posts.value.filter((post)=>post.title.toLowerCase().includes(search.value.toLowerCase()))
...

Upvotes: 2

Related Questions