JustLearning123
JustLearning123

Reputation: 77

HTML Table Filter With Multiple Check Boxes

I have a javascript function that identifies all checked boxes and creates a variable to be used in the return statement of the filter, with the goal of showing only those rows whose corresponding checkbox is checked. If I type out the return function (commented out in the below code), I get the desired result. However, the variable 'filter_list' whose contents are the same as the typed out return statement, does not work.

Either I am missing something, or what I am trying to do can't be done. Looking for a fix to my code or a better way to filter based on the selections made.

script.js

$(document).ready(function(){
    var markedLocations = document.getElementsByName('location_filter');
    var i = 0
    var filter_list = ''
    for (var location of markedLocations) {
        if (location.checked){
            if (i > 0){
                var filter_list = filter_list.concat(' || $(this).find("td").eq(2).text() == "' + location.value + '"')
            }else{
                var filter_list = '$(this).find("td").eq(2).text() == "' + location.value + '"'
            }
            var i = i + 1
       }else{
        var i = i
       }
    }
    (function ($) {
        $('.searchable tr').hide();
            $('.searchable tr').filter(function(){
                console.log(filter_list)
                return filter_list
//                    return $(this).find("td").eq(2).text() == "H3A"
//                    || $(this).find("td").eq(2).text() == "H5A"
//                    || $(this).find("td").eq(2).text() == "H5B"
//                    || $(this).find("td").eq(2).text() == "H6A"
//                    || $(this).find("td").eq(2).text() == "H6B"
//                    || $(this).find("td").eq(2).text() == "Backshop"
                }).show();
            }(jQuery));
    });

dashboard.html

<div class="table header mb-2">
                    <h3 class="header mb-2 text-center">Engineering Service Request Dashboard</h3>
                    <h6 class="header mb-2 text-center" id="current-time">Last Update: {% now "m-d-Y H:i" %} EDT</h6>
                    <div class="filters">
                        {% for user in user_info %}
                            {% if user.H1A is True %}
                                <label><input value="H1A" id="H1A_filter" name="location_filter" type="checkbox" checked>H1A | </label>
                            {% else %}
                                <label><input value="H1A" id="H1A_filter" name="location_filter" type="checkbox">H1A | </label>
                            {% endif %}
                            {% if user.H1B is True %}
                                <label><input value="H1B" id="H1B_filter" name="location_filter" type="checkbox" checked>H1B | </label>
                            {% else %}
                                <label><input value="H1B" id="H1B_filter" name="location_filter" type="checkbox">H1B | </label>
                            {% endif %}
                            {% if user.H2A is True %}
                                <label><input value="H2A" id="H2A_filter" name="location_filter" type="checkbox" checked>H2A | </label>
                            {% else %}
                                <label><input value="H2A" id="H2A_filter" name="location_filter" type="checkbox">H2A | </label>
                            {% endif %}
                            {% if user.H2B is True %}
                                <label><input value="H2B" id="H2B_filter" name="location_filter" type="checkbox" checked>H2B | </label>
                            {% else %}
                                <label><input value="H2B" id="H2B_filter" name="location_filter" type="checkbox">H2B | </label>
                            {% endif %}
                            {% if user.H3A is True %}
                                <label><input value="H3A" id="H3A_filter" name="location_filter" type="checkbox" checked>H3A | </label>
                            {% else %}
                                <label><input value="H3A" id="H3A_filter" name="location_filter" type="checkbox">H3A | </label>
                            {% endif %}
                            {% if user.H3B is True %}
                                <label><input value="H3B" id="H3B_filter" name="location_filter" type="checkbox" checked>H3B | </label>
                            {% else %}
                                <label><input value="H3B" id="H3B_filter" name="location_filter" type="checkbox">H3B | </label>
                            {% endif %}
                            {% if user.H5A is True %}
                                <label><input value="H5A" id="H5A_filter" name="location_filter" type="checkbox" checked>H5A | </label>
                            {% else %}
                                <label><input value="H5A" id="H5A_filter" name="location_filter" type="checkbox">H5A | </label>
                            {% endif %}
                            {% if user.H5B is True %}
                                <label><input value="H5B" id="H5B_filter" name="location_filter" type="checkbox" checked>H5B | </label>
                            {% else %}
                                <label><input value="H5B" id="H5B_filter" name="location_filter" type="checkbox">H5B | </label>
                            {% endif %}
                            {% if user.H6A is True %}
                                <label><input value="H6A" id="H6A_filter" name="location_filter" type="checkbox" checked>H6A | </label>
                            {% else %}
                                <label><input value="H6A" id="H6A_filter" name="location_filter" type="checkbox">H6A | </label>
                            {% endif %}
                            {% if user.H6B is True %}
                                <label><input value="H6B" id="H6B_filter" name="location_filter" type="checkbox" checked>H6B | </label>
                            {% else %}
                                <label><input value="H6B" id="H6B_filter" name="location_filter" type="checkbox">H6B | </label>
                            {% endif %}
                            {% if user.backshop is True %}
                                <label><input value="Backshop" id="backshop_filter" name="location_filter" type="checkbox" checked>Backshop</label>
                            {% else %}
                                <label><input value="Backshop" id="backshop_filter" name="location_filter" type="checkbox">Backshop</label>
                            {% endif %}
                        {% endfor %}
                        <br>
                        <label><input id="filter2" type="checkbox" checked>Filter By Assigned Work Stations (Hangars)</label>
                    </div>
                    <table id="table" class="table table-bordered table-striped w-auto mx-auto"
                            data-toggle="table"
                            data-search="true"
                            data-filter-control="true"
                            data-show-multi-sort="true"
                            data-show-multi-sort-button="true"
                            data-show-print="true"
                            data-show-refresh="true"
                            data-show-fullscreen="true"
                            data-filter-control-container="true"
                            data-show-export="true">
                         <thead>
                            <tr class="text-center">
                                <th data-field="esr" data-sortable="true">ESR #</th>
                                <th data-field="status" data-filter-control="select">Status</th>
                                <th data-field="location" data-filter-control="select">Location</th>
                                <th data-field="aircraft" data-filter-control="select">Aircraft</th>
                                <th data-field="subject" data-width="35" data-width-unit="%">ESR Subject</th>
                                <th data-field="nrc_date" data-sortable="true">NRC Date</th>
                                <th data-field="esr_date" data-sortable="true">ESR Date</th>
                                <th data-field="esr_due_date" data-sortable="true">ESR Due Date</th>
                                <th data-field="rts_date" data-sortable="true">RTS Date</th>
                            </tr>
                         </thead>
                         <tbody id="dashboard" class="searchable">
                         {% for request in requests %}
                             <tr class="text-center">
                                <td>
                                    <a href="{% url 'esr_dashboard:esr-detail' request.id %}" class="request_link text-primary" data-id="{{ request.id }}">{{ request.request_number }}</a>
                                </td>
                                 <td>
                                    {{ request.status }}
                                 </td>
                                 <td>
                                    {{ request.location }}
                                </td>
                                 <td>
                                    {{ request.aircraft }}
                                </td>
                                <td>
                                    {{ request.subject }}
                                </td>
                                 <td>
                                   {{ request.NrcDate|date:"m-d-Y" }}
                                </td>
                                 <td>
                                   {{ request.timestamp|date:"m-d-Y H:i" }} EDT
                                </td>

                                 <td>
                                   {{ request.due_date|date:"m-d-Y" }}
                                </td>

                                 <td>
                                     {% if request.location == 'Backshop' %}
                                        N/A
                                     {% else %}
                                        {{ request.release_date|date:"m-d-Y H:i" }} EDT
                                     {% endif %}
                                </td>
                                 {% empty %}
                                    <td colspan=9>
                                        <p class="text-center">No active requests</p>
                                    </td>
                                {% endfor %}
                             </tr>
                         </tbody>
                    </table>
Generated HTML

<div class="table header mb-2">
                    <h3 class="header mb-2 text-center">Engineering Service Request Dashboard</h3>
                    <h6 class="header mb-2 text-center" id="current-time">Last Update: 05-04-2021 12:43 EDT</h6>
                    <div class="filters">
                        
                            
                                <label><input value="H1A" id="H1A_filter" name="location_filter" type="checkbox">H1A | </label>
                            
                            
                                <label><input value="H1B" id="H1B_filter" name="location_filter" type="checkbox">H1B | </label>
                            
                            
                                <label><input value="H2A" id="H2A_filter" name="location_filter" type="checkbox">H2A | </label>
                            
                            
                                <label><input value="H2B" id="H2B_filter" name="location_filter" type="checkbox">H2B | </label>
                            
                            
                                <label><input value="H3A" id="H3A_filter" name="location_filter" type="checkbox" checked="">H3A | </label>
                            
                            
                                <label><input value="H3B" id="H3B_filter" name="location_filter" type="checkbox">H3B | </label>
                            
                            
                                <label><input value="H5A" id="H5A_filter" name="location_filter" type="checkbox" checked="">H5A | </label>
                            
                            
                                <label><input value="H5B" id="H5B_filter" name="location_filter" type="checkbox" checked="">H5B | </label>
                            
                            
                                <label><input value="H6A" id="H6A_filter" name="location_filter" type="checkbox" checked="">H6A | </label>
                            
                            
                                <label><input value="H6B" id="H6B_filter" name="location_filter" type="checkbox" checked="">H6B | </label>
                            
                            
                                <label><input value="Backshop" id="backshop_filter" name="location_filter" type="checkbox" checked="">Backshop</label>
        </div>
        <table id="table" class="table table-bordered table-striped w-auto mx-auto table-hover" data-toggle="table" data-search="true" data-filter-control="true" data-show-multi-sort="true" data-show-multi-sort-button="true" data-show-print="true" data-show-refresh="true" data-show-fullscreen="true" data-filter-control-container="true" data-show-export="true">
                    <thead style=""><tr class="text-center">
                <th style="" data-field="esr"><div class="th-inner sortable both">ESR #</div><div class="fht-cell"></div></th>
                <th style="" data-field="status"><div class="th-inner ">Status</div><div class="fht-cell"></div></th>
                <th style="" data-field="location"><div class="th-inner ">Location</div><div class="fht-cell"></div></th>
                <th style="" data-field="aircraft"><div class="th-inner ">Aircraft</div><div class="fht-cell"></div></th>
                <th style="width: 35%; " data-field="subject"><div class="th-inner ">ESR Subject</div><div class="fht-cell"></div></th>
                <th style="" data-field="nrc_date"><div class="th-inner sortable both">NRC Date</div><div class="fht-cell"></div></th>
                <th style="" data-field="esr_date"><div class="th-inner sortable both">ESR Date</div><div class="fht-cell"></div></th>
                <th style="" data-field="esr_due_date"><div class="th-inner sortable both">ESR Due Date</div><div class="fht-cell"></div></th>
                <th style="" data-field="rts_date"><div class="th-inner sortable both">RTS Date</div><div class="fht-cell"></div></th></tr>
            </thead>
                        <tbody id="dashboard" class="searchable">
                <tr class="text-center" data-index="0" style="">
                    <td><a href="/esr_dashboard/request_details/570/" class="request_link text-primary" data-id="570">210012</a></td>
                    <td>new_status</td>
                    <td>H5B</td><td>N8652B</td>
                    <td style="width: 35%; ">TEST REFRESH SETTING</td>
                    <td>05-03-2021</td>
                    <td>05-03-2021 23:35 EDT</td>
                    <td>05-03-2021</td>
                    <td>04-30-2021 20:00 EDT</td>
                </tr>
                <tr class="text-center" data-index="1" style="">
                    <td><a href="/esr_dashboard/request_details/569/" class="request_link text-primary" data-id="569">210011</a></td>
                    <td>in_work_EG</td>
                    <td>H5B</td><td>N8652B</td>
                    <td style="width: 35%; ">Plane broke</td>
                    <td>05-02-2021</td>
                    <td>05-01-2021 20:01 EDT</td>
                    <td>05-14-2021</td>
                    <td>04-30-2021 20:00 EDT</td>
                </tr>
                <tr class="text-center" data-index="2" style="">
                    <td><a href="/esr_dashboard/request_details/568/" class="request_link text-primary" data-id="568">210010</a></td>
                    <td>new_status</td><td>H3B</td>
                    <td>N8614M</td>
                    <td style="width: 35%; ">Plane broke</td>
                    <td>05-01-2021</td>
                    <td>05-01-2021 17:28 EDT</td>
                    <td>05-01-2021</td>
                    <td>04-28-2021 03:00 EDT</td>
                </tr>
                <tr class="text-center" data-index="3" style="">
                    <td><a href="/esr_dashboard/request_details/567/" class="request_link text-primary" data-id="567">210009</a></td>
                    <td>in_work_EG</td><td>H3B</td><td>N8614M</td>
                    <td style="width: 35%; ">Fuselage skin dent at STA 540 above S-12L</td>
                    <td>05-01-2021</td>
                    <td>05-01-2021 17:05 EDT</td>
                    <td>05-14-2021</td>
                    <td>04-28-2021 03:00 EDT</td>
                </tr>
                <tr class="text-center" data-index="4" style="">
                    <td><a href="/esr_dashboard/request_details/566/" class="request_link text-primary" data-id="566">210008</a></td>
                    <td>new_status</td>
                    <td>H6A</td>
                    <td>168764</td>
                    <td style="width: 35%; ">BUNO 168764 - Alternate Procedure Request</td>
                    <td>04-29-2021</td>
                    <td>04-30-2021 13:16 EDT</td>
                    <td>05-07-2021</td>
                    <td>08-05-2021 23:00 EDT</td>
                </tr>
                <tr class="text-center" data-index="5" style="">
                    <td><a href="/esr_dashboard/request_details/565/" class="request_link text-primary" data-id="565">210007</a></td>
                    <td>new_status</td>
                    <td>H3B</td>
                    <td>N8614M</td>
                    <td style="width: 35%; ">adsafsad</td>
                    <td>04-30-2021</td>
                    <td>04-30-2021 09:55 EDT</td>
                    <td>04-30-2021</td>
                    <td>04-28-2021 03:00 EDT</td>
                </tr>
                <tr class="text-center" data-index="6" style="">
                    <td><a href="/esr_dashboard/request_details/564/" class="request_link text-primary" data-id="564">210006</a></td>
                    <td>in_work_PROD</td>
                    <td>Backshop</td>
                    <td>DoD_P-8A</td>
                    <td style="width: 35%; ">Spare aileron skin repair - alternate repair part request</td>
                    <td>04-22-2021</td>
                    <td>04-29-2021 07:55 EDT</td>
                    <td>05-07-2021</td>
                    <td>N/A</td>
                </tr>
                <tr class="text-center" data-index="7" style="">
                    <td><a href="/esr_dashboard/request_details/563/" class="request_link text-primary" data-id="563">210005</a></td>
                    <td>in_work_CUST</td>
                    <td>H3B</td>
                    <td>N8664J</td>
                    <td style="width: 35%; ">Fuselage skin dent at STA 1016+15 below S-17L</td>
                    <td>04-06-2021</td>
                    <td>04-28-2021 23:58 EDT</td>
                    <td>04-23-2021</td>
                    <td>04-09-2021 12:00 EDT</td>
                </tr>
                <tr class="text-center" data-index="8" style="">
                    <td><a href="/esr_dashboard/request_details/562/" class="request_link text-primary" data-id="562">210004</a></td>
                    <td>new_status</td>
                    <td>H5B</td>
                    <td>N8652B</td>
                    <td style="width: 35%; ">asdfasd</td>
                    <td>04-06-2021</td>
                    <td>04-28-2021 23:58 EDT</td>
                    <td>04-21-2021</td>
                    <td>04-30-2021 20:00 EDT</td>
                </tr>
                <tr class="text-center" data-index="9" style="">
                    <td><a href="/esr_dashboard/request_details/561/" class="request_link text-primary" data-id="561">210003</a></td>
                    <td>new_status</td>
                    <td>H5B</td>
                    <td>N8652B</td>
                    <td style="width: 35%; ">sadfas</td>
                    <td>04-16-2021</td>
                    <td>04-28-2021 23:57 EDT</td>
                    <td>04-30-2021</td><td>04-30-2021 20:00 EDT</td>
                </tr>
                <tr class="text-center" data-index="10" style="">
                    <td><a href="/esr_dashboard/request_details/560/" class="request_link text-primary" data-id="560">210002</a></td>
                    <td>new_status</td>
                    <td>H3B</td>
                    <td>N8614M</td>
                    <td style="width: 35%; ">asdfsad</td>
                    <td>04-28-2021</td>
                    <td>04-28-2021 23:56 EDT</td>
                    <td>04-01-2021</td>
                    <td>04-28-2021 03:00 EDT</td>
                </tr>
                <tr class="text-center" data-index="11" style="">
                    <td><a href="/esr_dashboard/request_details/559/" class="request_link text-primary" data-id="559">210001</a></td>
                    <td>new_status</td>
                    <td>H3B</td>
                    <td>N8614M</td>
                    <td style="width: 35%; ">ABCD</td>
                    <td>04-28-2021</td>
                    <td>04-28-2021 23:52 EDT</td>
                    <td>04-28-2021</td>
                    <td>04-28-2021 03:00 EDT</td>
                </tr>
            </tbody>
        </table>
</div>

Upvotes: 3

Views: 155

Answers (1)

Frenchy
Frenchy

Reputation: 17007

The problem is coming from:

filter_list = filter_list.concat(' || $(this).find("td").eq(2).text() == "' + location.value + '"');

the result is a string, and $(this) is not the same thing than '$(this)'

so if you want to execute filter_list, you have to use eval() in your code:

(function ($) {
    $('.searchable tr').hide();
        $('.searchable tr').filter(function(){
            var res = eval(filter_list);
            console.log(res);
            return res;
            :
            :

Upvotes: 1

Related Questions