0

I have a filtration function, a lot of checkboxes and dropdowns. When user selects multiple check boxes and selects the dropdown values they click on the "Filter Now" button.

That button then carries out a POST request to my API and pass along the filtration options as the parameters and return the data from MongoDB.

Heres my code:

factory.getFilteredProjects = function(regions, services, sector){

  return $http.post('/user/test',{
      region: regions,
      sol: services,
      sec: sector
  }).
    success(function(data, status, headers, config) {
        // this callback will be called asynchronously
        // when the response is available

        console.log("this is the response data " + data);
    }).
    error(function(data, status, headers, config) {
        // called asynchronously if an error occurs
        // or server returns response with an error status.
    });

  };    
     return factory;
 });

In the above code you can see I have 3 parameters (regions, services, sector)

Now the user only might want to filter the data by:

  1. Regions or Sector
  2. Just Regions
  3. Just Services
  4. Services and Regions

And So On!

My question:

How can I pass options parameters with my POST regions. At the moment I have to send all 3 parameters to get data back. If I don't send all 3 then I don't get any data back. Only the ones the user actually interacted with so basically something like:

// This is just to get my point across.
function(a || b || c){
} 

UPDATE:

Testing my API through POSTMan. As you can see I only sent 2 parameters and got a 200 status back and i am also getting the correct data back.

enter image description here

Thanks.

12
  • Have you verified the JSON is shaped the way you expect on the wire, using a your browser dev tools or something list postman? What is returned from the server (ie, what is the HTTP status code, etc.)? Commented Jan 29, 2016 at 15:05
  • @Noel thanks for the comment. When i use POSTMan I can 1 or 2 or 3 or 4 parameters and I still get data back. The API can handle it on the server. Commented Jan 29, 2016 at 15:06
  • Then you just need to detect which ones the user actually interacted, and send only them. Commented Jan 29, 2016 at 15:13
  • @AlexBlex any suggestions on how I can do that? Also see update on my question please. Commented Jan 29, 2016 at 15:17
  • @Noel Please see my update on the question. Commented Jan 29, 2016 at 15:18

2 Answers 2

1

You can just give an object in parameter instead of three string. Like this you can control the number of POST parameters instead of have some of them undefined.

EDIT : I suggest to do the filtering in your service. Like this, you don't have to complexify your code on each controller :

factory.getFilteredProjects = function(params){

  // remove empty value or empty array
  angular.forEach(params, function(value, key) {
    if( ! value || value.length === 0 ) {
      delete params[key];
    }
  })

  return $http.post('/user/test', params).
    success(function(data, status, headers, config) {
        // this callback will be called asynchronously
        // when the response is available

        console.log("this is the response data " + data);
    }).
    error(function(data, status, headers, config) {
        // called asynchronously if an error occurs
        // or server returns response with an error status.
    });

  };    
     return factory;
 });

getFilteredProjects({ region: 'test', sec: 'sector' })
Sign up to request clarification or add additional context in comments.

3 Comments

You right. I just copy/paste and change Lorenzo's code :)
that's fine...is note for OP also. But should be mentioned
@charlietfl thanks for the Info. I'll change the code.
0

so after doing some research, a few cups of coffee and couple of swear words later I got this working.

But heres a note:

First thanks to Yoann Prot your suggestion was a huge help!

This is my initial solution, I will NOT be accepting my own answer. Because there might be a better solution to this and I would like someone to post an answer/comment if they think it can be improved

So my if you read the comments above you know my API was able to handle multiple or flexible number parameters. The issue was my HTTP Post function required all parameters to be present.

As Alex Blex suggested in the comments that:

Then you just need to detect which ones the user actually interacted, and send only them

And thats exactly what I did.

I created a filter object to which I added key/value pairs of only the options that the user interacted with and passed that whole filter object as the parameter. This made my parameters for the HTTP Post request much more flexible.

Heres the code:

 var filterObj = {};

    var form = document.getElementById("regionPicker");
    var servicesForm = document.getElementById("servicesPicker");

    var inputs = form.getElementsByTagName("input");
    var arr = [];

    var servicesInput = servicesForm.getElementsByTagName("input");
    var servicesArr = [];

    for (var i = 0;  i < inputs.length; i += 1) {
         // Take only those inputs which are checkbox
        if (inputs[i].type === "checkbox" && inputs[i].checked) {
                 arr.push(inputs[i].value);
         }
     }

    for (var i = 0;  i < servicesInput.length; i += 1) {
         // Take only those inputs which are checkbox
        if (servicesInput[i].type === "checkbox" && servicesInput[i].checked) {
                 servicesArr.push(servicesInput[i].value);
          }
       }


 // here arr contains an array of filter options selected by user
 filterObj.region = (arr.length > 0) ? arr:"";

 // here serviceArr contains an array of another filter options selected by user
 filterObj.sol = (servicesArr.length > 0) ? servicesArr:"";

And finally pass the filterObj as the parameter:

factory.getFilteredProjects = function(filterObj){

  return $http.post('/user/test',filterObj)
  .success(function(data, status, headers, config) {
       console.log("this is the response data " + data.length);
    })
  .error(function(data, status, headers, config) {
        // called asynchronously if an error occurs
        // or server returns response with an error status.
    });
  }; 

So far all the tests have been successful. But I repeat if you know a better solution please let me know or if you think this solution has drawbacks or issues or is just bad practice please let me know.

Thanks!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.