Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 142297

Is there a way to search a field in a JSON array that can contain multiple values for a specific value?

$
0
0

I have an ajax query that returns results and should display them in an html table like the 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'm wanting to filter this based on the affected area, so to show lists only items with a status of Open, and only impacting specific areas, such as Advisors, or Customers, etc. The current code I have to do this works in so far as it filters for Status, but I only get a blank return when I'm trying to filter the Affected Area field. I've been working with using JavaScript variables to contain information regarding whether a value has been selected or not, which is what I'm trying to filter against, however the filter script I have that shows only items with a Status value of Open doesn't seem to work for me if I have multiple values in the field.

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";
})
}

Admittedly, I'm not sure how the JSON results display multiple values in the same field, however the results come from a REST query to a SharePoint list, and the script works from that, the above was to give an idea regarding what sort of data the JSON table has in it.


Viewing all articles
Browse latest Browse all 142297

Trending Articles