Javascript -show the particular table row based on checkbox

imran_44

I want to display the table row based on the checkbox selection where I need to display only the particular row. I'm noob in JavaScript and took this snippet from How to show table rows based on checkbox selected

I tried to modify the code as per the requirement but I couldn't able to figure out which I retrieve from the database

In the JS script it matching the td value but I don't mention the table value explicitly all it comes from the db.

           <div>Country</div>
            <div class="row" name="country_checkbox" id="id_row"  onclick="return filter_type(this);">
             <ul id="id_country">
            <li><label for="id_country_0"><input type="checkbox" name="country" value="AMERICA" placeholder="Select Country" id="id_country_0">
         AMERICA</label>
        
        </li>
            <li><label for="id_country_3"><input type="checkbox" name="country" value="newyork" placeholder="Select Country" id="id_country_3">
         newyork</label>
        </li>

        <li><label for="id_country_2"><input type="checkbox" name="country" value="china" 
        placeholder="Select Country" id="id_country_2">china</label>
        </li>
        
         </ul>
                    </div>
            <table class="datatable" id='table_id'> 
              <thead>
                <thead>
                  <tr>
                    <th>Region</th>
                    <th> Area </th>
                    <th> Country </th>
                 </tr>
                </thead>
     
              <tbody>
                <tr id="trow">
                 {% for i in database%}
                  <td>i.Region</td>
                  <td>i.Area </td>
                  <td>i.Country</td>
                 </tr>
              </tbody>



    <script>
    // checkbox selection
    
         function filter_type(box) {
        //alert("checked");
         var cbs = document.getElementsByTagName('input');
         var all_checked_types = [];
         for(var i=0; i < cbs.length; i++) {
             if(cbs[i].type == "checkbox") {
                     if(cbs[i].name.match(/^country/)) {
                             if(cbs[i].checked) {
                                 all_checked_types.push(cbs[i].value);
                              }
                          }
                   }
          }
    
         if (all_checked_types.length > 0) {
             $('.dataclass tr:not(:has(th))').each(function (i, row) {
                 var $tds = $(this).find('td'),
                 type = $tds.eq(2).text();
                 if (type && all_checked_types.indexOf(type) >= 0) {
                     $(this).show();
                  }else{
                     $(this).hide();
                  }
              });
          }else {
                $('.datatbl tr:not(:has(th))').each(function (i, row) {
                    var $tds = $(this).find('td'),
                    type = $tds.eq(2).text();
                    $(this).show();
                 });
        }
        return true;
     }  
    
    </script>
fraaahzeep

Your approach could possibly use some work. W3 has a good example of filtering based on inputs that could be helpful in getting you on the right track.

Below I have provided a working example, using the W3 code as a starting place. This should give you an idea how to implement your desired solution.

In the code you provided it appears that you may be working with jQuery. The solution below is pure JS and does not require any outside libraries. In addition to modifying the JS filtering function (filter_type), we moved the onclick attribute from the div holding all the checkboxes and placed it on each individual checkbox input.

           <div>Country</div>
       <div class="row" name="country_checkbox" id="id_row">
         <ul id="id_country">
           <li><label for="id_country_0"><input type="checkbox" name="country" value="NORTHAMERICA" placeholder="Select Country" id="id_country_0" onclick="filter_type();">
               NORTHAMERICA</label>

           </li>
           <li><label for="id_country_3"><input type="checkbox" name="country" value="LATAM" placeholder="Select Country" id="id_country_3" onclick="filter_type();">
               LATAM</label>
           </li>

           <li><label for="id_country_2"><input type="checkbox" name="country" value="ASIA" placeholder="Select Country" id="id_country_2" onclick="filter_type();">ASIA</label>
           </li>

         </ul>
       </div>
       <table class="datatable" id='table_id'>
         <thead>
           <thead>
             <tr>
               <th>Region</th>
               <th> Area </th>
               <th> Country </th>
             </tr>
           </thead>

         <tbody>
           <tr id="trow_1">
             <td>i.Region</td>
             <td>i.Area </td>
             <td>NORTHAMERICA</td>
           </tr>
           <tr id="trow_2">
             <td>i.Region</td>
             <td>i.Area </td>
             <td>LATAM</td>
           </tr>
           <tr id="trow_3">
             <td>i.Region</td>
             <td>i.Area </td>
             <td>ASIA</td>
           </tr>
         </tbody>



         <script>
           function filter_type() {
             //declare variables
             var inputs, input, filters, table, tr, td, i, r;
             //get all checkbox inputs
             inputs = document.querySelectorAll("input[type=checkbox]");
             table = document.getElementById("table_id");
             tr = table.getElementsByTagName("tr");
             //array to hold filters
             filters = [];

             //loop through inputs and add value for all checked to filters
             for (i = 0; i < inputs.length; i++) {
               input = inputs[i];
               if (input.checked) {
                 filters.push(input.value);
               }
             }

             //loop through each row in table
             for (r = 0; r < tr.length; r++) {
               //get column to compare to, in this case the 3rd column
               td = tr[r].getElementsByTagName("td")[2];
               if (td) {
                 //get value of the column
                 txtValue = td.textContent || td.innerText;
                 //check if column value is the filters array
                 //or if filters array is empty, show all rows
                 if (filters.indexOf(txtValue) > -1 || filters.length == 0) {
                   //true, show row
                   tr[r].style.display = "";
                 } else {
                   //false, hide row
                   tr[r].style.display = "none";
                 }
               }
             }

           }

         </script>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Hide/Show table row(s) based on checkbox

jquery how to show a table row based on an html5 attribute of a checked checkbox

Show or hide table row if checkbox is checked

Show/Hide Table Rows based on Checkbox

Show and hide table based on checkbox selection not working

Hide table row based on checkbox in pure CSS

Javascript Get the text in a particular table cell in (this) row

How to display particular table row in javascript

Adding unique id to each row to hide/show table row based on column value - Laravel - javascript

Update an input if checkbox is selected on table row in javascript

Collapsing Table Row Using ASP Checkbox and JavaScript

Show Hide Table Row based on Check Box

Angular Material: Show checkbox when mouse over table row

Show mat-table row dynamically after checking a checkbox

delete particular row in a table

React - move row of data to new table based on checkbox

cannot unhide/hide table row based on a checkbox.

sql result show in particular row

How to show/hide menu based on checkbox(s) value with JavaScript?

How to enable and disable particular row in DataGridView by checkbox?

How to Get the Second Checkbox Value in a Table Row Using Javascript

javascript getting input checkbox on table using col and row

How to get this oninput JavaScript to check the checkbox by row of a table

JQuery: Table Row Show/Hide based on Column Value

SQL Hide/Show rows based on row count from another table

Show or hide a table row based on a Radio button value

Send a particular row of a table to a function

Add/Remove Table Columns in JavaScript Based On Checkbox Status

Show image based on checkbox selection

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive