Refine an HTML table by a specified value and eliminate columns using JavaScript to display a new table

Hey there, I'm currently working with two HTML tables labeled id="source" and id="destination".

The source table contains 7 columns while the destination table only has 5 columns.

My goal is to filter data based on col2 (with a value of 150) from the source table into the destination table. However, I only want to display columns 1, 3, 4, 5, and 6, completely ignoring col2 and col7 as shown in the image below. https://i.sstatic.net/3Ruod.png I've come across examples using jQuery for this task, but I prefer to achieve it using pure JavaScript since I'm not very familiar with jQuery. I have attempted to create two functions for this purpose, but unfortunately, I am still not obtaining the desired results.

function copytable() {
var source = document.getElementById('source');
var destination = document.getElementById('destination');
var copy = source.cloneNode(true);
copy.setAttribute('id', 'destination');
destination.parentNode.replaceChild(copy, destination);
filterTable();
}

function filterTable() {
var input, filter, table, tr, td, i;
input = document.getElementById("myInput");
filter = input.value.toUpperCase();
table = document.getElementById("destination");
 tr = table.getElementsByTagName("tr");
for (i = 0; i < tr.length; i++) {
td = tr[i].getElementsByTagName("td")[1];
if (td) {
  if (td.innerHTML.toUpperCase().indexOf(filter) > -1) {
    tr[i].style.display = "";
  } else {
    tr[i].style.display = "none";
  }
}       
}
}

Answer №1

Each step is clearly outlined with detailed explanations provided in javascript code :

function filterValue() {
 var vl = document.getElementById('txtValue').value; //retrieve filtering value
 var stbl = document.getElementById('source');  //fetch the source table
 var dtbl = document.getElementById('destionation');//access the destination table
 for(var i=0;i<stbl.rows.length;i++) {  //iterate through the rows of the source table
   var row=stbl.rows[i]; //get each row from the source table one by one
   if(row.cells[1].innerHTML==vl) {  //check if the entered value matches the value in column 2 of the source table
    var newrow=dtbl.insertRow();  //create a new row in the destination table
    for(var x=0;x<row.cells.length;x++) { //loop through all cells
      if(x!=1 && x!=6) {  //exclude column 2 and column 7 while copying values from source to destination cells
        var cell=newrow.insertCell();//add a cell in the newly created row
        cell.innerHTML=row.cells[x].innerHTML; //copy the value from the source cell to the destination cell
      }
    }
  }
 }
}
#source, #destionation {margin-top:15px; border:solid 1px #c0c0c0;}
#source tr:first-child, #destionation tr:first-child {background-color:#B6D7A8; border-bottom:solid 1px #c0c0c0;}
Filter by value from <b>col2</b> : 
<input type="text" id="txtValue" value="" />
<input type="button" value="Filter" onclick="filterValue();" /><br>
  <table id="source" cellpadding="5" cellspacing="0">
<tr>
<td>col1</td>
<td>col2</td>
<td>col3</td>
<td>col4</td>
<td>col5</td>
<td>col6</td>
<td>col7</td>
</tr>
<tr><td>45</td><td>150</td><td>john</td><td>3.00</td><td>15.00</td><td>95</td><td>45:00:00</td></tr>
<tr><td>46</td><td>150</td><td>Anyone</td><td>3.00</td><td>15.00</td><td>95</td><td>37:00:00</td></tr>
<tr><td>49</td><td>220</td><td>Not included</td><td>3.00</td><td>15.00</td><td>95</td><td>15:22:00</td></tr>
<tr><td>48</td><td>150</td><td>And so on</td><td>3.00</td><td>15.00</td><td>95</td><td>35:50:00</td></tr>
</tr>
  </table>
  <table id="destionation" cellpadding="5" cellspacing="0">
<tr>
<td>col1</td>
<td>col3</td>
<td>col4</td>
<td>col5</td>
<td>col6</td>
</tr>
  </table>

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

Puppeteer headful Browser running on Node JS fails to start

Experimenting with puppeteer to explore browser automation techniques. I'm trying to launch the chromium browser in visible mode rather than headless, so I've set the launch option to false. However, Chromium still isn't opening as expected. ...

Issue with JQuery Ajax: Some users experiencing failure with ajax requests (response code 0)

I've implemented an ajax-based login system where users input their username/password and the data is asynchronously sent to a PHP script for validation. Once validated, the user is logged in or an error message is returned in JSON format. However, I ...

Encounter an issue while trying to fetch a JavaScript value from a variable using Selenium with Python

Looking to extract the value of the variable remainingTimeString from this specific webpage using Python and Selenium webdriver. I am attempting to utilize the driver.execute_script() function in my code snippet below: import selenium.webdriver options = ...

Setting up node.js for angular - serving static files with connect.static and running unit tests

Currently, I am in the process of setting up a basic node webserver by following a tutorial outlined in Pro AngularJS published by Apress. Node.js along with both the connect and karma modules have been successfully installed on my system. During the ins ...

Change the input value by clicking different buttons

Looking for a way to change the value or source of an input when clicking on different buttons? For example, clicking on Button 1 changes the input to "apple" and Button 2 changes it to "orange", etc. Here is a snippet of what I have tried so far: $(doc ...

Preventing incomplete file uploads (through FTP) while retrieving the latest file using ajax and PHP

Yes, the title of this may seem unusual, but there's a reason for it. In my setup, I have a camera linked to a laptop. Through Remote Shooting, when a photo is captured by the photographer, it gets stored in a folder on the laptop's hard drive. ...

Unable to trigger $(document).ready function

So I've got this content script injected into a YouTube page as part of a Chrome extension, and it looks something like this: $(document).ready(function () { console.log("[INFO] Changes detected"); myFunc(); } When I refresh the page, t ...

Steps for disabling and collapsing an individual header on the JQuery Accordian

Looking to adjust the behavior of 4 headers in accordions? Specifically, you want to collapse and disable only the first header out of the set. Here's how: $("#ExpandCollapse").accordion({ active: false, collapsible: true }); To ...

Printing HTML to a VueJS page is simple and efficient

I have a situation where one of my attributes in a property contains an HTML string. Instead of rendering the HTML as expected, when I output it within my template, the browser displays the raw HTML code with tags intact. It doesn't interpret it as a ...

Encountered an error stating 'Cannot read property 'user' of undefined' while attempting to generate a user document during account creation using Firebase

I'm having trouble adding the user ID to a new collection named 'accounts' during account creation. I keep encountering an error that says 'Cannot read property 'user' of undefined'. Can someone please help me troubleshoo ...

Incapable of deactivating button in Vue JS when quantity equals zero

I'm currently working with Vue to create a quantity selector component where users can increase or decrease input values. However, I am facing an issue where I need to disable the minus button when the value is zero or lower. Below is the HTML and JS ...

An Illustration of Basic Nested Controller within Directive Parameters

Check out this code snippet app.directive('hello', function() { return { restrict: "E", templateUrl: "/Angular/Modules/Selector.html", controller: function () { this.message = [enter the attribute message he ...

Can VueJS capture events emitted by a group of child components simultaneously?

I have a parent component that contains several child components. <grid> <cell></cell> <cell></cell> <cell></cell> </grid> The child components emit an event with a payload indicating they are bein ...

Move the text below the fixed header position

Here is a fiddle I created to address the issue mentioned: link here In this scenario, the header and footer are positioned absolutely with 100% width, while the middle content consists of a dashboard table. The header contains two images that when clicke ...

Would it be considered acceptable practice to implement a setTimeout on the loading state to ensure that the log in page does not momentarily appear before the user has completed the authentication process?

Currently, I am developing a React Native application and encountered a situation where the login page was flashing before the user authentication. To solve this issue, I added a setTimeout function as shown below: export default function App() { const [ ...

Activate javascript when the range selector button in Highstock is clicked

Is there a way I can trigger an alert("Do not disturb!") when clicking the '2Week' button in this fiddle here: https://jsfiddle.net/610335vt/ ...

Dealing with multiple jQuery ajax requests - strategies for managing them

Whenever I click the button quickly while there is jQuery Ajax loading, it seems to get stuck. How can I manage multiple requests being fired at the same time? What is the solution for the following: Cancel/abort all previous requests and only handle th ...

Avoiding divs from crashing into each other

I've developed a game feature that allows users to chat. Each message is displayed as an absolute positioned element in the body with user x position set as the "left" CSS property. These messages are animated to move to the top of the screen. I want ...

Notifying the chosen option using jQuery

I have been attempting to display an alert on the webpage based on the selected option. Unfortunately, it appears that my code is not functioning properly. This is what I have tried: $(document).ready(function(){ $("select.country").change(functio ...

Post Request to Express API does not produce any output

Today, I encountered an issue while trying to create a JWT authentication API in Express. When I send a POST request with Postman, it only returns {}. Below is the code for server.js: const express = require("express"); const mongoose = require("mongoos ...