1

Below is the form for advanced search:

Advanced Search Box

I am able to create a URL and parameters for document sections but I am unable to think of a process to handle the "Add Property Restrictions" section as the property can be added up to 5 times and it depends on the end user.

Like Below:

Advanced with Dynamic User Entries

So I want to handle it in AngularJS with addition/deletion and dynamic changes on the go and also to form the URL (GET/POST) to send the data for searching to the API for the back end.

1 Answer 1

1

You can handle this using an array of objects inside your model.

The structure of your model will be something like

let dataModel = {
      'allwords': '',
      'exact_phrase':'',
      /// .. the rest of your basic search model variables

      'property_res': [ {'property':'','action':'contains','value':'','logical_operator':'and'} ]
}

In your template, you will dynamicaly generate the list of property restrictions with ng-repeat of dataModel['property_res']

As for the "add property" - you just implement click handler that will append annother object (with same structure as initial row) to your dataModel['property_res'] , ng-repeat will take care of the rest.

To get the values to your POST request, you simly iterate through the array of dataModel['property_res'] and construct your variables, or you can just JSON.serialize() it and handle it on your server side.

Hope this will get you going!

EDIT

Adding example of ng-repeat rendering:

var app = angular.module('app', []);

app.controller('mainController', function($scope, $http) {
    
   $scope.dataModel = {
      'property_res': [ {'property':'','action':'contains','value':'','logical_operator':'and'} ]
   }
   
   $scope.addRow = function(){
      $scope.dataModel['property_res'].push({'property':'','action':'contains','value':'','logical_operator':'and'})
   }
   $scope.showModel= function(){
      console.log($scope.dataModel)
   }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
 
<div ng-app="app">
  <div ng-controller="mainController">
    
     <h1>Property restrictions:</h1>
     <div ng-repeat="ps in dataModel.property_res">
       <select ng-model="ps.property">
         <option value="">Pick property</option>
         <option value="Property 1">Property 1</option>
         <option value="Property 2">Property 2</option>
       </select>
       <select ng-model="ps.action">
         <option value="doesn't contain">doesn't contain</option>
         <option value="contains">contains</option>
         
       </select>
       <input ng-model="ps.value">
       <select ng-model="ps.logical_operator">
         <option value="or">or</option>
         <option value="and">and</option>
         
       </select>
     </div>
     <hr>
     <div><button ng-click="addRow()">Add Row</button></div>
     <hr>
     <div><button ng-click="showModel()">Console Log Model</button></div>
  </div>
</div>

Sign up to request clarification or add additional context in comments.

3 Comments

I would be happy and gratefull if you can show me how to do that on the view with ng-repeat as i am not getting sure of the code hoiw to do it with dynamic addition and maintenance of the property addition in advanced search. I could throughly get in terms of how the json to be made for POST data.
I've added angular 1 example snippet to demonstrate what i mean
Perfect @mike_t ... Thanks for the explanatory code which has cleared my view and even learnt a new thing.. Cheers mate :)

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.