Thursday, November 30, 2023
HomeVideo EditingThe best way to Filter Knowledge on a Webpage with JavaScript

The best way to Filter Knowledge on a Webpage with JavaScript


Filtering is a generally used characteristic on varied webpages. It permits customers to slender down knowledge (like classes, tags, different traits) utilizing supplied parameters. Let’s create our personal!

One widespread instance of on-page filtering could be an eCommerce web site that permits customers to filter merchandise based on measurement, coloration, model and so on.

On this tutorial, we’ll be recreating the Tuts+ writer web page and implementing a filter part to permit us to filter articles based mostly on their class. 

1. Structure and Styling

For this demo, we’ll be cloning the writer web page so the markup and styling is taken instantly from the reside web site. The primary parts we’ll be specializing in are the filters and the displayed posts, so we’ll create a <div class="filter-container"> and <div class="posts-container"> and goal these in JavaScript.

We’ll model these containers and their content material in CSS:

1
.posts-container {
2
  show: flex;
3
  flex-wrap: wrap;
4
}
5
6
.publish {
7
  place: relative;
8
  width: 300px;
9
  margin-right: 30px;
10
  margin-bottom: 40px;
11
  border: 1px strong #e1e8ed;
12
  border-radius: 4px;
13
  margin-top: 13px;
14
  min-height: 300px;
15
  peak: auto;
16
}
17
18
.filter-container {
19
  show: flex;
20
  flex-wrap: wrap;
21
  justify-content: flex-start;
22
  hole: 10px;
23
  padding: 32px 0;
24
  border-top: 1px strong #e4e4e4;
25
  border-bottom: 1px strong #e4e4e4;
26
  margin-bottom: 32px;
27
}
28
29
.filter-button {
30
  transition: background-color 200ms, coloration 200ms;
31
  background-color: clear;
32
  font: inherit;
33
  cursor: pointer;
34
  show: inline-block;
35
  padding: 0 8px;
36
  coloration: #717171;
37
  border: 1px strong #9b9b9b;
38
  border-radius: 25px;
39
  font-size: 14px;
40
  white-space: nowrap;
41
}
42
43
.filter-button:hover {
44
  background-color: #f3f3f3;
45
  coloration: #3a3a3a;
46
}
47
48
.filter-button.is-active {
49
  background-color: #0085b6;
50
  border-color: #0085b6;
51
  coloration: #fff;
52
}

2. Getting Web page Knowledge utilizing Fetch API

On this demo, we’ll be utilizing the Fetch API to retrieve mock knowledge scraped from the Tuts+ authors web page and saved in a Github gist.

Our mock knowledge has the format:

1
[
2
  {
3
    "https://webdesign.tutsplus.com/tutorials/title"https://webdesign.tutsplus.com/tutorials/: ""https://webdesign.tutsplus.com/tutorials/,
4
    "https://webdesign.tutsplus.com/tutorials/link"https://webdesign.tutsplus.com/tutorials/: ""https://webdesign.tutsplus.com/tutorials/,
5
    "https://webdesign.tutsplus.com/tutorials/image"https://webdesign.tutsplus.com/tutorials/: ""https://webdesign.tutsplus.com/tutorials/,
6
    "https://webdesign.tutsplus.com/tutorials/categories"https://webdesign.tutsplus.com/tutorials/: [ ]
7
  },
8
  ...
9
]
10
  

That is the script for fetching knowledge from the script:

1
fetch("https://webdesign.tutsplus.com/tutorials/https://gist.githubusercontent.com/jemimaabu/b89339c1b7e5f81f8737fb66a858b6fc/uncooked/cdded4a10dbc98858481b5aedbcce3f3026dc271/tutorials"
2
).then(async (response) => {
3
  // deal with response knowledge
4
});

As soon as we’ve gotten our fetched knowledge, we are able to then manipulate the info and append it on the web page.

3. Appending Knowledge to the Webpage

For every object in our fetched response, we’ll create a publish div that may show the info on the web page. First, let’s outline our world variables:

1
let postsData = ""https://webdesign.tutsplus.com/tutorials/;
2
const postsContainer = doc.querySelector("https://webdesign.tutsplus.com/tutorials/.posts-container"https://webdesign.tutsplus.com/tutorials/);

Then we’ll create a operate createPost() that may deal with appending a brand new div to the posts-container aspect. On this operate, we create a brand new div aspect with the classname publish and set its innerHTML as the info we need to show.

1
const createPost = (postData) => {
2
  const { title, hyperlink, picture, classes } = postData;
3
  const publish = doc.createElement("https://webdesign.tutsplus.com/tutorials/div"https://webdesign.tutsplus.com/tutorials/);
4
  publish.className = "https://webdesign.tutsplus.com/tutorials/publish"https://webdesign.tutsplus.com/tutorials/;
5
  publish.innerHTML = `
6
      <a category="post-preview" href="https://webdesign.tutsplus.com/tutorials/${hyperlink}" goal="_blank">
7
        <img class="post-image" src="https://webdesign.tutsplus.com/tutorials/${picture}">
8
      </a>
9
      <div class="post-content">
10
        <p class="post-title">${title}</p>
11
        <div class="post-tags">
12
          ${classes
13
            .map((class) => {
14
              return "https://webdesign.tutsplus.com/tutorials/<span class="post-tag">' + class + "https://webdesign.tutsplus.com/tutorials/</span>"https://webdesign.tutsplus.com/tutorials/;
15
            })
16
            .be part of(""https://webdesign.tutsplus.com/tutorials/)}
17
        </div>
18
      </div>
19
  `;
20
21
  postsContainer.append(publish);
22
};

Inside our publish innerHTML, we use the be part of("") methodology on our classes.map() to take away the ‘,’ image that’s included in each array.

Now we are able to replace our response operate to name the createPost() operate as soon as the info has been fetched:

1
fetch("https://webdesign.tutsplus.com/tutorials/https://gist.githubusercontent.com/jemimaabu/b89339c1b7e5f81f8737fb66a858b6fc/uncooked/cdded4a10dbc98858481b5aedbcce3f3026dc271/tutorials"
2
).then(async (response) => {
3
  postsData = await response.json();
4
  postsData.map((publish) => createPost(publish));
5
});

4. Get Filter Params from Response

Since we’re utilizing JavaScript, we are able to map by way of our response to create a dynamic record of filter params. Let’s embrace the worldwide variables for our filter knowledge:

1
let filterData = ""https://webdesign.tutsplus.com/tutorials/;
2
const filterContainer = doc.querySelector("https://webdesign.tutsplus.com/tutorials/.filter-container"https://webdesign.tutsplus.com/tutorials/);

Now we need to write a script that types by way of the classes array in every response object and returns a singular record. We are able to replace our response object to deal with getting a singular record of filter params

1
fetch("https://webdesign.tutsplus.com/tutorials/https://gist.githubusercontent.com/jemimaabu/b89339c1b7e5f81f8737fb66a858b6fc/uncooked/cdded4a10dbc98858481b5aedbcce3f3026dc271/tutorials"
2
).then(async (response) => {
3
  postsData = await response.json();
4
  postsData.map((publish) => createPost(publish));
5
  filterData = [
6
    ...new Set(
7
      postsData
8
        .map((post) => post.categories)
9
        .reduce((acc, curVal) => acc.concat(curVal), [])
10
    )
11
  ];
12
});

Breaking down the code for our filterData:

  • We use [... new Set] to create an array of distinctive values. Set returns an object of distinctive values and the unfold syntax […] converts the thing into an array.
  • We map by way of the postsData to get the classes array of every publish object inside the info response.
  • We use the .cut back() methodology to mix the classes array for every publish object into one array.

As soon as we’ve gotten our array of distinctive filter values from the publish classes, we are able to create a operate to append every filter to the web page. We’ll create a brand new button aspect and set the innerText and className. We’ll additionally set a “data-state” attribute to deal with altering the button state when clicked.

Every filter button may have a click on occasion listener set to the handleButtonClick operate, which can be liable for dealing with the filtering logic.

1
const createFilter = (filter) => {
2
  const filterButton = doc.createElement("https://webdesign.tutsplus.com/tutorials/button"https://webdesign.tutsplus.com/tutorials/);
3
  filterButton.className = "https://webdesign.tutsplus.com/tutorials/filter-button"https://webdesign.tutsplus.com/tutorials/;
4
  filterButton.innerText = filter;
5
  filterButton.setAttribute("https://webdesign.tutsplus.com/tutorials/data-state"https://webdesign.tutsplus.com/tutorials/, "https://webdesign.tutsplus.com/tutorials/inactive"https://webdesign.tutsplus.com/tutorials/);
6
  filterButton.addEventListener("https://webdesign.tutsplus.com/tutorials/click on"https://webdesign.tutsplus.com/tutorials/, (e) =>
7
    handleButtonClick(e, filter)
8
  );
9
10
  filterContainer.append(filterButton);
11
};

And replace our response operate to name the createFilter() operate:

1
fetch("https://webdesign.tutsplus.com/tutorials/https://gist.githubusercontent.com/jemimaabu/b89339c1b7e5f81f8737fb66a858b6fc/uncooked/cdded4a10dbc98858481b5aedbcce3f3026dc271/tutorials"
2
).then(async (response) => {
3
  postsData = await response.json();
4
  postsData.map((publish) => createPost(publish));
5
  filterData = [
6
    ...new Set(
7
      postsData
8
        .map((post) => post.categories)
9
        .reduce((acc, curVal) => acc.concat(curVal), [])
10
    )
11
  ];
12
  filterData.map((filter) => createFilter(filter));
13
});

5. Deal with Filtering when Clicked

Now we’ve gotten our filter buttons and preliminary knowledge, we are able to outline a operate to deal with filtering the info when a button is clicked. This may be achieved utilizing the .filter() methodology on the posts knowledge array.

That is the logic we’ll use for filtering the info:

  • Create a operate handleFilterPosts() that accepts a filter param
  • Use the .filter() methodology to filter our posts knowledge based mostly on the filter param
  • Clear all components in posts-container and append the brand new filteredData to the container.
1
const handleFilterPosts = (param) => {
2
  let filteredPosts = [...postsData].filter(publish => publish.classes.contains(param))
3
  
4
  postsContainer.innerHTML = ""https://webdesign.tutsplus.com/tutorials/;
5
  filteredPosts.map(publish => createPost(publish))
6
};

We need to detect when a button has been clicked and replace the button state. On this tutorial, we’ll toggle the buttons so if clicked as soon as, the button is about to lively and if clicked once more, the button is about to inactive.

1
const handleButtonClick = (e, param) => {
2
  const button = e.goal;
3
  const buttonState = button.getAttribute("https://webdesign.tutsplus.com/tutorials/data-state"https://webdesign.tutsplus.com/tutorials/);
4
  
5
  if (buttonState =="https://webdesign.tutsplus.com/tutorials/inactive"https://webdesign.tutsplus.com/tutorials/) {
6
    button.classList.add("https://webdesign.tutsplus.com/tutorials/is-active"https://webdesign.tutsplus.com/tutorials/);
7
    button.setAttribute("https://webdesign.tutsplus.com/tutorials/data-state"https://webdesign.tutsplus.com/tutorials/, "https://webdesign.tutsplus.com/tutorials/lively"https://webdesign.tutsplus.com/tutorials/);
8
  } else {
9
    button.classList.take away("https://webdesign.tutsplus.com/tutorials/is-active"https://webdesign.tutsplus.com/tutorials/);
10
    button.setAttribute("https://webdesign.tutsplus.com/tutorials/data-state"https://webdesign.tutsplus.com/tutorials/, "https://webdesign.tutsplus.com/tutorials/inactive"https://webdesign.tutsplus.com/tutorials/)
11
  }
12
}

We additionally need to make certain just one button is chosen at a time and if no button is chosen, the posts ought to show the default knowledge so we are able to create features to deal with that logic:

1
const resetFilterButtons = (currentButton) => {
2
  const filterButtons = doc.querySelectorAll("https://webdesign.tutsplus.com/tutorials/.filter-button"https://webdesign.tutsplus.com/tutorials/);
3
  [...filterButtons].map(button => {
4
    if (button != currentButton) {
5
      button.classList.take away("https://webdesign.tutsplus.com/tutorials/is-active"https://webdesign.tutsplus.com/tutorials/);
6
      button.setAttribute("https://webdesign.tutsplus.com/tutorials/data-state"https://webdesign.tutsplus.com/tutorials/, "https://webdesign.tutsplus.com/tutorials/inactive"https://webdesign.tutsplus.com/tutorials/)
7
    }
8
  })
9
}
10
11
const resetPosts = () => {
12
  postsContainer.innerHTML = ""https://webdesign.tutsplus.com/tutorials/;
13
  postsData.map((publish) => createPost(publish));
14
}

Lastly, we are able to replace our button click on operate:

1
const handleButtonClick = (e, param) => {
2
  const button = e.goal;
3
  const buttonState = button.getAttribute("https://webdesign.tutsplus.com/tutorials/data-state"https://webdesign.tutsplus.com/tutorials/);
4
  resetFilterButtons(button);
5
  
6
  if (buttonState =="https://webdesign.tutsplus.com/tutorials/inactive"https://webdesign.tutsplus.com/tutorials/) {
7
    button.classList.add("https://webdesign.tutsplus.com/tutorials/is-active"https://webdesign.tutsplus.com/tutorials/);
8
    button.setAttribute("https://webdesign.tutsplus.com/tutorials/data-state"https://webdesign.tutsplus.com/tutorials/, "https://webdesign.tutsplus.com/tutorials/lively"https://webdesign.tutsplus.com/tutorials/);
9
    handleFilterPosts(param)
10
  } else {
11
    button.classList.take away("https://webdesign.tutsplus.com/tutorials/is-active"https://webdesign.tutsplus.com/tutorials/);
12
    button.setAttribute("https://webdesign.tutsplus.com/tutorials/data-state"https://webdesign.tutsplus.com/tutorials/, "https://webdesign.tutsplus.com/tutorials/inactive"https://webdesign.tutsplus.com/tutorials/)
13
    resetPosts()
14
  }
15
}

Conclusion

And with that, we’ve utterly constructed a filtering part for knowledge on a webpage utilizing vanilla JavaScript!

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments