What is the most effective method for filtering a table using both column-specific and global filters?

Looking for the most efficient way to filter a large chunk of JSON data client-side using a table? Each header has an input filter where users can enter a string to filter that specific property. There is also a global filter for free text search.

If you have any ideas on how to accomplish this, I'd love to hear your thoughts.

Example for visualization purposes:

+---------------+
| Global filter |
+---------------+

+----------+---------+----------+-----------+
| Column 1 | Column 2 | Column 3 |  Column 4 |
+----------+---------+----------+-----------+
|   ...    |   ...   |   ...    |    ...    |
+----------+---------+----------+-----------+

Option 1:

When clicking on a filter input:

  1. Generate a set of data from all populated filters except the clicked filter and cache it.
  2. On key-up - filter the data based on the cached result.

This method might have some overhead as some searches could be repeated.


Option 2:

  1. On key-up - filter based on cached searches.
  2. On blur - Current result replaces the cached search.

This method works well if only adding filters, but may cause issues when modifying existing ones.


Option 3:

A combination of Options 1 and 2. When clicking on a filter input:

  1. If the filter does not contain the default value, generate a cache based on all filters except the current one.
  2. On key-up - filter based on cached searches.
  3. On blur - Current result overwrites the cached search.

After brainstorming, Option 3 seems like the best approach in terms of simplicity and performance.

Do you think I'm on the right track? Would love to get your feedback!

/Patrik

Answer №1

The 3rd choice seems to be the preferred option, although it appears there may be a minor issue. If you are utilizing AND-based filtering, it is advisable to follow these steps:

  1. If the filter does not contain the default value (i.e., no input from the user), generate a cache based on all filters except the current one.
  2. If the filter contains some value, then discard the cache. Create a data set from all populated filters except for the filter that was clicked, and store it in the cache.
  3. During key-up events, filter based on the cached result mentioned above.
  4. Upon blurring, the current result will overwrite the cached search.

details = [
                     {user:"A",location:"kansas"},
                     {user:"B",location:"kansas"},
                     {user:"C",location:"phoenix"},
                     {user:"D",location:"phoenix"}
           ]

Imagine [ ] represents a text box. Initially, both are empty: [user=""] [location=""]

Now, if the user enters a city: [user=] [location=kansas]

// final cache: [{user:"A", location:"kansas"},{user:"B", location:"kansas"}]

Next, if the user attempts to enter a username:

[user=A] [location=kansas]

Cache used for search:

[{user:"A", location:"kansas"},{user:"C", location:"kansas"}]

// revised final cache after step 3: `[{user:"A", location:"kansas"}]`

If the user now wants to enter another city: [user=A] [location=phoenix] // yields 0 results

Cache used for search:[{user:"A", location:"kansas"}]

Remember, when filtering by a specific criterion like "location," avoid using a result set that has already been reduced by this same filter in the past.

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

Is it possible to convert checkbox values from a form into a JSON format?

HTML snippet : <form class="form-horizontal" id="addpersons" style="padding:20px;"> <fieldset class="scheduler-border"> <!-- Form Title --> <legend class="scheduler-border">Information</legend> <!-- First Name input-- ...

Error: The property 'price' is not defined and cannot be read

As I work on setting up my online store for teaching node.js, I encountered an issue when trying to delete a product from the products.json file. Surprisingly, once a product is removed, the corresponding entry in cart.json gets deleted as well. The error ...

What is the best way to extract data from a file containing numerous JSON objects?

I'm attempting to extract data from a large file that contains multiple JSON objects, but I'm struggling to find a suitable method. The structure of the file resembles BSON used in mongoDB. Here is an example snippet from the file: {"column" : v ...

Utilizing AngularJS to make an API call with $http method and handling a

I am attempting to make a call to my webservice using "$http". When I use "$ajax", it works fine. Here is an example of jQuery $Ajax working correctly: $.ajax({ type: "Get", crossDomain: true, contentType: "application/json; chars ...

Tips for recognizing users in socket.io?

I'm currently developing a chat application with socket.io. However, one issue I am facing is identifying the sender of a message. Unlike in Express where we have the "req" (request) variable to easily identify users, socket.io does not provide such f ...

saving numeric values and text to a document using node.js

In my node.js application, I am working with files to read and write numbers and strings. Currently, I am using fs.writeFileSync(myPath, value); where the value can be either a number or a string. When I try to read the file using fs.readFileSync(myPa ...

What could be causing my JavaScript code to not function properly in an HTML document?

Hello, I am a beginner in the world of coding and currently delving into web development. Recently, I was following a tutorial on creating a hamburger menu but I seem to be encountering some issues with the JavaScript code. I have double-checked my Visual ...

Bootstrap Tags Input - the tagsinput does not clear values when removed

I am attempting to manually remove the input value from bootstrap-tags-input when the x button is clicked, but the values are not changing in either the array or the inputs. This is the code I have tried: $('input').tagsinput({ allowDuplica ...

Leveraging the power of Angular to send the contents of a div via email

I have a div element with a specific class name and I am currently exploring ways to extract the rendered components of that div as text in order to include it in the body of an email. I have tried various methods such as using classes and ng-model, but so ...

Injecting dependencies in AngularJS when utilizing the controller-as syntax: A how-to guide

As I dive into the controller-as feature outlined in the documentation, I'm refining one of my controllers to align with their recommended syntax. However, I'm facing a challenge when it comes to injecting the $http service into my search() funct ...

Creating flexible items with a 1:1 ratio and ensuring that the padding on the left and right of the flex container remains consistent

Whenever I adjust the size of the window, https://i.sstatic.net/Fm3d1.png the flex-container ends up with uneven padding on the left and right, which is less than ideal. So, I am looking for a way to allow the flex-item to resize when the window size c ...

The Vue warning states that the property "$store" was attempted to be accessed during rendering, but it is not defined on the current instance

Hello, I am new to the world of web development and currently working on an online shop project. I have encountered two errors that I'm struggling to fix: 1) [Vue warn]: Property "$store" was accessed during render but is not defined on instance. 2) ...

arranging a collection of images based on their values in increasing order

cardShop is a container with various images, each with its own unique ID and value attribute. These values consist of two numbers separated by a comma, such as 2000,500 or 1500,200. My goal is to sort these images in ascending order based on the first numb ...

Strange occurrences within the realm of javascript animations

The slider works smoothly up until slide 20 and then it suddenly starts cycling through all the slides again before landing on the correct one. Any insights into why this is happening would be greatly appreciated. This issue is occurring with the wp-coda- ...

What is the reason behind the sudden "explosion" in this simulation?

Trying to create a simulation of a steerable vehicle, like a plane, hovercraft, or boat, in either a gas or liquid fluid (such as air or water). The JavaScript code is based on the 3D rigid body airplane simulator from Physics for Game Developers, adapted ...

What is the process of using TypeScript to import a constant exported in JavaScript?

In the environment I'm using typescript 2.6.1. Within react-table's index.js file, there is a declaration that looks like this: import defaultProps from './defaultProps' import propTypes from './propTypes' export const React ...

Maximizing Server Efficiency with PHP Functions

Suppose I have the code snippet below: <?php function someFunction() { //many lines of code } if ($someBooleanVariable) { //random code here } else { someFunction(); } ?> Query 1: Is it accurate to assume that the server will first lo ...

What is the best way to retrieve values from nested dictionaries in Python?

I am facing a challenge with this dictionary structure and need to loop through it to check if the key 'items' contains 'status' as well as 'In Progress' under the key 'toString'. Additionally, I have to extract the ...

How can I retrieve the value of an HTML component when submitting a form?

In my ExpressJS application, I have two pages: home and user. In the home.js file, I create a form that navigates to the user.js page when submitted. While I am able to retrieve values from input components, I am facing issues with other components. How ca ...

Tips for circumventing if statements in TypeScript-ReactJS?

I currently have various action checks in place to perform different functions. Essentially, I am using if and else conditions to determine the action type and execute the corresponding functionality as shown below: public onMessage = (messageEvent) => ...