Interested in learning how to filter nested tables?

After successfully integrating a filter code snippet, I encountered an issue with the filter not recognizing data tables that can only be inserted as nested tables. Due to my limited knowledge of JavaScript/CSS, I'm unsure if this problem can be resolved.

https://i.sstatic.net/xFZJZ.png

https://i.sstatic.net/SuLtv.png

JAVASCRIPT

var input, table, rows, noMatches, markInstance;

window.onload = function init() {

  input = document.getElementById('myInput');

  noMatches = document.getElementById('noMatches');

  table = document.getElementById('myTable');

  rows = table.querySelectorAll('tr');

  markInstance = new Mark(table);

  input.addEventListener('keyup', _.debounce(ContactsearchFX, 250));

var input, table, rows, noMatches, markInstance;

window.onload = function init() {

  input = document.getElementById('myInput');

  noMatches = document.getElementById('noMatches');

  table = document.getElementById('myTable');

  rows = table.querySelectorAll('tr');

  markInstance = new Mark(table);

  input.addEventListener('keyup', _.debounce(ContactsearchFX, 250));

}

function ContactsearchFX() {

  resetContent();

  markInstance.unmark({

    done: highlightMatches

  });

}

function resetContent() {

 noMatches.textContent = '';

  rows.forEach(function(row) {

    row.classList.remove('show');

  });

}

function highlightMatches() {

  markInstance.mark(input.value, {

    each: showParantRow,

    noMatch: onNoMatches,

  })

}

function showParantRow(element) {

  var row = element.closest('tr');

  row.classList.add('show');

}

function onNoMatches(text) {

  noMatches.textContent = 'No records match: "' + text + '"';

};
.input-wrap {

  margin-bottom: 12px;

}
#myInput:invalid~.hints {

  display: block;

}
#noMatches:empty, #noMatches:empty + .hints {

  display: none;

}
.style1 tr {

  display: none;

}
.style1 .show {

  display: table-row;

}
mark {

  background: orange;

  font-weight: bold;

  color: black;

}
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5b37343f3a28331b6f756a6c756a6a">[email protected]</a>/lodash.min.js">
</script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/mark.js/8.11.1
/mark.min.js"></script>
<div class="input-wrap">
  <label>
    Search Titles:
    <input id="myInput" type="text" required
           placeholder="Search Titles" />
  </label>
</div>
<div class="hintsWrap">
  <p id="noMatches"></p>
  <p class="hints">
    Hints: type "not working", "working" ...
  </p>
</div>
<table id="myTable" style="width: 100%" class="style1">
  <tr>
    <td>
    <table><tr><td>not working</td></tr></table>  
    </td>
  </tr>
  <tr>
    <td>
    working
    </td>
  </tr>
</table>

Answer №1

Displaying an entire table with a title search can be problematic. (I'm implementing the JavaScript and CSS you provided) I appreciate your insights, Seraphendipity, and am eager to hear more of your thoughts.

Here is the HTML code:

<div class="input-wrap">
  <label>
Search Titles: 
<input id="myInput" type="text" required
       placeholder="Search Titles" />
  </label>
</div>

<div class="hintsWrap">
  <p id="noMatches"></p>
  <p class="hints">
    Hints: Type "Title1", "Title2", "Title3"...
  </p>
</div>

<table id="myTable" style="width: 100%" class="style1">
  <tr>
    <td>

        <br />

    <table style="width: 100%">
        <tr>
            <td>Title1</td>
        </tr>
        <tr>
            <td>
            <img data-src-                        hq="/th?id=ODL.53f33f407ba28930afd14f3d9390813c&amp;w=197&amp;        h=110&amp;c=7&amp;rs=1&amp;qlt=80&amp;dpr=1.76&amp;pid=RichNav"     alt="Iran seizes 'fuel-smuggling' tanker in Gulf" data-    priority="2" id="emb6E9DDF7A" class="rms_img"         src="https://www.bing.com        /th?id=ODL.53f33f407ba28930afd14f3d9390813c&amp;w=197&amp;h=110&    amp;c=7&amp;rs=1&amp;qlt=80&amp;dpr=1.76&amp;pid=RichNav" data-        bm="50" width="197" height="110" />Description1</td>
        </tr>
        <tr>
            <td>Date1</td>
        </tr>
    </table>
    </td>
</tr>
</table>

    <br />

<table id="myTable0" style="width: 100%" class="style1">
  <tr>
    <td>

    <br />

    <table style="width: 100%">
        <tr>
            <td>Title2</td>
        </tr>
        <tr>
            <td>
            <img data-src-    hq="/th?id=ODL.22386d3e20f0b540ee8cc4874bdd4ba4&amp;w=197&amp;h=110&amp;c=7&amp;rs=1&amp;qlt=80&amp;dpr=1.76&amp;pid=RichNav" alt="Flying ants: Swarms appear on weather map as 'rain'" data-priority="2" id="emb13B6D9078" class="rms_img" src="https://www.bing.com/th?id=ODL.22386d3e20f0b540ee8cc4874bdd4ba4&amp;w=197&amp;h=110&amp;c=7&amp;rs=1&amp;qlt=80&amp;dpr=1.76&amp;pid=RichNav" data-bm="51" width="197" height="110" />Description2</td>
        </tr>
        <tr>
            <td>Date2</td>
        </tr>
    </table>
    </td>
</tr>
</table>

    <br />

<table id="myTable1" style="width: 100%" class="style1">
  <tr>
            <td>&nbsp;</td>
        </tr>
  <tr>
    <td>

    <br />

    <table style="width: 100%">
        <tr>
            <td>Title3</td>
        </tr>
        <tr>
            <td>
            <img data-src-        hq="/th?id=ODL.c9ffada4f6eedff438c5c3c2c26b73f4&amp;w=197&amp;h=110&amp;c=7&amp;rs=1&amp;qlt=80&amp;dpr=1.76&amp;pid=RichNav" alt="MPs back bid to block Parliament suspension" data-priority="2" id="emb27B566CEC" class="rms_img" src="https://www.bing.com/th?id=ODL.c9ffada4f6eedff438c5c3c2c26b73f4&amp;w=197&amp;h=110&amp;c=7&amp;rs=1&amp;qlt=80&amp;dpr=1.76&amp;pid=RichNav" data-bm="52" width="197" height="110" />Description3</td>
        </tr>       <tr>
            <td>Date3</td>
        </tr>
    </table>
    <br />
    </td>
</tr>
</table>

Answer №2

Hey there, does this catch your eye, or were you aiming for something different?

I concentrated on ensuring that you apply the show classes to all parent <tr> elements of matched items and remove the class on reset. Additionally, using $() tags around elements allows you to properly access their object model -- it seemed like you were attempting jQuery manipulation on the HTML elements directly.

CSS and HTML mostly unchanged. I'm not sure what the noMatches and .hint were meant to be -- some kind of feedback element?

HTML:

<!DOCTYPE html> <head>


<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="600c0f0401130820544e51574e5151">[email protected]</a>/lodash.min.js">
</script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/mark.js/8.11.1
/mark.min.js"></script>
</head>
<body>
<div class="input-wrap">

  <label>

    Search Titles: 

    <input id="myInput" type="text" required

           placeholder="Search Titles" />

  </label>

</div>



<div class="hintsWrap">

  <p id="noMatches"></p>

  <p class="hints">

    Hints: type "not working", "working" ...

  </p>

</div>



<table id="myTable" style="width: 100%" class="style1">

  <tr>

    <td>

    <table><tr><td>not working</td></tr></table>  

    </td>

  </tr>

  <tr>

    <td>

    working

    </td>

  </tr>

</table>
</body>

CSS:

.input-wrap {

  margin-bottom: 12px;

}





#myInput:invalid~.hints {

  display: block;

}



#noMatches:empty, #noMatches:empty + .hints {

  display: none;

}


.style1 tr {

  display: none;

}



.style1 .show {

  display: table-row;

}



mark {

  background: orange;

  font-weight: bold;

  color: black;

}

JS:

var input, table, rows, noMatches, markInstance;


$(document).ready(function init() {


  input = document.getElementById('myInput');

  noMatches = document.getElementById('noMatches');

  table = document.getElementById('myTable');

  rows = table.querySelectorAll('tr');

  markInstance = new Mark(table);



  input.addEventListener('keyup', _.debounce(ContactsearchFX, 250));

});



function ContactsearchFX() {
  resetContent();
  markInstance.unmark({ done: highlightMatches });
}



function resetContent() {

    $('.noMatchErrorText').remove(); 
    //Remove this line to have a log of searches

    //noMatches.textContent = '';
  rows.forEach(function(row) {
    $(row).removeClass('show'); 

  });

}



function highlightMatches() {

  markInstance.mark(input.value, {

    each: showRow,

    noMatch: onNoMatches,

  })

}



function showRow(element) {
//alert(element);
  $(element).parents('tr').addClass('show');
        //Parents in case of several nestings

}



function onNoMatches(text) {
  $('#myInput').after('<p class="noMatchErrorText">No records match: "' + text + '"</p>'); 
}

Further Nesting...

To address your comments on adding the ability to show the full sub-table, one would likely add this line in JS:

$(element).parents('tr').siblings('tr').addClass('show');
. This tells not only the parent table row to display properly but also any siblings of that parent that are <tr> tags, thus as long as the table is at least semi-normal, it'll show the table of the matched items. Try typing "Title" -- notice that it shows both tables. Then try "Title2," and note that it only shows the relevant table, as the <tr> tags of the first table are cousins, not siblings.

Also, I modified the HTML a bit. To have it work properly, you either need a parent table with id="myTable" that all of the tables are under, OR you can set up myTable as a class, perhaps with a few changes in the code to account for the fact that table is an array.

New HTML (new content):

<!DOCTYPE html> <head>


<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0e62616a6f7d664e3a203f39203f3f">[email protected]</a>/lodash.min.js">
</script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/mark.js/8.11.1
/mark.min.js"></script>
</head>
<body>
<div class="input-wrap">
  <label>
Search Titles: 
<input id="myInput" type="text" required
       placeholder="Search Titles" />
  </label>
</div>

<div class="hintsWrap">
  <p id="noMatches"></p>
  <p class="hints">
    Hints: type "Title1", "Title2", "Title3"...
  </p>
</div>



<table id="myTable" style="width: 100%" class="style1">
<tr><td><table id="myTabledawa" style="width: 100%" class="style1">
  <tr>
    <td>

        <br />

    <table style="width: 100%">
        <tr>
            <td>Title1</td>
        </tr>
        <tr>
            <td>
            <img data-src-                        hq="/th?id=ODL.53f33f407ba28930afd14f3d9390813c&amp;w=197&amp;        h=110&amp;c=7&amp;rs=1&amp;qlt=80&amp;dpr=1.76&amp;pid=RichNav"     alt="Iran seizes 'fuel-smuggling' tanker in Gulf" data-    priority="2" id="emb6E9DDF7A" class="rms_img"         src="https://www.bing.com        /th?id=ODL.53f33f407ba28930afd14f3d9390813c&amp;w=197&amp;h=110&    amp;c=7&amp;rs=1&amp;qlt=80&amp;dpr=1.76&amp;pid=RichNav" data-        bm="50" width="197" height="110" />Description1</td>
        </tr>
        <tr>
            <td>Date1</td>
        </tr>
    </table>
    </td>
    </tr></table></td></tr>



<tr><td>
<table id="myTable0" style="width: 100%" class="style1">
  <tr>
    <td>

    <br />

    <table style="width: 100%">
        <tr>
            <td>Title2</td>
        </tr>
        <tr>
            <td>
            <img data-src-    hq="/th?id=ODL.22386d3e20f0b540ee8cc4874bdd4ba4&amp;w=197&amp;h=110&amp;c=7&amp;rs=1&amp;qlt=80&amp;dpr=1.76&amp;pid=RichNav" alt="Flying ants: Swarms appear on weather map as 'rain'" data-priority="2" id="emb13B6D9078" class="rms_img" src="https://www.bing.com/th?id=ODL.22386d3e20f0b540ee8cc4874bdd4ba4&amp;w=197&amp;h=110&amp;c=7&amp;rs=1&amp;qlt=80&amp;dpr=1.76&amp;pid=RichNav" data-bm="51" width="197" height="110" />Description2</td>
        </tr>
        <tr>
            <td>Date2</td>
        </tr>
    </table>
    </td>
</tr>
</table></td></tr>
</table>

</body>

CSS remains exactly the same.

New JS (small edit):

var input, table, rows, noMatches, markInstance;

$(document).ready(function init() {

  input = document.getElementById('myInput');

  noMatches = document.getElementById('noMatches');

  table = document.getElementById('myTable');

  rows = table.querySelectorAll('tr');

  markInstance = new Mark(table);

  input.addEventListener('keyup', _.debounce(ContactsearchFX, 250));
});

function ContactsearchFX() {
  resetContent();
  markInstance.unmark({ done: highlightMatches });
}

function resetContent() {

    $('.noMatchErrorText').remove(); 
    //Remove this line to have a log of searches

    //noMatches.textContent = '';
  rows.forEach(function(row) {
    $(row).removeClass('show'); 
  });
}

function highlightMatches() {

  markInstance.mark(input.value, {
    each: showRow,
    noMatch: onNoMatches,
  })
}

function showRow(element) {
//alert(element);
  $(element).parents('tr').addClass('show');  $(element).parents('tr').siblings('tr').addClass('show');
        //Parents incase of several nestings
}

function onNoMatches(text) {
  $('#myInput').after('<p class="noMatchErrorText">No records match: "' + text + '"</p>'); 
}

Example Fiddle of Showing Full 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

JQuery: Creating a visually striking screen flash

How can I make the window or page flash/blink using JQuery or plain JavaScript? I also want to get the tab to flash at the same time. Currently, the code I have is: var flash = true; var alert = setInterval(function(){ if (flash) //doFlash ...

Tips for incorporating VueJS 2 global components within single file components

As I attempt to utilize a globally registered component (using Vue.component) within a single file component, I consistently encounter the following warning: vue.common.js:2611[Vue warn]: Unknown custom element: <my-component> - did you register the ...

The Power of AngularJS - Unlocking the Potential of Module Configuration

Exploring the concepts in this AngularJS example: angular.module('myModule', [], function($provide) { $provide.factory('serviceId', function() { var shinyNewServiceInstance; //the factory function creates shinyNewServiceInsta ...

Absolute positioning causes an element's height to increase

As I delve into the realm of typographical animations and seek to calculate the baseline of a font at various sizes, I've stumbled upon a peculiar discovery: It appears that the height values of elements tend to increase in an erratic manner when thei ...

Performing multiple asynchronous tasks using RxJS by running Array.prototype.map in parallel batches or queues

Imagine having an array of variables, such as: [Sasha, Misha, Caitlyn, ...String] (string[]) with a sizable length of about 10k elements. If you want to run an asynchronous parallel task with these elements, but not all at once like Promise.all, rather in ...

The success callback is not triggered when making a JSONP request

I have a specific URL that returns JSON data when accessed through a browser or REST client. However, I am having trouble making the request using jQuery in my express server running over HTTPS. Despite receiving a successful response in the network debug ...

Leveraging jQuery chosen for interactive form elements

This Table Row contains 5 input fields, with the first being a select box. I am utilizing the chosen jQuery plugin to enable search functionality for the select items. Since this is a dynamic form, I am duplicating these rows. However, I am facing an issu ...

What is the most efficient method for storing text in my web application?

Hey there, I'm looking for a way to easily store and access the wording used in my application so that I can use them again whenever needed. For example, loading messages, validation messages, or informational messages are some of the wordings I want ...

Tips for utilizing comment tags efficiently

I have encountered an issue with IE7 where my CSS is not functioning properly. In an attempt to target IE7 and lower versions specifically, I inserted IE comment tags into the HTML code. However, despite my efforts, it does not seem to be effective. Initia ...

What steps are involved in deploying a Vue.js application on a tomcat server?

Is it possible to deploy a vue.js application on a server using tomcat? If so, how can this be done? ...

An HTML attribute with a blank value will not display the equals sign operator

jQuery can be used like this: $select.append('<option value="">All</option>'); This code appears to insert the element in HTML as follows: <option value>All</option> However, what is intended is to append the elemen ...

function executed when meteor template finishes loading all content

Here is a template structure that I am working with: <template name="mainEvents"> <section class="main-events-list events-list js-content-slider"> {{#each events}} <div class="events-list-item"> &l ...

Encountering issues with Jest Setup in Next.js as it appears to unexpectedly include a React import in index.test.js

Hey there, I've been pondering over this issue for the past few days. It appears to be a common error with multiple solutions. I'm facing the dreaded: Jest encountered an unexpected token /__tests__/index.test.js:16 import React from "r ...

What is the best way to extract data from several SQL tables and display it in an HTML/PHP table?

In my database named result, I have two tables named res and sub. Here is an example of the contents in the res table: sno regno name sub1 sub2 sub3 sub4 sub5 1 1DU12CS100 student1 70 80 85 70 90 2 1DU12CS101 ...

Redux accesses the store data after a brief delay

I am new to using redux and I am currently working on implementing it for my very first project. Everything seems to be working fine, however, when I try to console.log the data from the redux store, initially it shows two empty arrays but in the subsequen ...

Performing an Ajax Get Request in Rails 4 without rendering a view

Welcome to a unique question that has been carefully crafted to stand out from the rest. After hours of dedicated research, it seems like the right terms are still eluding me. In the world of Rails 4, my aim is to utilize an ajax request to fetch data fro ...

Enhance the functionality of Map.set by automatically storing the new entry on the disk as well

This code is intended for development purposes only, to resume work from where I left off when restarting the local server. I aim to avoid modifying the production code in any way. My goal is to save new entries to disk when using map.set(...). Below is a ...

Swap the particular term within the href attribute to .data()

Is there a way to dynamically update the CSS file being linked in my website based on radio button selections? Here's an example of what I am trying to achieve: <link id="custom-css" href="mysite/css/color1.css" rel="stylesheet" type="text/css"/&g ...

Jquery animation in Wordpress without any need for scrolling

My Wordpress site features some animations that work flawlessly when the site is scrolled. The code looks like this: jQuery(document).ready(function($){ $(window).scroll( function(){ if(!isMobile) { $('.animate_1').each ...

Modify the `value` of the `<input type=color>` element

Hello there! I have been working on a feature where I need users to select a color and have that choice reflected in the value field. Initially, I assumed this could be achieved easily through Bootstrap's features since my project is based on Bootstr ...