/    Sign up×
Community /Pin to ProfileBookmark

Filter Table AND hide rows javascript issue

I have two JavaScript functions: one toggles (hide/show) a table row when a link is clicked, and another that filters table rows (showing only rows with text that’s entered into a text field). When someone enters text into the text field, I want to be able to hide all of the shown table rows (that were toggled). So, basically, I’m trying to add part of the toggle function to the filter function. The toggle function needs to function on its own, but all items will be hidden when text is entered.

**Toggle Function**

If a table row has an ID (id=”celltohid1″ or id=”celltohid1″), then the following function hides or shows that row when a link it clicked…

“`
<script type=”text/javascript”>
<!–
function toggle_visibility(id) {
var e = document.getElementById(id);
if(e.style.display == ‘table-row’)
e.style.display = ‘none’;
else
e.style.display = ‘table-row’;
}
//–>
</script>
“`

**Filter Function**

Below is the Filter function…

“`
<script>
$(document).ready(function(){
$(“.search”).on(“keyup”,function(){
var searchTerm = $(this).val().toLowerCase();
$(“#DMTbl tbody tr.contentrow”).each(function(){
var lineStr = $(this).text().toLowerCase();
if(lineStr.indexOf(searchTerm) === -1){
$(this).hide();
}else{
$(this).show();
}
});
});
});
</script>
“`

**Goal (and my weak attempt)**

I need to add functionality that hides all the rows with IDs that contain “celltohid” (since all the id’s are in the format `celltohid#`) … I don’t know JavaScript very well, so below is my weak attempt at this. Every row with the unique ids “celltohid#” has a class “descexpand”. So, I tried adding a toggle to change the display style for that class (since it is the same rows with the id affected above) … but it’s not working…

“`

<script>
$(document).ready(function(){
$(“.search”).on(“keyup”,function(){
var searchTerm = $(this).val().toLowerCase();
$(“#DMTbl tbody tr.contentrow”).each(function(){
var lineStr = $(this).text().toLowerCase();
if(lineStr.indexOf(searchTerm) === -1){
$(this).hide();
}else{
$(this).show();
}
var clasname = “descexpand”;
var e = document.getElementByClass(clasnam);
if(e.style.display == “table-row”) {
e.style.display = “none”;
}
});
});
});
</script>

“`

Can anyone help?

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumAug 18.2020 — You posted the function toggle_visibility. How is this function called or triggered? There has to be an event listener.

Additionally I do not understand this:
>I need to add functionality that hides all the rows with IDs that contain "celltohid" (since all the id's are in the format celltohid#)

It would be helpful if you posted some rows of the table.
Copy linkTweet thisAlerts:
@SempervivumAug 19.2020 — I made a small sample table and coded some javascript:
&lt;table id="DMTbl" style="width:100%"&gt;
&lt;tr&gt;
&lt;th&gt;Firstname&lt;/th&gt;
&lt;th&gt;Lastname&lt;/th&gt;
&lt;th&gt;Age&lt;/th&gt;
&lt;/tr&gt;
&lt;tr id="desc1" class="contentrow"&gt;
&lt;td&gt;Jill&lt;/td&gt;
&lt;td&gt;Smith&lt;/td&gt;
&lt;td&gt;50&lt;/td&gt;
&lt;/tr&gt;
&lt;tr id="desc2" class="contentrow"&gt;
&lt;td&gt;Eve&lt;/td&gt;
&lt;td&gt;Jackson&lt;/td&gt;
&lt;td&gt;94&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;table class="dmtables"&gt;
&lt;tbody&gt;
&lt;tr class="searchrow"&gt;
&lt;td&gt;
Filter List:&lt;/td&gt;
&lt;td&gt;
&lt;input type="text" class="search form-control input-txt" placeholder=""&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;a href="javascript:void(0)" onclick="toggle_visibility('desc1');" class="dmtabexplink "&gt;Details 1&lt;/a&gt;
&lt;a href="javascript:void(0)" onclick="toggle_visibility('desc2');" class="dmtabexplink "&gt;Details 2&lt;/a&gt;
&lt;script&gt;
function toggle_visibility(id) {
const ele = document.getElementById(id);
ele.classList.toggle('hidden-toggle');
}
document.querySelector('.search').addEventListener('keyup', event =&gt; {
const searchTerm = event.target.value.toLowerCase();
document.querySelectorAll('#DMTbl tbody tr.contentrow').forEach(row =&gt; {
const lineStr = row.textContent.toLowerCase();
if (lineStr.indexOf(searchTerm) === -1) {
row.classList.add('hidden-search');
} else {
row.classList.remove('hidden-search');
}
});
});
&lt;/script&gt;
Place the script below the HTML of the table, right before the closing </body>This script adds or removes classes that control the visibility of the table rows by CSS:
&lt;style&gt;
tr.hidden-toggle {
display: none;
}

<i> </i> tr.hidden-search {
<i> </i> display: none;
<i> </i> }
<i> </i>&lt;/style&gt;

Check if this fits your needs and come back if it does not.
Copy linkTweet thisAlerts:
@jabbamonkyauthorAug 20.2020 — @Sempervivum#1622293 I put your code into Codepen to see if it worked, but it didn't seem to do anything...

https://codepen.io/jabbamonkey/pen/eYZBYXN

I uploaded the page to my server, so you can see it here...

https://www.thievesguild.cc/5e/tables/weapons

The toggle is INSIDE the table (and toggles the view of an "expanded" row). So, when someone filters the list, the "expanded rows" are viewable and searchable. I want those expanded rows hidden when someone enters anything in the search field. But, if the list is filtered, the user still needs to be able to expand the row if needed. Is this possible?
Copy linkTweet thisAlerts:
@SempervivumAug 20.2020 — Two reasons:
  • 1. `&lt;script&gt; ... &lt;/script&gt;</C> tags are not allowed in the Javascript tab, delete them.</LI>
    <LI>2. You forgot about the CSS, add it in the CSS tab, without <C>
    &lt;style&gt; ... &lt;/style&gt;`
  • ×

    Success!

    Help @jabbamonky spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 6.2,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,
    )...