"The issue with the quasar q-select filter event not functioning properly requires a shared function to be used

Trying out this event filtering example

This can be successful

Index.vue

<template>
  <q-select
    :options="options"
    @filter="filter"
    v-model="model"
    use-input
  ></q-select>
</template>

<script>
import { defineComponent } from "vue";
import { computed, reactive, toRefs, ref } from "vue";
const selectedOptions = ["Google", "Facebook", "Twitter", "Apple", "Oracle"];

export default defineComponent({
  name: "PageIndex",
  setup() {
    let state = reactive({
      model: null,
      options: selectedOptions,
    });

    const filter = (val, update, abort) => {
      update(() => {
        const needle = val.toLowerCase()
        state.options = selectedOptions.filter(v => v.toLowerCase().indexOf(needle) > -1)
      })
    }

    return {
      filter,
      selectedOptions,
      ...toRefs(state),
    };
  },
});
</script>

Attempting to implement a common function from utils/filter.js without success

Index.vue

<template>
  <q-select
    use-input
    :options="options"
    @filter="
      (val, update, abort) =>
        filter(val, update, abort, selectedOptions, options)
    "
    v-model="model"
  >  </q-select>
</template>

<script>
import { defineComponent } from "vue";
import { computed, reactive, toRefs, ref } from "vue";
import { filter } from "../utils/filter";
const selectedOptions = ["Google", "Facebook", "Twitter", "Apple", "Oracle1"];

export default defineComponent({
  name: "PageIndex",
  setup() {
    let state = reactive({
      model: null,
      options: selectedOptions,
    });

    return {
      filter,
      selectedOptions,
      ...toRefs(state),
    };
  },
});
</script>

utils/filter.js

export function filter(val, update, abort, from, to) {
  update(() => {
    const needle = val.toLowerCase()
    to.value = from.filter((v) => v.toLowerCase().indexOf(needle) > -1)
  })
}

How can we make the filtering work effectively?

Working version https://codesandbox.io/s/blissful-brattain-vd085n?file=/src/pages/Index.vue

Not working version https://codesandbox.io/s/blissful-brattain-vd085nv2-forked-feiv7v?file=/src/pages/Index.vue

Answer №1

finally passed this to a function and made changes

@filter="(val, update, abort) => filter(val, update, abort, selectedOptions, 'options' ,this)"

filter.js

export const filter = (val, update, abort, from, key, vm) => {
  update(() => {
    const needle = val.toLowerCase()
    vm[key] = from.filter((v) => v.toLowerCase().indexOf(needle) > -1)
  })
}

Following best practices after inquiring about version 2

@filter="filterHandler"

const filterHandler = (val, update) => {
      update(() => {
        options.value = filter(val, oriOptions)
      })
    }

//filter.js
export const filter = (val, options) => {
  if (val === '') return options
  const needle = val.toLowerCase()
  return options.filter((v) => v.toLowerCase().indexOf(needle) > -1)
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the method for including HTML special characters in d3.js?

I am attempting to incorporate the degree symbol in my HTML code using ° const degreeNum = d3 .select(`#windBox${order}`) .append("text") .attr("x", 250) .attr("y", 130) .style("font", "bold 50px sans-serif") ...

The HTML5 onclick function is not functioning properly on the button

My HTML file includes a button with the id "botonRegistro" that should call a function when clicked. However, nothing happens and no alerts are displayed. I'm not sure why this button is not working while other buttons do. <!DOCTYPE html> <h ...

Can you suggest the optimal setup for (javascript SOAP)?

As part of my nightly maintenance process on a LAMP server, I need to retrieve data from a web service using SOAP, which is then applied to a database. While researching various options, I have become overwhelmed by the abundance of information available. ...

JavaScript - rearrange array of objects based on specified property

I am currently designing a select dropdown input element for a webpage and I want to create a specific 'popular' options group that will be displayed at the top of the dropdown menu. The data structure I am working with is as follows. I am tryi ...

Guide on removing the parent JSON array while retaining the child JSON array

I have been searching for a solution to remove specific JSON array elements, but I have not found one yet. Here is the JSON data I am working with: [ { "KODE": [ { "name": "kode", "value": [ ...

Modifying the content in one form field based on the information entered in another input field

I have a scheduling application that includes a form for selecting both the departure city and arrival city. The app is designed for international travel only, so when a user selects a city from Hungary as the departure city, I want to exclude all Hungaria ...

What is causing the race condition in the order of AngularJS directive listeners?

I am currently facing an issue with a custom directive that is listening to change events on input elements using jQuery's delegate function. The problem arises in Chrome where the listener fires before the ng-models update, resulting in stale data wi ...

React Native can trigger a press event, as long as it is not within

My situation involves triggering an action when clicking on the parent component (TouchableOpacity, for example), but not triggering anything when clicking on the children components (Screen and others). It's similar to preventing bubbling on the web. ...

Exploring the Possibilities of Message Embeds in Discord using JavaScript

My goal is to teach my bot how to scan the description of embeds for a specific phrase. After reviewing the documentation at https://discord.js.org/#/docs/main/v11/class/MessageEmbed?scrollTo=description, it appears that I need to implement code similar to ...

How can JavaScript pass a variable through the URL?

I am attempting to pass a variable through the URL: http://localhost/new_wiki/test.php?id=http://example.com In my code, I have var first = getUrlVars()["id"];. This line is supposed to pass the value but it doesn't seem to be working. Can someone pl ...

What is the best method to eliminate whitespace from array values using JavaScript and jQuery?

When I extract a number value from an array and append it to the DOM, there is an unwanted blank space before the value that needs to be removed. The desired result should look like this. data-filter-class="["4"]" for (var i=0, len=str ...

Ensuring that the text box only accepts the letters A, B, and C is essential for

Could you please help me with validating a text box? I would like to set it so that the user can only enter either A, B, or C. If they input D to Z or any other characters, I want a popup message to appear asking them to Enter A, B, or C. Would you recom ...

How can I pass a DOM element as a prop in Vue 3?

As someone who is brand new to Vue, I'm exploring the possibilities. Although it's not something I typically do, I believe achieving a similar outcome in React + JSX (untested) could look like this: render() { const el = <p>Blah <a hre ...

Implement a mouseenter event to all input elements that have specific names stored in an array, utilizing jQuery

I'm struggling to figure out how to apply my function to all input elements with a name that is included in my array. This is what I've attempted so far: var names = ["name1", "name2"]; $(document).ready(function(){ $('input[name=names[ ...

The usage of nextTick in Vue.js and its role in updating components

Although I am a beginner with vue.js and have a basic understanding of it, I came across a sample code utilizing nextTick() today. Trying to comprehend its purpose led me to explore the documentation, which ended up complicating things further and leavin ...

"Enhancing user experience: dynamically adding rows using a combo of jquery, ajax, and php

This is the layout of my table. Here is the result I'm getting. Below is the code snippet: <table width="100%" id="controltable" border="1"> <tr> <th> Product Name </th> <th> Product Pri ...

How to change a CSS 'left' property using Jquery or Javascript

Upon examining my DOM, I found the following element: <div id="itemEditor" class="quoteItemEditorView partType_MATERIAL editMode selectorEnabled" style="left: -1px; right: 0px; width: auto; min-width: 480px; display: block;" > I have b ...

Leveraging Tealium Tags for Enhanced VueJs Nuxt Tracking

I'm currently exploring the integration of Tealium Tags into my Nuxt.js application. Despite consulting the provided documentation and various StackOverflow threads, I'm struggling to determine where exactly in my Nuxt application all the code sh ...

Express.js problem: Unable to load CSS and JS files from the 'public' directory

Currently, I am working on a web project with Express.js and encountering difficulties in loading CSS and JavaScript files from the 'public' folder. Despite following common practices, it seems that something is amiss. Here is the layout of my pr ...

Troubleshooting: ng-disabled feature is not properly functioning with Bootstrap buttons

I am currently using a combination of bootstrap.js and angular js in my project. The code snippet I have is as follows: //snippet from the controller $scope.isWaiting = true; $scope.promise = $http.get("voluumHandler.php?q=campaigns&filter=traffic-sou ...