BojidarDosev

Working HTML filter but not correct method

Aug 7th, 2025
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.74 KB | None | 0 0
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <title>Employees</title>
  5. <script src="mfiles.extensibility.protocol.js"></script>
  6. <link href="https://cdn.jsdelivr.net/npm/ag-grid-community/styles/ag-theme-alpine.css" rel="stylesheet">
  7. <script src="https://cdn.jsdelivr.net/npm/ag-grid-community/dist/ag-grid-community.min.js"></script>
  8. <link href="style.css" rel="stylesheet" />
  9. </head>
  10. <body>
  11. <div id="content"></div>
  12. <div id="toolbar">
  13. <label for="departmentFilter">Filter by Department:</label>
  14. <select id="departmentFilter">
  15. <option value="">-- Choose Department --</option>
  16. </select>
  17. <button id="clearFilter">Clear Filter</button>
  18.  
  19. <label for="jobTitleFilter">Filter by Job Title:</label>
  20. <select id="jobTitleFilter">
  21. <option value="">-- All Job Titles --</option>
  22. </select>
  23. <button id="clearJobFilter">Clear Job Filter</button>
  24. </div>
  25.  
  26. <div id="myGrid" class="ag-theme-alpine" style="height: 500px; width: 100%;"></div>
  27. <script>
  28. let gridApi = null;
  29. let allEmployees = [];
  30. let currentViewJson = null;
  31.  
  32. async function OnNewDashboard(newDashboard) {
  33. var torJsonString = await newDashboard.ShellFrame.ShellUI.Vault.VaultExtensionMethodsOperations.RunExtensionMethod({
  34. method_name: "GetEmployeeDTOs",
  35. input: "1234",
  36. });
  37. allEmployees = JSON.parse(torJsonString.output);
  38. var torJson = JSON.parse(torJsonString.output);
  39. currentViewJson = torJson;
  40.  
  41. console.log(torJson);
  42.  
  43. const gridOptions = {
  44. rowData: torJson,
  45. columnDefs: [
  46. //{
  47. { field: "fullName", headerName: "Name" },
  48. { field: "employeeId", headerName: "Employee ID" },
  49. { field: "department", headerName: "Department" },
  50. { field: "jobTitle", headerName: "Job Title" },
  51. { field: "totalDays", headerName: "Total Days" },
  52. { field: "ObjID", headerName: "ObjID", hide: true },
  53. { field: "ObjVersion", headerName: "Version", hide: true },
  54. { field: "ObjTypeID", headerName: "Type ID", hide: true }
  55.  
  56.  
  57. ],
  58. defaultColDef: {
  59. flex: 1,
  60. resizable: true,
  61. filter: true
  62. },
  63. rowData: allEmployees,
  64. onGridReady: function (params) {
  65. gridApi = params.api;
  66. }
  67. };
  68.  
  69. const gridDiv = document.querySelector("#myGrid");
  70. agGrid.createGrid(gridDiv, gridOptions);
  71. //gridApi = gridOptions; // Store the grid API for later use
  72. populateDropdown(); // Populate the department filter dropdown
  73. // Set initial row data
  74.  
  75.  
  76. /*
  77. if (gridDiv) {
  78. agGrid.createGrid(gridDiv, gridOptions);
  79. window.gridApi = gridOptions.api; // store globally
  80.  
  81. }
  82.  
  83. const result = await newDashboard.ShellFrame.ShellUI.Vault.VaultExtensionMethodsOperations.RunExtensionMethod({
  84. method_name: "GetEmployeeDTOs",
  85. input: ""
  86. });
  87. console.log("Raw result from extension:", result); // <- log entire object
  88.  
  89. //const gridApi = agGrid.createGrid(gridDiv, gridOptions);
  90. //gridApi.sizeColumnsToFit(); // Auto-resize columns to fit width
  91. */
  92. function populateDropdown() {
  93. const dropdown = document.getElementById("departmentFilter");
  94. const uniqueDepartments = [...new Set(allEmployees.map(e => e.department).filter(Boolean))];
  95.  
  96. uniqueDepartments.forEach(dept => {
  97. const option = document.createElement("option");
  98. option.value = dept;
  99. option.textContent = dept;
  100. dropdown.appendChild(option);
  101. });
  102.  
  103. dropdown.addEventListener("change", () => {
  104. const selected = dropdown.value;
  105. console.log("Selected department:", selected);
  106.  
  107. if (selected === "") {
  108. //gridApi.setRowData(allEmployees);
  109. gridApi.setGridOption("rowData", allEmployees);
  110.  
  111. } else {
  112. const filtered = allEmployees.filter(emp => emp.department === selected);
  113. console.log("Filtering by department:", filtered);
  114. console.log("gridApi:", gridApi);
  115. //gridApi.setRowData(filtered);
  116. gridApi.setGridOption("rowData", filtered);
  117. }
  118. });
  119.  
  120. document.getElementById("clearFilter").addEventListener("click", () => {
  121. dropdown.value = "";
  122. gridApi.setGridOption("rowData", currentViewJson);
  123. // gridApi.setRowData(allEmployees);
  124. });
  125.  
  126. const jobTitleDropdown = document.getElementById("jobTitleFilter");
  127. const uniqueJobTitles = [...new Set(allEmployees.map(e => e.jobTitle).filter(Boolean))];
  128.  
  129. uniqueJobTitles.forEach(title => {
  130. const option = document.createElement("option");
  131. option.value = title;
  132. option.textContent = title;
  133. jobTitleDropdown.appendChild(option);
  134. });
  135.  
  136. jobTitleDropdown.addEventListener("change", () => {
  137. const selected = jobTitleDropdown.value;
  138. console.log("Selected job title:", selected);
  139. if (selected === "") {
  140. //gridApi.setRowData(allEmployees);
  141. gridApi.setGridOption("rowData", allEmployees);
  142. } else {
  143. const filtered = allEmployees.filter(emp => emp.jobTitle === selected);
  144. console.log("Filtering by job title:", filtered);
  145. //gridApi.setRowData(filtered);
  146. gridApi.setGridOption("rowData", filtered);
  147. }
  148. });
  149.  
  150. document.getElementById("clearJobFilter").addEventListener("click", () => {
  151. jobTitleDropdown.value = "";
  152. gridApi.setGridOption("rowData", currentViewJson);
  153. // gridApi.setRowData(allEmployees);
  154. });
  155.  
  156. }
  157. }
  158. </script>
  159.  
  160. </body>
  161. </html>
  162.  
Advertisement
Add Comment
Please, Sign In to add comment