Search form with a variety of fields that allows for searching without needing to repeat the component for each condition

I am currently facing an issue with my form that consists of multiple fields, each used to search through an API and display matching data in a table below. While I have successfully implemented this for one field, I now need it to work for all fields without repeating the component multiple times.

Here is a snippet of the relevant code:

  <form class="uk-form-stacked">
    <div>
       // Code for various input and select fields
    </div>
    <div>
       // Buttons for clearing and submitting the form
    </div>

    <div class="results-area"&g...

The current implementation only works for the first field (Product ID). I would like to make all fields searchable without duplicating components as shown below:

    <div class="results-area">
       // Result-item components for different fields
       
    </div>

Having separate computed methods for each field seems repetitious. Is there a better way to handle this?

Additionally, I want the search functionality to trigger on button click instead of dynamically updating as the v-model changes. How can I achieve this effectively without creating numerous buttons?

Edit

I managed to optimize the computed method to avoid repetition. However, I wonder if using a switch statement would be more efficient for rendering. Any thoughts on this?

Despite the optimization, I am still struggling to implement onclick search functionality and searching by dropdown <select> values.

filteredResults: function() {
  // Optimized filtering logic for all fields
  
},

Answer №1

To enhance code readability, consider renaming the filteredResults function to getFilteredResults. Additionally, include filteredResults as an array in the data section and modify the search function as follows:

<result-item
  :data="filteredResults"
  :total-pages="Math.ceil(filteredResults.length / 10)"
  :total="filteredResults.length"
  :per-page="10"
  :current-page="currentPage"
  @pagechanged="onPageChange"
/>
...
methods: {
  search () {
    this.filteredResults = this.getFilteredResults()
  }
  reset () {
   // clear all fields here
   ...
   this.filteredResults = []
  }
}

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 installing particular versions or tags of all npm dependencies?

We are embarking on a complex project that involves the use of numerous node modules and operates within a three-tiered development framework. develop stage production Our goal is to distribute modules to our private registry with tags for develop, stag ...

Error: The expression `xmlDoc.load` returns a value of undefined, which is not an object

My current challenge involves loading an XML file using Javascript in IE, Firefox, and Safari. I have yet to find a function that works well across all three browsers. The load function I am currently using is similar to the one found in w3schools tutorial ...

How can I utilize Pinia and TypeScript to set the State using an Action?

I have a Pinia + TypeScript store named user.ts with the following structure: import { User } from 'firebase/auth'; import { defineStore } from 'pinia'; export const useUserStore = defineStore('user', { state: () => ( ...

Center the image within the div by setting its position to absolute

<div class='img-box'> <img /> //position absolute <img /> //position absolute <img /> //position absolute <img /> //position absolute I am struggling to center the images within this div because of their absolute p ...

Is your child component releasing data?

Currently, I am facing an issue with my child component where the data changes are not being emitted properly. Here is how I am trying to handle it: <input type="checkbox" value="john" v-model="users" @click="updateUsers"> Upon clicking on the chec ...

Challenges with the dropdown menu navigation bar

I am struggling with my dropdown menu and sign up/sign in buttons as they are not aligning properly. I have tried various coding methods but haven't been able to fix the issue. Can someone provide me with suggestions on how to rectify this problem? c ...

How can I apply a class to the pre and code elements in Redactor?

I've been struggling for days to figure out how to add a class to the formatting option element within Redactor. By default, the "Code" formatting option wraps content in either <pre></pre> or <code></code> HTML elements. Howe ...

What steps should I take to fix the issue of "[ERR_REQUIRE_ESM]: Must use import to load ES Module" while working with D3.js version 7.0.0 and Next.js version 11.0.1?

Encountered a roadblock while integrating D3 with Next.js - facing an error when using D3.js v7.0.0 with Next.js v11.0.1: [ERR_REQUIRE_ESM]: Must use import to load ES Module Tried utilizing next-transpile-modules without success Managed to make D3.js ...

Is there a compelling case for implementing Meteor in 2017?

Back in the day, Meteor was expected to revolutionize web development on node by simplifying the process of creating interactive applications. Though I'm not well-versed in its history, it seems like most of the development effort has shifted elsewher ...

Issue with form array patching causing value not to be set on material multiple select

When attempting to populate a mat-select with multiple options using an array of ids from Firestore, I encountered an issue. The approach involved looping through the array, creating a new form control for each id, and then adding it to the formArray using ...

Calculator built with HTML, CSS, and JavaScript

Hi there, I'm experiencing some issues with my calculator. The buttons seem to be working fine and lining up correctly, but for some reason, nothing is showing up on the monitor or getting calculated when I press the buttons. Here's the code that ...

Modifying the width of a jQuery UI dialog from within the dialog box itself

I have designed an ASP.Net web page with a div and iFrame to display another page within it. Now, I am wondering if there is a way to change the width of the dialog from a button inside the dialog itself... Is this achievable? Thank you. ...

An odd issue has arisen where the website functions properly in Firefox 3.0 but encounters problems when accessed in Firefox 3

Could someone investigate this issue for me? When you click on the showcase and then on the logo, a modal window should open with the logo. It works perfectly in FF 3.0, but in FF 3.5, the tab switches from showcase to home after clicking the logo. Even ...

What is the purpose of wrapping my EventEmitter's on function when I pass/expose it?

My software interacts with various devices using different methods like serial (such as USB CDC / Virtual COM port) and TCP (like telnet). To simplify the process, I have created a higher-level interface to abstract this functionality. This way, other sect ...

What methods can be employed to stop tests from being included in rollup bundles?

I am currently in the process of creating a react component package and I would like to know how to prevent my tests folder from being included in the dist file that is generated by rollup. After running rollup -c, my directory structure looks like this: ...

Should I utilize capacitor's geolocation or opt for capacitor-community's background-location?

Currently developing a mobile app using Ionic/Vue 6 that traces the path of hikers. I have reached the stage where GPS information is required, and the challenge is making sure the GPS plugin remains active even when the user keeps their phone in their p ...

What is the best way to combine the elements of two arrays within a v-for loop?

Within my code, I have defined two arrays: users and projects. Each array contains unique numbers as IDs. A project can be owned by multiple users, so in the projects array, there is an array of user IDs named ownersId that corresponds to the id of users i ...

Vue.js - Error: Module not found: Cannot locate module 'lottie-vuejs'

I've been attempting to integrate lottie-vuejs into my project. After running the npm install command and following the steps outlined here, I encountered an issue. Unfortunately, I received the following error message: Module not found: Error: Can ...

Error: JSON data abruptly terminated (Node.js)

As a beginner developer diving into the world of API's, I've encountered a recurring error in Node.js that seems to be causing crashes: SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>) at IncomingMessage.<anonymo ...