My challenge involves an ajax query that fetches results to be displayed in an html table as shown below:
<input type="button" value="Customer" onclick="SelectCustomer()">
<input type="button" value="Advisor" onclick="SelectAdvisor()">
<input type="button" value="Get Results" onclick="GetResults()">
<table id="results">
</table>
I aim to filter these results based on the affected area, displaying items with a status of Open and specific areas like Advisors or Customers. While my current filtering code successfully filters by Status, I face difficulties when trying to filter by the Affected Area field. Experimenting with JavaScript variables to store selection information for filtering has not yielded expected results, especially if the field contains multiple values.
var dResponse = [
{"Reference": "123456","Status": "Open","AffectedArea": ["IT","Advisor"]}
{"Reference": "654321","Status": "Closed","AffectedArea": ["Customer","IT"]}
{"Reference": "567889","Status": "Open","AffectedArea": ["Advisor","Customer"]}
{"Reference": "987654","Status": "Open","AffectedArea": ["Customer"]}
]
var custSelect = "Unselected";
var adviSelect = "Unselected";
var custfil = "";
var advifil = "";
function SelectCustomer(){
if (custSelect == "Unselected") {
custSelect = "Selected";
adviSelect = "Unselected";
} else {
custSelect = "Unselected";
}
}
function SelectAdvisor(){
if (adviSelect == "Unselected") {
adviSelect = "Selected";
custSelect = "Unselected";
} else {
adviSelect = "Unselected";
}
}
function GetResults() {
var results = document.getElementById("results");
var filResponse = dResponse.filter(function(item){
return item.Status == "Open";
})
if (custSelect == "Selected"){
FilterCust();
}
if (adviSelect ==" Selected"){
FilterAdvi();
}
results.innerHTML += "<tr><td>Reference</td><td>Status</td><td>Affected Area</td></tr>";
for(var obj in filResponse){
results.innerHTML += "<tr><td>" + dResponse[obj].Reference + "</td><td>" + dResponse[obj].Status + "</td><td>" + dResponse[obj].AffectedArea + "</td></tr>";
}
}
function FilterCust() {
filResponse = dResponse.filter(function(item){
return item.ImpactedArea == "Customer";
})
}
function FilterAdvi() {
filResponse = dResponse.filter(function(item){
return item.ImpactedArea == "Advisor";
})
}
The JSON results display multiple values in the same field, stemming from a REST query to a SharePoint list. Despite facing challenges with filtering, the script manages to extract data as intended.