Sometimes it is necessary to filter data on the front end to enhance the interactive experience. When there are many filtering conditions available for data, hard-coding the logic will cause trouble in later maintenance. Below is a simple filter I wrote myself. The filter conditions can be set dynamically based on the fields contained in the data. This article mainly introduces the multi-condition filtering function of front-end data based on JavaScript in detail. It has certain reference value. Interested friends can refer to it. I hope it can help everyone.
Imitating JD.com’s filtering conditions, here we take the price range and brand as a test.
Code
The code mainly uses the js filter Array.prototype.filter, which will traverse the array elements. Check, return a new array that meets the check conditions, and the original array will not be changed.
1 2 3 4 5 6 7 8 9 10 |
|
With this method, it is much easier to filter data. Let’s first define a product category.
1 2 3 4 5 6 |
|
Create a filter object and put all methods for filtering data in it. In order to automatically adapt to different filtering conditions, the filtering conditions are divided into two major categories. One is the range type rangesFilter, such as brand, memory, etc.; the other is the selection type choosesFilter, such as: price, screen size, etc.
When different categories are screened at the same time, AND logic is used. Each category is screened based on the screening results of the previous category. For example, if I want to filter Huawei mobile phones priced between 2000 and 5000, I first call rangesFilter to filter products and return result1, and then use choosesFilter to filter result1 and return result2.
Of course, if there are other major categories, not necessarily logical, they will be dealt with separately.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
Interval type filtering, the code is as follows.
1 2 3 4 5 6 7 8 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
Select type filter:
1 2 3 4 5 6 7 8 9 10 11 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
Define an execution function doFilter().
1 2 3 4 5 6 7 8 9 10 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
Test
Create 10 product data and filter conditions
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
|
Call function
1 2 |
|
Output
code The scalability and maintainability are very good. As long as the type field in the filtering conditions is consistent in the product data, you can filter. For example, change the filtering conditions to
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
Output
# Search matching and other places also need to be optimized, whether it is case-sensitive, whether it is an exact match or a fuzzy match, etc.
Related recommendations:
Angularjs implements select drop-down box sample code with search and filter function
How to implement something similar to Taobao in PHP Filtering function
Example analysis of table content filtering function based on jquery_jquery
The above is the detailed content of JavaScript front-end data multi-condition filtering function implementation code. For more information, please follow other related articles on the PHP Chinese website!