Advertisement
Udoro

Filter dynamic table

Jul 28th, 2022
606
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. //SORT TABLE DATA BY NAME
  2.  
  3. //Get searchform
  4. let searchForm = document.getElementById('search_form');
  5.  
  6. //Get input
  7. let searchInput = document.getElementById('search_input');
  8.  
  9. // Get radio buttons
  10. let highlightRadio = document.getElementById('radio_highlight');
  11. let filterRadio = document.getElementById('radio_filter');
  12.  
  13.  
  14.  
  15.  
  16. //add event listener
  17.  
  18. searchForm.addEventListener('click', checkRadio);
  19.  
  20.  
  21.  
  22. //----------------------------------
  23. //Check
  24.  
  25. function checkRadio(){
  26.  
  27.  
  28.  
  29. if(highlightRadio.checked){
  30. resetFilter()
  31.  
  32. searchInput.addEventListener('keyup', highlightData);
  33.  
  34. //set input placeholder
  35. searchInput.setAttribute('placeholder','Highlight Table Data...')
  36. }
  37.  
  38. else if (filterRadio.checked){
  39.  
  40. resetHighlight()
  41.  
  42. searchInput.addEventListener('keyup', filterNames);
  43.  
  44. //set input placeholder
  45. searchInput.setAttribute('placeholder','Filter Names...')
  46. }
  47.  
  48. }
  49.  
  50. //------------------------------------
  51.  
  52.  
  53. function filterNames(){
  54.  
  55.  
  56. if(highlightRadio.checked){
  57.  
  58. return;
  59. }
  60.  
  61.  
  62.  
  63. //Get value of input
  64.  
  65. let filterValue = searchInput.value.toUpperCase();
  66.  
  67.  
  68.  
  69. // Get table row
  70.  
  71. let trow = document.querySelectorAll('.tr');
  72.  
  73. // Get names
  74. trow.forEach(tr =>{
  75.  
  76. let tdName = tr.querySelector("[data-label='Name']")
  77.  
  78.  
  79. var NameSpan = tdName.querySelector("span")
  80.  
  81.  
  82. // if matched
  83.  
  84. if(NameSpan.innerHTML.toUpperCase().indexOf(filterValue) > -1){
  85.  
  86. // set table data parent style
  87.  
  88. NameSpan.parentElement.parentElement.style.display= '';
  89.  
  90.  
  91.  
  92. } else{
  93.  
  94. NameSpan.parentElement.parentElement.style.cssText= "display: none !important";
  95. }
  96.  
  97.  
  98. })
  99.  
  100.  
  101. }
  102.  
  103. //------------------------------------
  104.  
  105.  
  106. function resetFilter(){
  107.  
  108.  
  109. //Get value of input
  110.  
  111. let filterValue = searchInput.value.toUpperCase();
  112.  
  113.  
  114. let trow = document.querySelectorAll('.tr');
  115.  
  116. // Get names
  117. trow.forEach(tr =>{
  118.  
  119. let tdName = tr.querySelector("[data-label='Name']")
  120.  
  121. tdName.parentElement.style.display= '';
  122.  
  123.  
  124. })
  125.  
  126.  
  127. }
  128.  
  129.  
  130.  
  131.  
  132. //------------------------------------
  133.  
  134. function highlightData(){
  135.  
  136. if (filterRadio.checked){
  137.  
  138. return;
  139. }
  140.  
  141.  
  142.  
  143. //Get value of input
  144.  
  145. let filterValue = searchInput.value.toUpperCase();
  146.  
  147. //console.log(filterValue)
  148.  
  149. // Get trow
  150.  
  151. let trow = document.querySelectorAll('.tr');
  152.  
  153. // Get names
  154. trow.forEach(tr =>{
  155.  
  156. let tds = tr.querySelectorAll("[data-label]")
  157.  
  158. //console.log(td.parentElement)
  159.  
  160. // if matched
  161.  
  162. tds.forEach(td => {
  163.  
  164. var tdSpan = td.querySelector("span")
  165.  
  166.  
  167.  
  168. if(tdSpan.innerHTML.toUpperCase().indexOf(filterValue) > -1 && filterValue != ''){
  169.  
  170. tdSpan.parentElement.style.backgroundColor= 'yellow';
  171.  
  172.  
  173.  
  174. } else{
  175.  
  176. tdSpan.parentElement.style.backgroundColor= 'inherit';
  177. }
  178.  
  179.  
  180.  
  181. })
  182.  
  183.  
  184.  
  185.  
  186. })
  187.  
  188.  
  189. }
  190.  
  191.  
  192.  
  193. //------------------------------------
  194.  
  195. function resetHighlight(){
  196.  
  197.  
  198. let filterValue = searchInput.value.toUpperCase();
  199. let trow = document.querySelectorAll('.tr');
  200.  
  201.  
  202. trow.forEach(tr =>{
  203.  
  204. let td = tr.querySelectorAll("[data-label]")
  205.  
  206. for (let i=0; i < td.length; i++) {
  207.  
  208. td[i].style.backgroundColor= 'inherit';
  209.  
  210. }
  211.  
  212. })
  213.  
  214.  
  215. }
  216.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement