Implementation of Vue.js for filtering search results in a list view

I would like to enhance my searching functionality by including both header and content. Currently, the search is only based on content. Any recommendations on how to modify this source code to accomplish that? Thank you

Below is the HTML snippet:

<div id="list">
<input type="text" v-model="search">
  <ol>
    <li v-for="(items, key) in groupedItems">
      <h3>{{ key }}</h3>
      <p v-for="todo in items">{{ todo.name }}</p>
    </li>
  </ol>
</div>

For a preview of the code, visit this JS Fiddle link: https://jsfiddle.net/60jtkp30/9/

Answer №1

Although not the most elegant solution, after reviewing your code on jdfiddle, it appears that simply modifying the filter function should suffice.

var list = new Vue({
    el: '#list',
    data: {
    search: '',
        items: [
            { name: 'mike', type: 'student' },
            { name: 'beckham john', type: 'footballer' },
            { name: 'walcott', type: 'footballer' },
    { name: 'cech', type: 'footballer' },
    { name: 'jordan', type: 'actor' },
    { name: 'tom', type: 'actor' },
    { name: 'john', type: 'actor' }
        ]
    },
computed: {
    groupedItems() {
    const arr = {}
    //search function
    var searchResult = this.items.filter( task => {
        return task.name.toLowerCase().indexOf(this.search.toLowerCase())>-1 || task.type.toLowerCase().indexOf(this.search.toLowerCase())>-1;
    } )
    //grouping
    for(var i = 0; i < searchResult.length; i++) {
        const key = searchResult[i].type
        if (arr[key]) {
        arr[key].push(searchResult[i])
      } else {
        arr[key] = [searchResult[i]]
      }
    }
    return arr
  }
}
})

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

The Google Picker API encounters a server error when attempting to retrieve the OAuth token after being released as a private add-on

Recently, I encountered a puzzling issue with my script that utilizes the Google Picker API. During testing, everything worked flawlessly until I decided to publish it as a private add-on. From that point on, the script's getOAuthToken function starte ...

"Encountering a problem with b-table in the most recent release of Buefy (version 0.9

Encountering an issue with the latest version of buefy (0.9.2) when using b-table. Upon loading my page, I receive an error stating "cannot read effectiveDate of undefined" (i.e., props.row is undefined). This problem was not present in the previous versio ...

Angular: identifying inconsistencies between previous and current values with range slider and numeric input spinner

Attempting to resolve a discrepancy between oldVal and newVal in a 'range slider' and 'number spinner input' that are both linked. Discovered a bug where the difference between oldVal and newVal is much larger than expected, considering ...

Ensuring User Data is Current in the UI with Firebase Auth Callbacks

Below is the standard method for setting the user state to currentuser that is returned from onAuthStateChanged. I am looking for a useEffect hook that will be triggered whenever there is an update to the user's information. Unfortunately, I am unable ...

Rounding Decimals using JavaScript

I am facing the challenge described in this particular query. In most cases, my code works fine to round numbers to two decimal places with the following formula: Math.round(num * 100) / 100 However, there was a peculiar scenario where it failed. When tr ...

I'm getting an error message that says THREE is not recognized. How can I resolve this issue

Currently, I am diving into the world of Three.js and encountering a persistent error that has been quite challenging to resolve. Despite my efforts in exploring various solutions, the error remains unresolved. I have ensured that the Three.js library is ...

Invoke the function defined within a modal when dismissing a ui-bootstrap modal

Inside my ui-bootstrap modal controller, I have a $watch function set up for a variable. The code snippet looks like this: main.controller('modalCtrl', ['$scope', '$rootScope', '$modalInstance', function ($sc ...

How can I transform an array of text into dropdown options?

Currently, while utilizing Vue and vue-bootstrap, I am facing the task of populating a dropdown menu with an array of text items retrieved from my database. The <b-form-select> component in Bootstrap requires objects of the form {value: 'some va ...

Retrieve the initial value of the input element following a modification

Is there a way to retrieve the original default value of an input field after changing it using .val()? In my HTML, I have various text-inputs with classes .large and .medium, each with its own default text value. What I'm trying to achieve is when a ...

Tips for preventing the use of website URLs as variables in jQuery ajax() calls:

Having some trouble. For example: $(document).ready(function () { $("#btnSend").click(function () { var noti_p = { url: "Pusher_Controller.ashx", data: "&appname=" + $("#selt1").val() + "&title=" + $("#title"). ...

ng-repeat table grouping by date

How can I utilize *ngFor to generate an HTML table grouped by date in columns? Here is an example of a JSON List: [{ "id": "700", "FamilyDesc": "MERCEDES", "model": "Mercedes-BenzClasse A", " ...

Creating unique URLs for websites can be accomplished by following these steps:

I am in the process of developing a website where content in the main div within the body section can be replaced. However, I have realized that all content will end up on the same URL. Any suggestions on how to fix this? HTML/PHP(index.php): <htm ...

Emphasize the engaged menu selection when the page is refreshed

My application is a dashboard app built with React, Redux, and Antdesign. I utilized the layout provided by Ant design at https://ant.design/components/layout/. One issue I encountered is that when clicking on side menus, the active menu gets bold but upo ...

Error occurs when attempting to access second level route in Vue directly

I am currently utilizing vue-router within my vue application. The application consists of the following routes: const routes = [ {name: "home", path: "/", component: Home}, {name: "user", path: "/user", component: User}, {name: "edit-user", p ...

The active class functions properly with errors, but does not respond to messages or warnings

Within my component, there is a member named capsLockIsOn. This member interacts perfectly with the following code: <div class="error" [class.active]="capsLockIsOn">Caps is on!</div> Surprisingly, a switch to either class=& ...

Tips for generating dynamic JSON: Organize the data by filtering it and creating key-value pairs for the appropriate JSON objects

How can I generate dynamic JSON based on input, filter data, and create appropriate key-value pairs for JSON objects? The database schema can be viewed https://i.sstatic.net/iP1JS.png Although I attempted the following code, it did not produce the desi ...

Ways to require semicolons in a Typescript interface

When declaring a TypeScript interface, both , (comma) and ; (semicolon) are considered valid syntax. For example, the following declarations are all valid: export interface IUser { name: string; email: string; id: number; } export interface IUser { ...

There seems to be an issue with the location.href function in the server-side code of

I'm encountering an issue in the code below where I am attempting to redirect to the login page, but it seems to not be functioning as expected. app.get('/current-pass', (req, res) => { res.sendFile(path.join(staticPath, "currentPass ...

Issue with gulp-uglifyjs arising from an empty return by gulp.src

Within a directory named app, I am iterating through an unknown number of folders to generate a folderName.min.js file for each one. Below is the Gulp script being used: var es = require('event-stream'); var uglify = require('gulp-uglifyjs ...

Using Web SQL always seems to throw me for a loop

I attempted to populate a web SQL database with some data, but encountered an issue. Here is my code: database(); for(var i=0; i<m.length; i++){ showid = m[i].id; showtitle = m[i].title; insert(); } function database(){ //open the database ...