Enhance Your HTML Table with a Dynamic Multi-select Interactive Filter

I am currently working on generating an HTML table dynamically using JSON objects. My goal is to implement a filter for one of the table columns, which is functioning correctly. However, I'm facing challenges in integrating a multiselect feature.

I have attempted to include this line:

$('.One1').multiselect();

within my script at various locations, but unfortunately, it hasn't produced the desired outcome.

var data = {"headers":["One","Two","Three","Four","Five","Six","Seven","Number1","Number2"],"rows":[["One1","Two1","Three1","Four1","Five1","Six1","Seven1",11,1000],["One1","Two1","Three2","Four1","Five1","Six1","Seven1", 22, 2000],["One2","Two1","Three1","Four2","Five1","Six1","Seven1", 77, 99]]};

...

<div id="one"></div>

Answer №1

If you want to specify your select as being multiple, declare it like this:

var One = '<div class = "filter"><select multiselect class="One1" data-col="0"><option value="a">' + "One" + '</option>';

Modify it to read as follows:

var One = '<div class = "filter"><select multiple class="One1" data-col="0"><option value="a">' + "One" + '</option>';

Outcome:

var data = {"headers":["One","Two","Three","Four","Five","Six","Seven","Number1","Number2"],"rows":[["One1","Two1","Three1","Four1","Five1","Six1","Seven1",11,1000],["One1","Two1","Three2","Four1","Five1","Six1","Seven1", 22, 2000],["One2","Two1","Three1","Four2","Five1","Six1","Seven1", 77, 99]]};


//////First table - Three1 for Views
var table1 = '<div class="filter_box"></div><div class="row"><div class="col-lg-6" style="background-color: #e90649; width: 117px;">&nbsp;</div><div class="col-lg-6" style="max-width: 100px; padding-left: 10px; font-size: 2vw;">Table<br/><br/><span style="font-size: 1vw;">One1, Three1, Five1</span></div><div class="col-lg-6"><div class="container"><table><thead><tr></tr><tr><th>One</th><th>Two</th><th>Three</th><th>Four</th><th>Five</th><th>Six</th><th>Seven</th><th>Number1</th><th>Number2</th></tr></thead><tbody>';
var One = '<div class="filter"><select multiple class="One1" data-col="0"><option value="a">' + "One" + '</option>';

for (i = 0; i < data.rows.length; i++) 
{

table1 +="<tr><td>" + data.rows[i][0] + "</td><td>" + data.rows[i][1] + "</td><td>" + data.rows[i][2] + "</td><td>" + data.rows[i][3] + "</td><td>" + data.rows[i][4] + "</td><td>" + data.rows[i][5] + "</td><td>" + data.rows[i][6] + "</td><td>" + data.rows[i][7].toLocaleString() + "</td><td>" + data.rows[i][8].toLocaleString() + "</td><td>" + "</td></tr>";
  // Interactive filters

       One +='<option value="' + i + '">' + data.rows[i][0] + '</option>';
}
...
//$('.One1').multiselect();
@import url('https://fonts.googleapis.com/css?family=Roboto');

body, * { 
    margin: 0;
    color:#fff;
    font-family: Roboto; 
    text-transform:capitalize;
    }
.row {
    display: table;
    width: 100%;
    height: 241px; 
    background-color:#454545;
}
...
/*
tr th:nth-child(5) {
    background: red;
    display:none;
} 

tr td:nth-child(5) {
    background: red;
    display:none;
} 
*/
<script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prettify/r298/prettify.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/0.9.13/js/bootstrap-multiselect.js"></script>


<div id="one"></div>

Answer №2

To illustrate, if you wish to filter out "One1" in a table, you can easily achieve this using the following code:

$('td:contains("One1")').parent().hide();
and
$('td:contains("One1")').parent().show();
as a filtering method. If you intend to filter a specific column, you can combine the above method with your selector like so:
$('table tr > td:nth-child(yourColumnNumberShouldBeFiltered), table tr > th:nth-child(yourColumnNumberShouldBeFiltered)')
. These expressions can be included in a function attached to the 'change' event of your multi-select dropdown.

UPDATE: The earlier approach, utilizing :contains(), filters for all instances of "One1", "One12", and similar patterns! For precise match filtration, consider using this code:

$('table tr > td:nth-child(yourColumnNumberShouldBeFiltered)').filter(function() {
    return $(this).text() == "yourExactTextToFilter";
}).parent().hide();

$('table tr > td:nth-child(yourColumnNumberShouldBeFiltered)').filter(function() {
    return $(this).text() == "yourExactTextToFilter";
}).parent().show();

For improved performance in finding exact matches, refer to this resource: Select element by exact match of its content

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

"Unlocking the potential of JSON: A guide to retrieving and displaying three specific fields in

My PHP service is returning the following data: [[1,"16846"],[2,"16858"],[3,"16923"],[4,"16891"]] Within my HTML, I have ajax set up to fetch this information; $.ajax({ type: 'POST', url: 'getDadosGrafico.php', ...

Having trouble assigning a value to the datapicker through the onchange event and the name attribute in the code below

const stateValues = { code: '', product: '', checked: 'false', jobCardNo: '', openDate: '', completionDate: '', serial: '', technicalNo: '', ...

Tips for adjusting the color of a link in the Navbar after being clicked

Upon clicking a link in the Navbar, I want to change its color (Link A) and have it return to default if I navigate to another link (Link B). links.forEach( a=>{ //I want all other links to revert back to default color a.onclick = () => { ...

Creating static pages with dynamic nested routes is a powerful feature that can enhance the structure

I've been working on a website and I've encountered a problem. Essentially, I need to display movies by genre from a database. When a user clicks on a genre, it should take them to another list of movies in that genre. Then, when they click on a ...

Express-Postgres encounters an issue with applying array filtering: error message indicates that the operator for comparing integer arrays and text arrays does not exist

I'm facing an issue where the same query that works on the terminal is now giving me an error: operator does not exist: integer[] && text[] It seems that pg.query is having trouble processing the expression path && Array[$1]. You can ...

Issue with jQuery animation opacity not functioning correctly while simulating toggling using .on() and .off() methods

I have been working on creating a form that should show up when the user clicks a button, and then disappear when the user clicks anywhere outside the form. http://jsfiddle.net/z4gavkfd/ var app = { init: function() { $("#button").on("click", functi ...

Can you provide guidance on utilizing OneNote JavaScript APIs to interpret indented paragraphs within OneNote?

I keep a notebook that contains the following entries: https://i.stack.imgur.com/MLdO0.png For information on OneNote APIs, you can refer to this link (paragraph class selected already) https://learn.microsoft.com/en-us/javascript/api/onenote/onenote.p ...

What are some strategies for optimizing React performance when managing controlled input from numerous components?

Building a Parent component with multiple child components, each passing data to the parent for an API call. The structure is as follows: const MainComponent = () => { const [child1input, setChild1Input] = useState(""); const [child2input, setChild ...

What is the best way to make sure the background color of a container stretches across the full width of the screen?

I am currently learning HTML and CSS, and as a practice project, I am working on building a portfolio webpage. In the image provided, there are two containers, and I am facing an issue with the space on the sides when changing the background color. Despite ...

Is there a way to verify if the $compile process has finished?

I am currently developing a function that can dynamically create an email template from an HTML template and some provided data. To accomplish this, I am utilizing Angular's $compile function. However, I have encountered a challenge that I seem unabl ...

How come sinon fails to identify my stub?

Imagine a scenario where I have a module that is exported in the following way: module.exports = mymodule; Now, in my testing file, I require this module and then proceed to stub it. var mymodule = require('./mymodule'); describe('Fetchi ...

Create a div that is positioned absolutely on the page and spans the entire height of the

I have a position: absolute div that I need to be tall enough to reach the bottom of the page when scrolling all the way down. Essentially, it should span the entire height of the document page, not just the window. I've discovered that using viewpor ...

CSS for leaving white space at the end of one third of a container

Currently developing a website and facing an issue with the layout: I am trying to create 3 columns of equal height with each column taking up one-third of the width. However, there seems to be a small white gap on the right side of the last column. Here ...

Is it a common issue that sound.stop() doesn't seem to function properly, while sound.play() operates successfully within Howler

I am having trouble utilizing Howler.js in Reactjs with typescript. While I can successfully play the sound, pausing or stopping it seems to be an issue. Below is the code snippet: This component takes all the audio details as props. I used console.log() ...

Three.js: Objects in the distance appear more subtle

Currently, I am developing a three.js scenario that showcases textured point sprites. These sprites obtain their textures from a single uniform, which is a 2D canvas containing the alphabet: https://i.stack.imgur.com/Ceh9x.png In my scene, all the letter ...

Make sure to review for any duplicate entries prior to making updates to

Here is the issue I am facing... I am working with an SQL table that contains detailed information about flights, such as arrival date, arrival time, departure date, departure time, and more. When updating a flight entry, I need to ensure that the dates o ...

What could be causing the carousel to be hidden in a Next.js project?

My project involves utilizing the react-multi-carousel component to display a maximum of two stock photos. I made modifications to the next.config.js file as well. import Image from "next/image"; import Carousel from "react-multi-carousel&qu ...

JavaScript CheckBox Color Change Not Functioning

Hello, I am currently experimenting with the checkAll function. When I click on the checkAll checkbox, it should select all rows and change their background color accordingly. Below is the JavaScript code I am using: function checkAll(objRef) { v ...

I am having trouble getting bootstrap-icons to work in my Angular project and I'm eager to figure it out

I am having trouble incorporating bootstrap-icons into my angular project. Despite running the npm i bootstrap-icons command, I am unable to successfully add icons using icon fonts on my webpage. As a temporary solution, I have added the bootstrap icon CD ...

Footer flickers while changing routes

There seems to be a glitch where the footer briefly flashes or collapses when switching routes, specifically if the page is scrolled down to the middle. If at the top of the page, the transition works smoothly. This issue becomes more apparent on high refr ...