|
| 1 | +import {DataTable} from "../dist/module.js" |
| 2 | +const datatable = new DataTable("#demo-table", { |
| 3 | + perPageSelect: [5, 10, 15, ["All", -1]], |
| 4 | + columns: [ |
| 5 | + { |
| 6 | + select: 1, |
| 7 | + sortable: false, |
| 8 | + searchMethod: (terms, cell, row, _column, source) => { |
| 9 | + let found |
| 10 | + if (source === "class-filter") { |
| 11 | + found = terms.find( |
| 12 | + // For each of the terms, check if at least one of the class tags in the cell exactly matches the term |
| 13 | + term => cell.data.find(span => span.attributes?.class === `class ${term}`) |
| 14 | + ) |
| 15 | + } else { |
| 16 | + // This is normal search. We just check if one of the terms is part of the text of the cell |
| 17 | + found = terms.find( |
| 18 | + term => cell.text.toLowerCase().includes(term.toLowerCase().trim()) |
| 19 | + ) |
| 20 | + } |
| 21 | + if (found) return true |
| 22 | + return false |
| 23 | + } |
| 24 | + }, |
| 25 | + { |
| 26 | + select: 4, |
| 27 | + searchMethod: (terms, cell, row, _column, source) => { |
| 28 | + if (source === "completion-filter") { |
| 29 | + const cellPercentage = parseInt(cell.data, 10) |
| 30 | + const minPercentage = parseInt(terms[0], 10) |
| 31 | + if (cellPercentage >= minPercentage) return true |
| 32 | + return false |
| 33 | + } |
| 34 | + // This is normal search. We just check if one of the terms is part of the text of the cell |
| 35 | + const found = terms.find( |
| 36 | + term => cell.data.includes(term.toLowerCase().trim()) |
| 37 | + ) |
| 38 | + |
| 39 | + if (found) return true |
| 40 | + return false |
| 41 | + } |
| 42 | + } |
| 43 | + ] |
| 44 | +}) |
| 45 | + |
| 46 | + |
| 47 | +// Multi-select dropdown |
| 48 | +let isDropdownOpen = false |
| 49 | + |
| 50 | +const toggleDropdown = () => { |
| 51 | + const checkboxes = document.getElementById("checkboxes") |
| 52 | + if (isDropdownOpen) { |
| 53 | + checkboxes.style.display = "none" |
| 54 | + isDropdownOpen = false |
| 55 | + } else { |
| 56 | + checkboxes.style.display = "block" |
| 57 | + isDropdownOpen = true |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +document.querySelector(".select-box").addEventListener("click", toggleDropdown) |
| 62 | +// Close the dropdown if the user clicks outside of it |
| 63 | +window.onclick = function(event) { |
| 64 | + if (!event.target.matches(".select-box") && !event.target.matches(".over-select") && !event.target.closest("#checkboxes")) { |
| 65 | + const checkboxes = document.getElementById("checkboxes") |
| 66 | + checkboxes.style.display = "none" |
| 67 | + isDropdownOpen = false |
| 68 | + } |
| 69 | + if (event.target.closest("#checkboxes label")) { |
| 70 | + // Change in checked classes, restart search |
| 71 | + // Get all checked checkboxes |
| 72 | + const checked = Array.from(document.querySelectorAll("#checkboxes input:checked")).map(checkbox => checkbox.value) |
| 73 | + if (!checked.length) { |
| 74 | + // Don't allow deselecting the last checkbox. |
| 75 | + event.target.closest("#checkboxes label").querySelector("input").checked = true |
| 76 | + return |
| 77 | + } |
| 78 | + datatable.multiSearch([ |
| 79 | + {terms: checked, |
| 80 | + columns: [1]} |
| 81 | + ], "class-filter") |
| 82 | + } |
| 83 | + |
| 84 | +} |
| 85 | + |
| 86 | +const updateSliderValue = value => { |
| 87 | + document.getElementById("slider-value").innerHTML = `${value}%` |
| 88 | + datatable.multiSearch([ |
| 89 | + {terms: [value], |
| 90 | + columns: [4]} |
| 91 | + ], "completion-filter") |
| 92 | +} |
| 93 | + |
| 94 | +document.querySelector("#percentage-slider").addEventListener("input", function() { |
| 95 | + updateSliderValue(this.value) |
| 96 | +}) |
| 97 | + |
| 98 | + |
| 99 | +window.dt = datatable |
0 commit comments