The action of clicking on a button does not navigate through the items listed in a Bootstrap 4 table using JS and JQuery

I have a list of items with assigned statuses (5 statuses in total), each displayed in a specific column. Above the list, I have buttons corresponding to each status.

My goal is to click on one of these buttons to filter the list and show only items with the selected status. There are 5 buttons for each status, plus an additional button labeled 'Total' which will display all data again.

Although I am using Bootstrap 4 and JavaScript, I'm struggling with the JavaScript logic.

Below you can find a snippet of my code:

$(document).ready(function() {
    resStatusList();
    $(".searchGo").click(function() {
        var searchVal = $('#searchTotal').val();
        if (searchVal == "") {
        $('#searchroleName').addClass("alert");
        } else {
        $('#searchroleName').removeClass("alert");
        }
    });

    var $rows = $('#resListTable tr');
    $('#searchroleName').keyup(function() {
        var val = $.trim($(this).val()).replace(/ +/g, ' ').toLowerCase();
        $rows.show().filter(function() {
        var text = $(this).text().replace(/\s+/g, ' ').toLowerCase();
        return !~text.indexOf(val);
        }).hide();
    });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" rel="stylesheet"/>
<div class="addRoleDiv">
    <h4>Reservation List</h4>
    <label>Click on a button to filter items</label><br>
    <button class="btn searchGo btn-active" id="searchTotal">Total</button>
    <button class="btn searchGo btn-success" id="searchExpected">Expected</button>
    <button class="btn searchGo btn-danger" id="searchCancelled">Cancelled</button>
    <button class="btn searchGo btn-warning" id="searchPartial">Partial</button>
    <button class="btn searchGo btn-info" id="searchInhouse">Inhouse</button>
    <button class="btn searchGo btn-active" id="searchFinished">Finished</button>
</div>

<div class="col-lg-12 col-md-12 col-xs-12 padding table-responsive">
    
    <table class="table table-hover table-bordered" id="reservationListCheckIn">
        <thead class="reservationTableHead">
            <tr>
                <th>ID</th>
                <th>Name</th>
                <th>Status</th>
            </tr>
        </thead>
        <tbody id="resListTable">
            <tr>
                <td>864</td>
                <td>Helen Fields</td>
                <td>Expected</td>
            </tr>
            <tr>
                <td>2435</td>
                <td>Frank White</td>
                <td>Cancelled</td>
            </tr>
            <tr>
                <td>2343</td>
                <td>Hugo Egon</td>
                <td>Inhouse</td>
            </tr>
            <tr>
                <td>245</td>
                <td>Marc Jacobs</td>
                <td>Partial</td>
            </tr>
            <tr>
                <td>43</td>
                <td>Julia Kline</td>
                <td>Finished</td>
            </tr>
        </tbody>
    </table>
    
</div>

Answer №1

When the .searchGo button is clicked, extract the current text of the button. If it does not say "Total," hide all rows except those with the specified value in the last column:

$('#reservationListCheckIn tbody tr').hide();
$('#reservationListCheckIn tbody tr td:nth-of-type(3)').filter(function(idx, ele) {
    return ele.textContent == titleBtnClicked;
}).closest('tr').show();

To learn more, refer to:

  • :nth-of-type() Selector: This selects elements that are the nth child relative to their siblings with the same element name.
  • .filter( function ): Reduces the set of matched elements using a selector or function test.

The following script demonstrates this functionality:

$(document).ready(function() {
    $(".searchGo").on('click', function(e) {

        // Extract current button text
        var titleBtnClicked = $(this).text();

        // Check if it's not "Total"
        if (titleBtnClicked != 'Total') {

            // Hide all rows
            $('#reservationListCheckIn tbody tr').hide();

            // Show only rows with the correct value in the 3rd cell
            $('#reservationListCheckIn tbody tr td:nth-of-type(3)').filter(function(idx, ele) {
                return ele.textContent == titleBtnClicked;
            }).closest('tr').show();
        } else {
            $('#reservationListCheckIn tbody tr').show();
        }
    });

});
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" rel="stylesheet"/>
<link rel="stylesheet" href="https://rawgit.com/HubSpot/tether/master/dist/css/tether.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js"></script>

<div class="addRoleDiv">
    <h4>Reservation List</h4>
    <label>Click on a button to filter items</label><br>
    <button class="btn searchGo btn-active" id="searchTotal">Total</button>
    <button class="btn searchGo btn-success" id="searchExpected">Expected</button>
    <button class="btn searchGo btn-danger" id="searchCancelled">Cancelled</button>
    <button class="btn searchGo btn-warning" id="searchPartial">Partial</button>
    <button class="btn searchGo btn-info" id="searchInhouse">Inhouse</button>
    <button class="btn searchGo btn-active" id="searchFinished">Finished</button>
</div>

<div class="col-lg-12 col-md-12 col-xs-12 padding table-responsive">

    <table id="reservationListCheckIn" class="table table-hover table-bordered">
        <thead class="reservationTableHead">
        <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Status</th>
        </tr>
        </thead>
        <tbody id="resListTable">
        <tr>
            <td>864</td>
            <td>Helen Fields</td>
            <td>Expected</td>
        </tr>
        <tr>
            <td>2435</td>
            <td>Frank White</td>
            <td>Cancelled</td>
        </tr>
        <tr>
            <td>2343</td>
            <td>Hugo Egon</td>
            <td>Inhouse</td>
        </tr>
        <tr>
            <td>245</td>
            <td>Marc Jacobs</td>
            <td>Partial</td>
        </tr>
        <tr>
            <td>43</td>
            <td>Julia Kline</td>
            <td>Finished</td>
        </tr>
        </tbody>
    </table>

</div>

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

Seeking assistance with creating a form that is centered on the page

As a newcomer here, I am in the process of learning new things. My current challenge is to create a center form. Here is what I have experimented with so far: <!doctype html> <html lang="en> <head> <meta charset="utf-8"> &l ...

Top tips for utilizing CSS in a web component library to support various themes

Our team is currently in the process of developing a comprehensive design system that will be utilized across multiple projects for two distinct products. Each product operates with its own unique brand styleguide which utilizes design tokens, such as: Th ...

Tips for showcasing HTML as code tailored to certain programming languages

While browsing the web, I stumbled upon a website that had SQL code displayed with specific classes and styles applied to it. For example, table names were colored blue when inspected. Here is a glimpse of what I encountered: https://i.sstatic.net/WAacj.p ...

Enhancing Image Quality in Internet Explorer 10

Here is my current situation: I am working on a web page with a responsive design. The layout of the page consists of two vertical halves, and I intend to display an image (specifically a PDF page converted to PNG or JPG) on the right side. The challenge ...

oversized user interface selection container

When using semantic-ui for my search form with large input fields, I am facing an issue where the select option field does not adjust its size. I have followed the documentation but it seems like I might be missing something. Can someone help me figure out ...

AngularJS errorCallBack

I'm currently struggling with AngularJS and would really appreciate any constructive assistance. Below is the Angular code snippet I am working on: app.controller('customersCtrl', function($scope, $http) { $scope.element = function(num ...

Is there a way to prevent Javascript from modifying styles when printing?

Is there a way to prevent JavaScript from affecting styling during printing? For example, if I'm using JavaScript to hide certain content but want that content to be visible when printed. I have hidden a div using JavaScript and now I'm trying ...

How can we send and insert values into each class container individually and independently?

Is there a way to send an ajax request for each class container called dataContainer separately? I am trying to figure out how to send and insert values independently for each dataContainer without affecting others. I have searched online but couldn' ...

Utilizing jQuery to send data to a PHP script

Actually, I am not very familiar with jQuery. I have a jQuery script that passes variables to a file which displays data in JSON format. However, I am unable to show that data using the code below: $(document).ready(function() { var globalRequest = 0; ...

I keep encountering an issue in my application where I am receiving an error stating that

Encountering the error $ is not defined in my application due to the usage of the npm package. Link to the npm package Attached is the code snippet: Link to the code sandbox import React from "react"; import "./styles.css"; import { ZoomMtg } from "@zoo ...

Having trouble getting JQuery Ajax POST to work in Codeigniter

Encountering an issue with jQuery AJAX post in CodeIgniter, where clicking the submit button triggers an error. Below is the code snippet: home.js form.on('submit', function(e){ e.preventDefault(); var fields = {}; form.find(' ...

How to target the following element with CSS that has a specified class name

Would it be possible to achieve this using CSS alone, or would I need to resort to jQuery? This is how my code currently looks: <tr>...</tr> <tr>...</tr> <tr>...</tr> <tr class="some-class">...</tr> // My g ...

Display a collection of Mongoose objects in a React component

In my development stack, I rely on node js + React. The data I work with comes from mongoose and typically follows this format: { "_id": "61b711721ad6657fd07ed8ae", "url": "/esports/match/natus-vincere-vs-team-liquid- ...

Unlocking the secret to transferring information from POJO and Controller to Ajax

I have a situation where I am sending data from my controller as Json using the @ResponseBody annotation. Unfortunately, I am facing an issue where ajax is unable to recognize the data sent from my controller. When I check the typeof data in ajax, it shows ...

Generating individual div elements for every piece of data in ReactJS with the help of Redux

I am utilizing redux to manage the data within a React application. Each block of data is displayed within its own DIV element. My goal is to have each div separated by space and transformed into an accordion card. I am seeking guidance on the best appro ...

Tips for altering the color of spans that share a common top position without affecting the surrounding sibling elements above or below

Is there a way to change the color of spans with the same top position, excluding sibling elements above or below, in a paragraph made up of spans? I've been working on how to change the color of the line of span tags that contain another span with t ...

How can I make my Twitter Bootstrap carousel actually start "carousel"-ing?

I often find myself in over my head with programming, but I am giving it my best shot. Please be patient with me! Recently, I followed a tutorial on YouTube to create a carousel in my Django project. Unfortunately, instead of displaying one image at a ti ...

What is the best way to show the associated ul tag?

I've constructed the following HTML: <input id="<code generated id>" type="text" value="select"/> <div id="<code generated id>" class="popup"> <ul id="<code generated id>" class="none"> <li>A</li& ...

Trigger a series of child functions upon clicking the parent button

I am facing an issue where I am attempting to trigger the functions of each child component from a button click event on the parent component. I have tried using props and setting up a new function in the componentDidMount lifecycle method, but only the la ...

Content missing after centered banner

Seeking help! I need to figure out how to center text after a container. Whenever I try, it ends up either behind the banner picture or misaligned. I want both the banner and the text example to be centered, but placing the text in the right position has b ...