petritr's avatar
Level 15

Show X(close) icon on search input vue

How can is how an X(close) icon when something is typed in the search input, also would be good the search to close with escape or with click outside the search result.

0 likes
12 replies
petritr's avatar
Level 15

LOL What are you talking man! I need to add close(X) to an input search type. Im not taking about input types. :D

petritr's avatar
Level 15

@D9705996 you are talking for input types html, my question is how to add an close(X) action with vue.js. My question is about Vue.js not about input types HTML.

mornir's avatar

@D9705996 Nice! I didn't know about this. And this is also great for accessibility.

D9705996's avatar

@petritr - I have provided how to add clear icon to a search input which I assumed was what you required. This will work in your VueJS template as well as in vanilla HTML/Blade.

If you need a different behaviour can you please provide more information about exactly what you need and provide your existing code so I can help

petritr's avatar
Level 15

@D9705996 Okay one more time, i need close action (X), escape(key) action close, and click anywhere outside the search, and the search drop down result close action.

I need close action that will give the possibility to close the result from the the search input and the search result drop down in Vue.js

I don't need HTML or CSS solution focused on input and icon or something, i need an Vue.js close action example.

D9705996's avatar

@petritr - you never mentioned this was a drop down. That does change things a bit. Can you please share your code to allow me to see what you are currently doing so I can help

petritr's avatar
Level 15

@D9705996 you can imagine i search for result and somehow they are display somewhere.

By the code you will be even more confused. Thanks for trying to help.

D9705996's avatar

@petritr - there are hundreds, if not thousands of way to achieve what you want but without knowing what you setup already it is impossible to know if they will work for you.

I have previously used Select2 for drop downs that has a lot of flexibility and worked for me. It has all the features you ask for and there is a vue specific plugin

https://sagalbot.github.io/vue-select/

Let me know if this looks like what you are looking for.

petritr's avatar
Level 15

@D9705996 well i don't use an package i have done it alone:

The input:

<input class="form-control" type="search" name="search" v-model="search" placeholder="Search">

The computed property:

computed: {
            filteredLog() {
                return this.logs.filter((log) => {
                    //return customer.name.match(this.search);
                    return `${log.id} ${log.name}`.toLowerCase().includes(this.search.toLowerCase());
                });
            }
        },

here i need to add close action as e mentioned before.

D9705996's avatar

You can bind to the keydown and blur events in your input

<input 
  class="form-control" 
  type="search" 
  name="search" 
  v-model="search" 
  placeholder="Search"
  @keydown.esc="close_search" 
  @blur="close_search" 
>

Then in your vue code.

methods: {
    close_search() {
        // put your close logic here
    }
}

Does this help?

1 like

Please or to participate in this conversation.