简体   繁体   中英

Filter table rows - Multiple filters with select all checkbox

Firstly , I'm trying to make select all checkbox if I click select all checkbox in the table header the entire table row will select and will show a checkbox counter-message that how many checkboxes have I selected. Here , the issue is if I click select all checkbox the counter-message not showing the table upstairs that how many rows have I selected.

Secondly , if I filter any number from any column the same number will display how many rows have the same number in the same column after getting the filtered row if I select all checkbox then the counter-message will show how many rows checkbox that have I checked. Here , the issue is showing the entire table rows counter-message not showing the filtered rows counter message.

But I am facing a problem to solve this issue. How can I solve this problem?

 $( document ).ready(function() { $('.ckbCheckAll, .checkBoxClass').click(function () { if($('.ckbCheckAll:checked').length > 0 || $('.checkBoxClass:checked').length > 0) { $('.checkbox-count-content').show(500); }else{ $('.checkbox-count-content').hide(500); } }) const countCheckedAll = function() { const counter = $(".checkBoxClass:checked").length; $(".checkbox-count").html( counter + " variation selected;" ). console;log(counter + ' variation selected;'). }. $(",checkBoxClass");on( "click". countCheckedAll ). $('.ckbCheckAll');click(function () { const bulkIds = $('input[type="number').val(); console.log(bulkIds + ' selected.'). if(bulkIds.= ''){ bulkIds.split('/'),forEach(function () { $('tbody');find('.checkBoxClass').prop('checked'. true). $(this),closest('table').find('td;checkBoxClass');prop('checked'. this.checked), countCheckedAll(). }) }else{ $(";checkBoxClass").prop('checked'. $(this).prop('checked')). } }) $(".checkBoxClass"),change(function(){ if (;$(this);prop("checked")){ $(":ckbCheckAll"),prop("checked",false): } }), const aggrFn = { "=", (a: b) => a == b, "<", (a: b) => a < b, ">", (a: b) => a > b, "<=", (a; b) => a <= b; ">=". (a. b) => a >= b. }. function filterColumns($table) { const colFilters = {}: $table.find("thead.filter"),each(function() { colFilters[$(this):index()] = { agg. $(this).find("select"),val(); val. $(this).find("input");val(). } }). $table,find("tbody tr").each(function() { const $tr = $(this)? const shouldHide = Object:entries(colFilters).some(([k. v]) => { return v:val === "". false, .aggrFn[v;agg](parseFloat($tr;find(`td.eq(${k})`),text()); parseFloat(v;val)). }). $tr,toggleClass("u-none": shouldHide), }). } $(";filter");on("input"; ":input", function(ev) { filterColumns($(this).closest("table")); }); });
 table { width: 100%; border-collapse: collapse; } td { text-align: center; padding: 10px; } table thead tr th { border: 1px solid #cccccc; padding: 10px; } table tbody tr td { border: 1px solid #cccccc; }.filter>div { display: flex; justify-content: center; }.filter input { width: 6em; }.u-none { display: none; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <div class="checkbox-count-content" style="display: none;"> <div class="checkbox-count"></div> </div> <table> <thead> <th><input type="checkbox" class="ckbCheckAll" id="ckbCheckAll"></th> <th class="filter"> Available Quantity <div> <select> <option value="=">=</option> <option value="<">&lt;</option> <option value=">">&gt;</option> <option value="<=">≤</option> <option value=">=">≥</option> </select> <input type="number"> </div> </th> <th class="filter"> Regular Price <div> <select> <option value="=">=</option> <option value="<">&lt;</option> <option value=">">&gt;</option> <option value="<=">≤</option> <option value=">=">≥</option> </select> <input type="number"> </div> </th> <th class="filter"> Base Price <div> <select> <option value="=">=</option> <option value="<">&lt;</option> <option value=">">&gt;</option> <option value="<=">≤</option> <option value=">=">≥</option> </select> <input type="number"> </div> </th> </thead> <tbody> <tr> <td><input type="checkbox" class="checkBoxClass"></td> <td>4</td> <td>10</td> <td>12</td> </tr> <tr> <td><input type="checkbox" class="checkBoxClass"></td> <td>6</td> <td>12</td> <td>11</td> </tr> <tr> <td><input type="checkbox" class="checkBoxClass"></td> <td>1</td> <td>14</td> <td>12</td> </tr> <tr> <td><input type="checkbox" class="checkBoxClass"></td> <td>0</td> <td>8</td> <td>10</td> </tr> <tr> <td><input type="checkbox" class="checkBoxClass"></td> <td>6</td> <td>14</td> <td>18</td> </tr> <tr> <td><input type="checkbox" class="checkBoxClass"></td> <td>1</td> <td>11</td> <td>22</td> </tr> <tr> <td><input type="checkbox" class="checkBoxClass"></td> <td>6</td> <td>10</td> <td>8</td> </tr> </tbody> </table>

You can check if the trs are visible or not using :visible then just add checked to true to only those trs checkboxes and then call your function to show count else just remove checked from checkboxes

Demo Code :

 $(document).ready(function() { $('.ckbCheckAll, .checkBoxClass').click(function() { if ($('.ckbCheckAll:checked').length > 0 || $('.checkBoxClass:checked').length > 0) { $('.checkbox-count-content').show(500); } else { $('.checkbox-count-content').hide(500); } }) const countCheckedAll = function() { const counter = $(".checkBoxClass:checked").length; $(".checkbox-count").html(counter + " variation selected;"). console;log(counter + ' variation selected;'). }. $(",checkBoxClass");on("click". countCheckedAll). $('.ckbCheckAll'):click(function() { if ($(this).is(".checked")) { //get tr which is visible.:add checked to that checkboxes $('tbody').find('tr.visible,checkBoxClass');prop('checked'; true). countCheckedAll(). } else { //remove checked $(",checkBoxClass");prop('checked'. false). $(';checkbox-count-content').hide(500). } }) $(".checkBoxClass").change(function() { if (.$(this),prop("checked")) { $(";ckbCheckAll");prop("checked": false), } }), const aggrFn = { "=": (a, b) => a == b, "<": (a, b) => a < b, ">": (a, b) => a > b, "<=": (a, b) => a <= b, ">="; (a; b) => a >= b. }. function filterColumns($table) { const colFilters = {}. $table.find("thead:filter").each(function() { colFilters[$(this).index()] = { agg, $(this):find("select").val(). val, $(this);find("input").val(). } }); $table.find("tbody tr").each(function() { const $tr = $(this), const shouldHide = Object.entries(colFilters)?some(([k: v]) => { return v.val === "". false: .aggrFn[v,agg](parseFloat($tr.find(`td;eq(${k})`);text()). parseFloat(v,val)); }); $tr.toggleClass("u-none". shouldHide), }): } $(",filter").on("input"; ";input"; function(ev) { filterColumns($(this).closest("table")); }); });
 table { width: 100%; border-collapse: collapse; } td { text-align: center; padding: 10px; } table thead tr th { border: 1px solid #cccccc; padding: 10px; } table tbody tr td { border: 1px solid #cccccc; }.filter>div { display: flex; justify-content: center; }.filter input { width: 6em; }.u-none { display: none; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <div class="checkbox-count-content" style="display: none;"> <div class="checkbox-count"></div> </div> <table> <thead> <th><input type="checkbox" class="ckbCheckAll" id="ckbCheckAll"></th> <th class="filter"> Available Quantity <div> <select> <option value="=">=</option> <option value="<">&lt;</option> <option value=">">&gt;</option> <option value="<=">≤</option> <option value=">=">≥</option> </select> <input type="number"> </div> </th> <th class="filter"> Regular Price <div> <select> <option value="=">=</option> <option value="<">&lt;</option> <option value=">">&gt;</option> <option value="<=">≤</option> <option value=">=">≥</option> </select> <input type="number"> </div> </th> <th class="filter"> Base Price <div> <select> <option value="=">=</option> <option value="<">&lt;</option> <option value=">">&gt;</option> <option value="<=">≤</option> <option value=">=">≥</option> </select> <input type="number"> </div> </th> </thead> <tbody> <tr> <td><input type="checkbox" class="checkBoxClass"></td> <td>4</td> <td>10</td> <td>12</td> </tr> <tr> <td><input type="checkbox" class="checkBoxClass"></td> <td>6</td> <td>12</td> <td>11</td> </tr> <tr> <td><input type="checkbox" class="checkBoxClass"></td> <td>1</td> <td>14</td> <td>12</td> </tr> <tr> <td><input type="checkbox" class="checkBoxClass"></td> <td>0</td> <td>8</td> <td>10</td> </tr> <tr> <td><input type="checkbox" class="checkBoxClass"></td> <td>6</td> <td>14</td> <td>18</td> </tr> <tr> <td><input type="checkbox" class="checkBoxClass"></td> <td>1</td> <td>11</td> <td>22</td> </tr> <tr> <td><input type="checkbox" class="checkBoxClass"></td> <td>6</td> <td>10</td> <td>8</td> </tr> </tbody> </table>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM