Do you want to search from a list or a group of products to make your life a little easier? We can perform this action by using Bootstrap 4 filters.
Bootstrap 4 does not have any specific element with the name of filters that can perform this function. However, the addition of jQuery allows us to conduct this filtration.
We can conduct filtration on tables, lists, or anything using the Bootstrap 4 filters.
First, create a table, a list, or anything like a product’s grid to perform the practical implementation of the filters. Here, we'll create a sample table and implement jQuery on it.
HTML
tag to add all the required bootstrap and JavaScript libraries in the head
tag. We also start the body
tag.div
which holds the search bar and product table in it.h2
to give a title. This can be optional, according to our needs.myFilter
. Inside the tag class, we use the form-control
to make the search bar responsive and expand it to the entire page size.bordered-table
class.head
tag, then the row
tag following the column
tag. These are the demo names that we can add according to our needs.body
tag. Inside the tag, we define its ID as productTable
. This ID is used to define the point where we search the data.div
tag.Let's add code to search anything from the table shown above:
document.ready
function to confirm the execution of the JavaScript code written inside this function.keyup
function specifies when the user releases the key.variable
value that holds the new value after converting the typed value of the user to lowercase. Here, conversion of lowercase is done to perform a case-insensitive search.filter
function. Note: The table body is defined as the starting point of the search to avoid the header part being included in it.
toggle
to hide the rows that are not entertained through the search query and toLowerCase
to make the search case insensitive. The index of -1
means the records have no matching values. In the end, we close all the tags.