function FilterLookupFieldBasedOnUserLocations(optionSetField,locations,TypeofRecord) { // Supprimer les espaces ou caractères inutiles var cleanData = locations.trim(); // Convertir le contenu en objet JavaScript var locationList = JSON.parse(cleanData); var locationIDs = []; switch (TypeofRecord) { case "Location": locationIDs = [ ...new Set(locationList.map((location) => location.LocationID)), ]; break; case "Vendor": locationIDs = [ ...new Set(locationList.map((location) => location.VendorID)), ]; break; case "Customer": locationIDs = [ ...new Set(locationList.map((location) => location.CustomerID)), ]; break; case "Item": locationIDs = [ ...new Set(locationList.map((location) => location.ItemID)), ]; break; }; // Filter the Option Set FilterOptionSet(locationIDs, optionSetField); }; function FilterFieldIOnLocationChange(optionSetField,locations,TypeofRecord,selectedLocationID) { // Supprimer les espaces ou caractères inutiles var cleanData = locations.trim(); // Convertir le contenu en objet JavaScript var locationList = JSON.parse(cleanData); const filteredLocations = locationList.filter(item => item.LocationID === selectedLocationID); var locationIDs = []; if (selectedLocationID) { locationIDs.push(selectedLocationID); // Add the selected value to the array } switch (TypeofRecord) { case "Location": locationIDs = [ ...new Set(filteredLocations.map((location) => location.LocationID)), ]; break; case "Vendor": locationIDs = [ ...new Set(filteredLocations.map((location) => location.VendorID)), ]; break; case "Customer": locationIDs = [ ...new Set(filteredLocations.map((location) => location.CustomerID)), ]; break; case "Item": locationIDs = [ ...new Set(filteredLocations.map((location) => location.ItemID)), ]; break; } FilterOptionSet(locationIDs, optionSetField); }; function FilterOptionSet(validIDs, optionSetField) { // Iterate through the options in the dropdown optionSetField.find("option").each(function () { const optionValue = $(this).val(); // Get the value of the option // Hide the option if its value is not in the validIDs array if (!validIDs.includes(optionValue)) { $(this).hide(); // Hide the option } else { $(this).show(); // Show the option (in case it was hidden before) } }); }; function FilterListByUserLocations(locations, FieldName) { $(".entitylist.entity-grid").on("loaded", function() { var cleanData = locations.trim(); // Convertir le contenu en objet JavaScript var locationList = JSON.parse(cleanData); const locationNames = [...new Set(locationList.map(location => location.LocationName))]; $(this).children(".view-grid").find("tbody tr").each(function() { var cell = $(this).find("td[data-attribute='" + FieldName + "']"); locationName = cell.attr("data-value"); // Remove the row if the location is not in the valid locations if (!locationNames.includes(locationName)) { $(this).remove(); } }); }); }; function FilterLookupFieldBasedOnUserLocationsandCustomer(optionSetField,locations,TypeofRecord,selectedLocationID,SelectedCustomerID,TypeofEntity) { // Supprimer les espaces ou caractères inutiles var cleanData = locations.trim(); // Convertir le contenu en objet JavaScript var locationList = JSON.parse(cleanData); var filteredLocations ; if(TypeofEntity === "Customer"){ filteredLocations= locationList.filter(item => item.LocationID === selectedLocationID && item.CustomerID === SelectedCustomerID); } if(TypeofEntity === "Vendor"){ filteredLocations= locationList.filter(item => item.LocationID === selectedLocationID && item.VendorID === SelectedCustomerID); } var locationIDs = []; if (selectedLocationID) { locationIDs.push(selectedLocationID); // Add the selected value to the array } switch (TypeofRecord) { case "Location": locationIDs = [ ...new Set(filteredLocations.map((location) => location.LocationID)), ]; break; case "Vendor": locationIDs = [ ...new Set(filteredLocations.map((location) => location.VendorID)), ]; break; case "Customer": locationIDs = [ ...new Set(filteredLocations.map((location) => location.CustomerID)), ]; break; case "Item": locationIDs = [ ...new Set(filteredLocations.map((location) => location.ItemID)), ]; break; } FilterOptionSet(locationIDs, optionSetField); }; function decodeHTMLEntities(text) { var textArea = document.createElement('textarea'); textArea.innerHTML = text; return textArea.value; }; function filterList(FieldName) { var locations = document.getElementById("locations").innerHTML; var cleanData = locations.trim(); // Convertir le contenu en objet JavaScript var locationList = JSON.parse(cleanData); const locationNames = [ ...new Set(locationList.map((location) => decodeHTMLEntities(location.LocationName))), ]; $(`div[data-automation-key='${FieldName}']`).each(function () { // Check if the cell's text matches the filter text const cellText = decodeHTMLEntities($(this).text().trim()); if (!locationNames.includes(cellText)) { $(this).closest("div.ms-List-cell").hide(); } else { $(this).closest("div.ms-List-cell").show(); // } }); } function ObserveAndFilter(dataAutomationKey) { debugger; const observer = new MutationObserver(() => { filterList(dataAutomationKey); // Trigger filter logic on DOM changes }); // Start observing when the list is detected const interval = setInterval(() => { const target = document.querySelector(".ms-DetailsList"); if (target) { clearInterval(interval); // Stop checking after finding the list observer.observe(target, { childList: true, subtree: true }); // Watch for changes filterList(dataAutomationKey); // Run the filter immediately } }, 500); } function ObserveAndFilterLocationCode(dataAutomationKey) { const observer = new MutationObserver(() => { filterListLocationCode(dataAutomationKey); // Trigger filter logic on DOM changes }); // Start observing when the list is detected const interval = setInterval(() => { const target = document.querySelector(".ms-DetailsList"); if (target) { clearInterval(interval); // Stop checking after finding the list observer.observe(target, { childList: true, subtree: true }); // Watch for changes filterListLocationCode(dataAutomationKey); // Run the filter immediately } }, 500); } function filterListLocationCode(FieldName) { var locations = document.getElementById("locations").innerHTML; var cleanData = locations.trim(); // Convertir le contenu en objet JavaScript var locationList = JSON.parse(cleanData); const locationCodes = [ ...new Set(locationList.map((location) => decodeHTMLEntities(location.LocationCode))), ]; $(`div[data-automation-key='${FieldName}']`).each(function () { // Check if the cell's text matches the filter text const cellText = decodeHTMLEntities($(this).text().trim()); if (!locationCodes.includes(cellText)) { $(this).closest("div.ms-List-cell").hide(); } else { $(this).closest("div.ms-List-cell").show(); // } }); } window.FilterOptionSet = FilterOptionSet; window.FilterLookupFieldBasedOnUserLocations = FilterLookupFieldBasedOnUserLocations; window.FilterFieldIOnLocationChange = FilterFieldIOnLocationChange; window.FilterListByUserLocations = FilterListByUserLocations; window.FilterLookupFieldBasedOnUserLocationsandCustomer = FilterLookupFieldBasedOnUserLocationsandCustomer; window.ObserveAndFilter = ObserveAndFilter; window.ObserveAndFilterLocationCode = ObserveAndFilterLocationCode;