cfoster5
cfoster5

Reputation: 1826

How can I filter a GET request?

Using a web request, I am retrieving data and displaying it on a web page. Rather than using Angular's ng-if and hiding data that doesn't meet the criteria, I'd like to simply not retrieve the data at all.

The JS:

var app = angular.module('myApp', ['ngSanitize']);
app.controller('MainCtrl', function($scope, $http, $q){
  $(document).ready(function() {
    $scope.getAdminList();
  });
    $scope.prepContext = function(url,listname,query){
        var path = url + "/_api/web/lists/getbytitle('" + listname + "')/items" + query;
        console.log(path);
        return path;
    }
    $scope.getAdminList = function() {
    adminList = $http({
      method: 'GET',
      url: this.prepContext(siteOrigin+"/corporate/projecthub/anchormn/associates","User Administration","?$orderBy=LastName"),
      headers: {
        "Accept": "application/json; odata=verbose"
      }
    }).then(function(data) {
      //$("#articleSection").fadeIn(2000);
      console.log("adminlist", data.data.d.results);
      $scope.users = data.data.d.results;
    });
  };
});

Logging data.data.d.results; logs an object array similar to:

{
  0: {
    "ID": 21,
    "Name": Me
    },
  1: {
    "ID": 14,
    "Name": Test
    },
  2: {
    "ID": 3,
    "Name": Test1
    }
}

Instead of using ng-if="user.ID == 21, how can I retrieve the item only using a web request?

Upvotes: 1

Views: 23934

Answers (3)

Tymur Abdullaiev
Tymur Abdullaiev

Reputation: 471

$scope.users = (data.data.d.results).filter(function(user) {
    return user.ID === 21;
});

Upvotes: 0

Akki
Akki

Reputation: 83

Using ngResource (query) which is also GET request and using ControllerAS syntax:    
        var app = angular.module('myApp', [ 'ngResource' ]);
        app.factory('userOrderID', function($resource) {
            return $resource(
                    '/URL/:userID ', {
                        userID : '@uID'
                    });
        });
        app.controller('ExampleController', ExampleController);
        function ExampleController(userOrderID) {
        //findtheResult function being called from HTML on click of search button
            this.findtheResult = function() {
                this.userDetails = [];
                this.userDetails = userOrderID.query({
                        userID : this.userID //passed from html page as input type
                    }, function(data) {
                        if (data.length == 0) {
                            alert("No Master ID found in database");
                        }
                    });
        };
        };

Upvotes: 0

Raghu
Raghu

Reputation: 709

From your request header and URI ($orderBy), I understand your server is an OData server. If the server implements the filters correctly, you could use something OData $filter expressions as part of your query string similar to your $orderBy.

Example:

GET Products?$filter=ProductName+eq+%27iPhone%27

Please check the URL: http://www.odata.org/documentation/odata-version-2-0/uri-conventions/

Logical Operators

Eq  Equal   /Suppliers?$filter=Address/City eq 'Redmond'
Ne  Not equal   /Suppliers?$filter=Address/City ne 'London'
Gt  Greater than    /Products?$filter=Price gt 20
Ge  Greater than or equal   /Products?$filter=Price ge 10
Lt  Less than   /Products?$filter=Price lt 20
Le  Less than or equal  /Products?$filter=Price le 100
And Logical and /Products?$filter=Price le 200 and Price gt 3.5
Or  Logical or  /Products?$filter=Price le 3.5 or Price gt 200
Not Logical negation    /Products?$filter=not endswith(Description,'milk')

Arithmetic Operators
Add Addition    /Products?$filter=Price add 5 gt 10
Sub Subtraction /Products?$filter=Price sub 5 gt 10
Mul Multiplication  /Products?$filter=Price mul 2 gt 2000
Div Division    /Products?$filter=Price div 2 gt 4
Mod Modulo  /Products?$filter=Price mod 2 eq 0

Grouping Operators
( ) Precedence grouping /Products?$filter=(Price sub 5) gt 10

Upvotes: 1

Related Questions