Click here to Skip to main content
15,885,435 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a list of names and have saved them in an array.
then i am passing the array to a get ajax function as a parameter to return the required info.

what i am trying to achieve is to filter that list based on the values in the array.

I tried using filter as show below but it is not return anything and if i remove the filter it returns everything in the list:

url: "https://mysite/_api/EmployeeInfo?$filter=FullName eq'" + dataarray+ "'",


I have shown my function below please help as i am not very familiar with how to go about this. any help is greatly appreciated.

What I have tried:

function getEmployeeDetails(id) {
   console.log(id);

    var dataarray=JSON.stringify(id);

       var dfd = $.Deferred();
       $.ajax({

           url: "https://mysite/_api/EmployeeInfo?$filter=FullName eq'" + dataarray+ "'",

           data:JSON.stringify(id) ,
            type: "GET",
           dataType: "json",
           headers: {
               "accept": "application/json;odata=verbose"
           },
           success: function(data) {
               dfd.resolve(data);
               console.log(data);
           },
           error: function(data, errCode, errMessage) {
               {
                   swal('Error', 'Item not found', 'error')
               }
           }
       });
       return dfd.promise();

   }
Posted
Updated 26-Jun-17 6:57am

1 solution

When your array contains multiple items, your query will look something like:
FullName eq 'a,b,c'

This will only return records where the FullName is exactly equal to all the names from your array combined into one comma-separated list. That's obviously not going to work.

You need to build a query for each value, and combine them with the or operator:
url: "...?$filter=FullName eq '" + dataarray.join("' or FullName eq '") + "'",

That will generate a query that looks like:
FullName eq 'a' or FullName eq 'b' or FullName eq 'c'
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900